Skip to content

Commit a11bfef

Browse files
committed
fix: Double
1 parent fe0b993 commit a11bfef

7 files changed

Lines changed: 127 additions & 6 deletions

File tree

src/main/java/blue/language/Blue.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,17 @@ public <T> T convertObject(Object object, Class<T> clazz) {
8080
}
8181

8282
public boolean nodeMatchesType(Node node, Node type) {
83-
return new NodeTypeMatcher(this).matchesType(node, type);
83+
return new NodeTypeMatcher(this).matchesType(node, type, globalLimits);
8484
}
8585

8686
public void setGlobalLimits(Limits globalLimits) {
8787
this.globalLimits = globalLimits != null ? globalLimits : NO_LIMITS;
8888
}
8989

90+
public Limits getGlobalLimits() {
91+
return globalLimits;
92+
}
93+
9094
public Node yamlToNode(String yaml) {
9195
return preprocess(YAML_MAPPER.readValue(yaml, Node.class));
9296
}

src/main/java/blue/language/mapping/ComplexObjectConverter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public Object convert(Node node, Type targetType, boolean prioritizeTargetType)
4444
return ValueConverter.convertValue(node, classToInstantiate);
4545
}
4646

47+
if (resolvedClass != null && getRawType(targetType).isAssignableFrom(resolvedClass)) {
48+
classToInstantiate = resolvedClass;
49+
}
50+
4751
try {
4852
Object instance = classToInstantiate.getDeclaredConstructor().newInstance();
4953
convertFields(node, classToInstantiate, instance);

src/main/java/blue/language/model/Constraints.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import static blue.language.utils.TypeUtils.*;
1313

14-
public class Constraints {
14+
public class Constraints implements Cloneable {
1515

1616
private Node required;
1717
private Node allowMultiple;
@@ -288,6 +288,41 @@ public Constraints uniqueItems(Boolean uniqueItems) {
288288
return this;
289289
}
290290

291+
@Override
292+
public Constraints clone() {
293+
try {
294+
Constraints cloned = (Constraints) super.clone();
295+
296+
if (this.required != null) cloned.required = this.required.clone();
297+
if (this.allowMultiple != null) cloned.allowMultiple = this.allowMultiple.clone();
298+
if (this.minLength != null) cloned.minLength = this.minLength.clone();
299+
if (this.maxLength != null) cloned.maxLength = this.maxLength.clone();
300+
if (this.minimum != null) cloned.minimum = this.minimum.clone();
301+
if (this.maximum != null) cloned.maximum = this.maximum.clone();
302+
if (this.exclusiveMinimum != null) cloned.exclusiveMinimum = this.exclusiveMinimum.clone();
303+
if (this.exclusiveMaximum != null) cloned.exclusiveMaximum = this.exclusiveMaximum.clone();
304+
if (this.multipleOf != null) cloned.multipleOf = this.multipleOf.clone();
305+
if (this.minItems != null) cloned.minItems = this.minItems.clone();
306+
if (this.maxItems != null) cloned.maxItems = this.maxItems.clone();
307+
if (this.uniqueItems != null) cloned.uniqueItems = this.uniqueItems.clone();
308+
309+
if (this.pattern != null) {
310+
cloned.pattern = this.pattern.stream()
311+
.map(Node::clone)
312+
.collect(Collectors.toList());
313+
}
314+
if (this.options != null) {
315+
cloned.options = this.options.stream()
316+
.map(Node::clone)
317+
.collect(Collectors.toList());
318+
}
319+
320+
return cloned;
321+
} catch (CloneNotSupportedException e) {
322+
throw new AssertionError("Constraints must be cloneable", e);
323+
}
324+
}
325+
291326
@Override
292327
public String toString() {
293328
return "Constraints{" +

src/main/java/blue/language/model/Node.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@ public Integer getAsInteger(String path) {
262262
}
263263
}
264264

265-
@Override
266-
public Node clone() {
265+
public Node clone2() {
267266
try {
268267
Node cloned = (Node) super.clone();
269268
if (this.type != null) {
@@ -300,6 +299,61 @@ public Node clone() {
300299
}
301300
}
302301

302+
@Override
303+
public Node clone() {
304+
try {
305+
Node cloned = (Node) super.clone();
306+
307+
cloned.name = this.name;
308+
cloned.description = this.description;
309+
cloned.value = this.value;
310+
311+
if (this.type != null) {
312+
cloned.type = this.type.clone();
313+
}
314+
315+
if (this.itemType != null) {
316+
cloned.itemType = this.itemType.clone();
317+
}
318+
319+
if (this.keyType != null) {
320+
cloned.keyType = this.keyType.clone();
321+
}
322+
323+
if (this.valueType != null) {
324+
cloned.valueType = this.valueType.clone();
325+
}
326+
327+
if (this.items != null) {
328+
cloned.items = this.items.stream()
329+
.map(Node::clone)
330+
.collect(Collectors.toCollection(ArrayList::new));
331+
}
332+
333+
if (this.properties != null) {
334+
cloned.properties = this.properties.entrySet().stream()
335+
.collect(Collectors.toMap(
336+
Map.Entry::getKey,
337+
entry -> entry.getValue().clone(),
338+
(e1, e2) -> e1,
339+
HashMap::new
340+
));
341+
}
342+
343+
if (this.constraints != null) {
344+
cloned.constraints = this.constraints.clone();
345+
}
346+
347+
if (this.blue != null) {
348+
cloned.blue = this.blue.clone();
349+
}
350+
351+
return cloned;
352+
} catch (CloneNotSupportedException e) {
353+
throw new AssertionError("Node must be cloneable", e);
354+
}
355+
}
356+
303357
@Override
304358
public String toString() {
305359
return "Node{" +

src/main/java/blue/language/preprocess/Preprocessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
public class Preprocessor {
2020

21-
public static final String DEFAULT_BLUE_BLUE_ID = "5y7V3sYaj72uet7gc3WFjADBUhFe4bGWzQtmZ8v3wr8z";
21+
public static final String DEFAULT_BLUE_BLUE_ID = "8Na7RPBKK2rkDR1iUBhMjmUg5BAku3YfihLCeN3SYcuD";
2222

2323
private TransformationProcessorProvider processorProvider;
2424
private NodeProvider nodeProvider;

src/main/resources/transformation/DefaultBlue.blue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
blueId: 648mybP3oHoi8kx6HvYzw6Jtuz6BjUpGPk1r2DpCq7e4
33
mappings:
44
Text: F92yo19rCcbBoBSpUA5LRxpfDejJDAaP1PRxxbWAraVP
5-
Number: 68ryJtnmui4j5rCZWUnkZ3DChtmEb7Z9F8atn1mBSM3L
5+
Double: 68ryJtnmui4j5rCZWUnkZ3DChtmEb7Z9F8atn1mBSM3L
66
Integer: DHmxTkFbXePZHCHCYmQr2dSzcNLcryFVjXVHkdQrrZr8
77
Boolean: EL6AjrbJsxTWRTPzY8WR8Y2zAMXRbydQj83PcZwuAHbo
88
List: G8wmfjEqugPEEXByMYWJXiEdbLToPRWNQEekNxrxfQWB

src/test/java/blue/language/mapping/NodeToObjectConverterTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import blue.language.utils.BlueIdCalculator;
77
import blue.language.utils.Properties;
88
import blue.language.utils.TypeClassResolver;
9+
import blue.language.utils.UncheckedObjectMapper;
910
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.Test;
1112

@@ -760,4 +761,27 @@ public void testXSubscriptionConversion() throws Exception {
760761
assertEquals(Integer.valueOf(5), subscription2.getSubscriptionId());
761762
}
762763

764+
@Test
765+
public void testObjectSimple() throws Exception {
766+
String personTestDataYaml = "type:\n" +
767+
" blueId: Nurse-BlueId\n" +
768+
"name: Alice\n" +
769+
"surname: Smith\n" +
770+
"age: 25\n" +
771+
"yearsOfExperience: 4";
772+
773+
Node node = blue.yamlToNode(personTestDataYaml);
774+
775+
Person data = converter.convert(node, Person.class);
776+
777+
assertNotNull(data);
778+
779+
assertInstanceOf(Nurse.class, data);
780+
Nurse nurse = (Nurse) data;
781+
assertEquals("Alice", nurse.getName());
782+
assertEquals("Smith", nurse.getSurname());
783+
assertEquals(Integer.valueOf(25), nurse.getAge());
784+
assertEquals(Integer.valueOf(4), nurse.yearsOfExperience);
785+
}
786+
763787
}

0 commit comments

Comments
 (0)