Skip to content

Commit 3d5c7da

Browse files
authored
121 math type constants (#128)
* Support constant value with math type constructor arg * Impl constant value reference for qualified name * Fix math type constructor arg referencing constant value
1 parent bcbc06e commit 3d5c7da

30 files changed

Lines changed: 637 additions & 131 deletions

File tree

annotation/src/main/java/online/sharedtype/SharedType.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@
127127
*
128128
* <p>
129129
* <b>Math types:</b><br>
130-
* By default {@link java.math.BigInteger} and {@link java.math.BigDecimal} are emitted number types.
131-
* A client can use type mappings to override the emitted types. Default mappings are:
132-
* <ul>
133-
* <li>Typescript: {@code number}</li>
134-
* <li>Rust: {@code i128} and {@code f64}</li>
135-
* </ul>
130+
* By default {@link java.math.BigInteger} and {@link java.math.BigDecimal} are emitted as strings.
131+
* A client can use type mappings to override the emitted types.
132+
* <br>
133+
* Note: Constants' values with math types are converted to strings. Custom mapping of math types will not affect constant value type.
134+
* See "Constants" section for details.
135+
* </p><br>
136136
*
137137
* <p>
138138
* <b>Type literal mappings:</b><br>

client-test/rust/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ pub static CUSTOM_CODE_TYPE: types::CustomInjectedStruct = types::CustomInjected
44
field: 33,
55
};
66

7-
pub static MATH_CLASS: types::MathClass = types::MathClass {
8-
bigInteger: 500000000i128,
9-
bigDecimal: 6.534543474564f64,
10-
};
11-
127
#[cfg(test)]
138
mod tests {
149
use std::collections::HashMap;
@@ -95,11 +90,21 @@ mod tests {
9590
);
9691
}
9792

93+
#[test]
94+
fn math_classes() {
95+
let _: MathClass = MathClass {
96+
bigInteger: String::from("500000000"),
97+
bigDecimal: String::from("500000000.123456789"),
98+
};
99+
}
100+
98101
#[test]
99102
fn constants() {
100103
assert_eq!(STATIC_FIELD_FROM_JAVA_RECORD, 888);
101104
assert_eq!(FLOAT_VALUE, 1.888);
102105
assert_eq!(LONG_VALUE, 999);
106+
assert_eq!(MATH_VALUE, "1.1");
107+
assert_eq!(MATH_VALUE_QUALIFIED_NAME, "88885555");
103108

104109
assert_eq!(MyEnumConstants::INT_VALUE, 1);
105110
assert_eq!(MyEnumConstants::STR_VALUE, "abc");

client-test/typescript/src/types.java17.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ export const jodaTime: JodaTimeClass = {
134134
}
135135

136136
export const mathClass: MathClass = {
137-
bigDecimal: 1,
138-
bigInteger: 1,
137+
bigDecimal: "1.1",
138+
bigInteger: "8888888555555",
139139
}
140140

141141

client-test/typescript/tests/runtime.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
REFERENCED_PACKAGE_PRIVATE_VALUE_IN_STATIC_BLOCK, REFERENCED_SUPER_VALUE_IN_STATIC_BLOCK, SELECTED_SUPER_VALUE_IN_STATIC_BLOCK,
99
REFERENCED_STATIC_VALUE_IN_STATIC_BLOCK, INNER_REFERENCED_SUPER_VALUE_IN_STATIC,
1010
REFERENCED_ENUM_VALUE, REFERENCED_ENUM_VALUE_IN_STATIC_BLOCK,
11+
MATH_VALUE, MATH_VALUE_QUALIFIED_NAME, MATH_VALUE_REFERENCED_LOCAL_VALUE
1112
} from '../src/index.java17';
1213

1314
test('Constant values', () => {
@@ -38,4 +39,7 @@ test('Constant values', () => {
3839
expect(INNER_REFERENCED_SUPER_VALUE_IN_STATIC).toBe(345);
3940
expect(REFERENCED_ENUM_VALUE).toBe("MilkyWay");
4041
expect(REFERENCED_ENUM_VALUE_IN_STATIC_BLOCK).toBe("MilkyWay");
42+
expect(MATH_VALUE).toBe("1.1");
43+
expect(MATH_VALUE_QUALIFIED_NAME).toBe("88885555");
44+
expect(MATH_VALUE_REFERENCED_LOCAL_VALUE).toBe("555");
4145
});

internal/src/main/java/online/sharedtype/processor/domain/Constants.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public final class Constants {
3838

3939
public static final ConcreteTypeInfo STRING_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.String", "String");
4040
public static final ConcreteTypeInfo VOID_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Void", "Void");
41+
public static final ConcreteTypeInfo NULL_TYPE_INFO = ConcreteTypeInfo.ofPredefined("null", "null");
4142
public static final ConcreteTypeInfo OBJECT_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Object", "Object");
4243
public static final ConcreteTypeInfo CLASS_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Class", "Class");
4344
public static final ConcreteTypeInfo ENUM_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Enum", "Enum");
@@ -92,6 +93,18 @@ public final class Constants {
9293
LITERAL_TYPES.add(BOXED_CHAR_TYPE_INFO);
9394
}
9495

96+
public static final Set<ConcreteTypeInfo> MATH_TYPES = new HashSet<>(2);
97+
static {
98+
MATH_TYPES.add(BIG_DECIMAL_TYPE_INFO);
99+
MATH_TYPES.add(BIG_INTEGER_TYPE_INFO);
100+
}
101+
public static final Set<String> MATH_TYPE_QUALIFIED_NAMES = new HashSet<>(2);
102+
static {
103+
for (ConcreteTypeInfo mathType : MATH_TYPES) {
104+
MATH_TYPE_QUALIFIED_NAMES.add(mathType.qualifiedName());
105+
}
106+
}
107+
95108
private Constants() {
96109
}
97110
}

internal/src/main/java/online/sharedtype/processor/domain/value/ValueHolder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package online.sharedtype.processor.domain.value;
22

3+
import online.sharedtype.processor.domain.Constants;
34
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
45
import online.sharedtype.processor.domain.type.TypeInfo;
56

@@ -37,5 +38,5 @@ static EnumConstantValue ofEnum(String enumConstantName, ConcreteTypeInfo valueT
3738
return new EnumConstantValue(enumConstantName, actualValueType, actualValue);
3839
}
3940

40-
LiteralValue NULL = new LiteralValue(null,null);
41+
LiteralValue NULL = new LiteralValue(Constants.NULL_TYPE_INFO,null);
4142
}

it/java17/src/test/java/online/sharedtype/it/ConstantsIntegrationTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void parseMyConstants() {
1313
ConstantNamespaceDef constantsDef = (ConstantNamespaceDef) deserializeTypeDef("$online.sharedtype.it.java8.MyConstants.ser");
1414
assertThat(constantsDef.simpleName()).isEqualTo("MyConstants");
1515
var components = constantsDef.components();
16-
assertThat(components).hasSize(28);
16+
assertThat(components).hasSize(33);
1717
assertThat(components).satisfiesExactly(
1818
component -> {
1919
assertThat(component.name()).isEqualTo("FLOAT_VALUE");
@@ -31,6 +31,10 @@ void parseMyConstants() {
3131
assertThat(component.name()).isEqualTo("SELF_REFERENCED_LOCAL_VALUE");
3232
assertThat(component.value().getValue()).isEqualTo(555L);
3333
},
34+
component -> {
35+
assertThat(component.name()).isEqualTo("SELF_REFERENCED_LOCAL_VALUE_QUALIFIED_NAME");
36+
assertThat(component.value().getValue()).isEqualTo(555L);
37+
},
3438
component -> {
3539
assertThat(component.name()).isEqualTo("REFERENCED_IMPORTED_VALUE");
3640
assertThat(component.value().getValue()).isEqualTo(666L);
@@ -84,6 +88,10 @@ void parseMyConstants() {
8488
assertThat(component.name()).isEqualTo("SELF_REFERENCED_LOCAL_VALUE_IN_STATIC_BLOCK");
8589
assertThat(component.value().getValue()).isEqualTo(555L);
8690
},
91+
component -> {
92+
assertThat(component.name()).isEqualTo("SELF_REFERENCED_LOCAL_VALUE_QUALIFIED_NAME_IN_STATIC_BLOCK");
93+
assertThat(component.value().getValue()).isEqualTo(555L);
94+
},
8795
component -> {
8896
assertThat(component.name()).isEqualTo("REFERENCED_IMPORTED_VALUE_IN_STATIC_BLOCK");
8997
assertThat(component.value().getValue()).isEqualTo(666L);
@@ -128,6 +136,18 @@ void parseMyConstants() {
128136
component -> {
129137
assertThat(component.name()).isEqualTo("REFERENCED_ENUM_VALUE3_IN_STATIC_BLOCK");
130138
assertThat(component.value().getValue()).isEqualTo(1);
139+
},
140+
component -> {
141+
assertThat(component.name()).isEqualTo("MATH_VALUE");
142+
assertThat(component.value().getValue()).isEqualTo("1.1");
143+
},
144+
component -> {
145+
assertThat(component.name()).isEqualTo("MATH_VALUE_QUALIFIED_NAME");
146+
assertThat(component.value().getValue()).isEqualTo("88885555");
147+
},
148+
component -> {
149+
assertThat(component.name()).isEqualTo("MATH_VALUE_REFERENCED_LOCAL_VALUE");
150+
assertThat(component.value().getValue()).isEqualTo("555");
131151
}
132152
);
133153
}

it/java8/src/main/java/online/sharedtype/it/java8/MyConstants.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import online.sharedtype.SharedType;
44
import online.sharedtype.it.java8.other.OtherConstants;
55

6+
import java.math.BigDecimal;
7+
import java.math.BigInteger;
8+
69
import static online.sharedtype.it.java8.other.OtherConstants.Inner1.STATIC_IMPORTED_VALUE;
710

811
@SharedType(constantNamespaced = SharedType.OptionalBool.FALSE, includes = SharedType.ComponentType.CONSTANTS)
@@ -13,6 +16,7 @@ final class MyConstants extends IgnoredSuperClassB {
1316
static final long IGNORED_LOCAL_VALUE = 555L;
1417
static final long REFERENCED_LOCAL_VALUE = IGNORED_LOCAL_VALUE;
1518
static final long SELF_REFERENCED_LOCAL_VALUE = MyConstants.IGNORED_LOCAL_VALUE;
19+
static final long SELF_REFERENCED_LOCAL_VALUE_QUALIFIED_NAME = online.sharedtype.it.java8.MyConstants.IGNORED_LOCAL_VALUE;
1620
static final long REFERENCED_IMPORTED_VALUE = OtherConstants.LONG_VALUE;
1721
static final long REFERENCED_NESTED_VALUE = OtherConstants.Inner1.Inner2.INNER_LONG_VALUE;
1822
static final long REFERENCED_STATIC_IMPORTED_VALUE = STATIC_IMPORTED_VALUE;
@@ -27,6 +31,7 @@ final class MyConstants extends IgnoredSuperClassB {
2731
static final long REFERENCED_VALUE_IN_STATIC_BLOCK;
2832
static final long REFERENCED_LOCAL_VALUE_IN_STATIC_BLOCK;
2933
static final long SELF_REFERENCED_LOCAL_VALUE_IN_STATIC_BLOCK;
34+
static final long SELF_REFERENCED_LOCAL_VALUE_QUALIFIED_NAME_IN_STATIC_BLOCK;
3035
static final long REFERENCED_IMPORTED_VALUE_IN_STATIC_BLOCK;
3136
static final long REFERENCED_NESTED_VALUE_IN_STATIC_BLOCK;
3237
static final long REFERENCED_STATIC_IMPORTED_VALUE_IN_STATIC_BLOCK;
@@ -42,6 +47,7 @@ final class MyConstants extends IgnoredSuperClassB {
4247
REFERENCED_VALUE_IN_STATIC_BLOCK = 112L;
4348
REFERENCED_LOCAL_VALUE_IN_STATIC_BLOCK = IGNORED_LOCAL_VALUE;
4449
SELF_REFERENCED_LOCAL_VALUE_IN_STATIC_BLOCK = MyConstants.IGNORED_LOCAL_VALUE;
50+
SELF_REFERENCED_LOCAL_VALUE_QUALIFIED_NAME_IN_STATIC_BLOCK = online.sharedtype.it.java8.MyConstants.IGNORED_LOCAL_VALUE;
4551
REFERENCED_IMPORTED_VALUE_IN_STATIC_BLOCK = OtherConstants.LONG_VALUE;
4652
REFERENCED_NESTED_VALUE_IN_STATIC_BLOCK = OtherConstants.Inner1.Inner2.INNER_LONG_VALUE;
4753
REFERENCED_STATIC_IMPORTED_VALUE_IN_STATIC_BLOCK = STATIC_IMPORTED_VALUE;
@@ -54,6 +60,9 @@ final class MyConstants extends IgnoredSuperClassB {
5460
REFERENCED_ENUM_VALUE2_IN_STATIC_BLOCK = EnumTShirt.S;
5561
REFERENCED_ENUM_VALUE3_IN_STATIC_BLOCK = EnumSize.SMALL;
5662
}
63+
static final BigDecimal MATH_VALUE = new BigDecimal("1.1");
64+
static final BigInteger MATH_VALUE_QUALIFIED_NAME = new java.math.BigInteger("88885555");
65+
static final BigDecimal MATH_VALUE_REFERENCED_LOCAL_VALUE = new java.math.BigDecimal(MyConstants.REFERENCED_LOCAL_VALUE);
5766

5867
@SharedType(constantNamespaced = SharedType.OptionalBool.FALSE, includes = SharedType.ComponentType.CONSTANTS)
5968
static final class InnerConstantClass {
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package online.sharedtype.it.java8;
22

3-
import lombok.RequiredArgsConstructor;
43
import online.sharedtype.SharedType;
54

6-
public class TempClass extends IgnoredSuperClassB {
7-
5+
import java.math.BigDecimal;
86

7+
@SharedType(includes = SharedType.ComponentType.CONSTANTS)
8+
public class TempClass extends IgnoredSuperClassB {
9+
// static final BigDecimal VALUE2 = BigDecimal.valueOf(555);
910
}

processor/src/main/java/online/sharedtype/processor/parser/value/ConstantValueParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public ValueHolder resolve(Element fieldElement, TypeElement ctxTypeElement) {
4242
" the type is '%s'.", fieldElement.asType());
4343
return ValueHolder.NULL;
4444
} catch (SharedTypeException e) {
45-
ctx.error(fieldElement, "Failed to resolve constant value. " +
46-
"Field tree: %s in %s. Consider to ignore this field or exclude constants generation for this type. " +
45+
ctx.error(fieldElement, "Failed to parse constant value. " +
46+
"Field tree: '%s' in '%s'. Consider to ignore this field or exclude constants generation for this type. " +
4747
"Error message: %s",
4848
tree, ctxTypeElement, e.getMessage());
4949
}

0 commit comments

Comments
 (0)