Skip to content

Commit af92000

Browse files
Add item ordering in record value mapper
1 parent f4f65b4 commit af92000

File tree

3 files changed

+151
-22
lines changed

3 files changed

+151
-22
lines changed

contrib/src/main/java/gov/nasa/jpl/aerie/contrib/serialization/mappers/RecordValueMapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.reflect.Constructor;
99
import java.lang.reflect.InvocationTargetException;
1010
import java.lang.reflect.RecordComponent;
11+
import java.util.ArrayList;
1112
import java.util.Arrays;
1213
import java.util.HashMap;
1314
import java.util.HashSet;
@@ -36,11 +37,15 @@ public record Component<R, T>(
3637
@Override
3738
public ValueSchema getValueSchema() {
3839
final var valueSchemas = new HashMap<String, ValueSchema>();
40+
final var metadata = new ArrayList<SerializedValue>();
3941
for (final var component : this.components) {
42+
metadata.add(SerializedValue.of(component.name));
4043
valueSchemas.put(component.name,
4144
component.mapper.getValueSchema());
4245
}
43-
return ValueSchema.ofStruct(valueSchemas);
46+
return components.isEmpty() ?
47+
ValueSchema.ofStruct(valueSchemas) :
48+
ValueSchema.withMeta("item_order", SerializedValue.of(metadata), ValueSchema.ofStruct(valueSchemas));
4449
}
4550

4651
@Override

contrib/src/test/java/gov/nasa/jpl/aerie/contrib/serialization/mappers/RecordValueMapperTest.java

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ record EmptyRecord() {}
1616
record StringRecord(String aString) {}
1717

1818
record MultiValueRecord(String aString, Map<String, List<Boolean>> fancy) {}
19+
record NestedMultiValueRecord(String aString, Map<String, List<Boolean>> fancy, MultiValueRecord record) {}
1920

2021
@Test
2122
void getValueSchema_emptyRecord() {
@@ -32,12 +33,16 @@ void getValueSchema_stringRecord() {
3233
StringRecord::aString,
3334
new StringValueMapper()
3435
))).getValueSchema();
35-
assertEquals(ValueSchema.ofStruct(Map.of("aString", ValueSchema.STRING)), valueSchema);
36+
assertEquals(ValueSchema.withMeta(
37+
"item_order",
38+
SerializedValue.of(List.of(SerializedValue.of("aString"))),
39+
ValueSchema.ofStruct(Map.of("aString", ValueSchema.STRING))),
40+
valueSchema);
41+
3642
}
3743

38-
@Test
39-
void getValueSchema_multiValueRecord() {
40-
final var valueSchema = new RecordValueMapper<>(
44+
final RecordValueMapper<MultiValueRecord> getMultiValueRecordValueMapper() {
45+
return new RecordValueMapper<>(
4146
MultiValueRecord.class,
4247
List.of(
4348
new RecordValueMapper.Component<>(
@@ -49,15 +54,65 @@ void getValueSchema_multiValueRecord() {
4954
"fancy",
5055
MultiValueRecord::fancy,
5156
new MapValueMapper<>(new StringValueMapper(), new ListValueMapper<>(new BooleanValueMapper()))
57+
)));
58+
}
59+
60+
@Test
61+
void getValueSchema_multiValueRecord() {
62+
final var valueSchema = getMultiValueRecordValueMapper().getValueSchema();
63+
assertEquals(
64+
ValueSchema.withMeta(
65+
"item_order",
66+
SerializedValue.of(List.of(SerializedValue.of("aString"), SerializedValue.of("fancy"))),
67+
ValueSchema.ofStruct(Map.of(
68+
"aString", ValueSchema.STRING,
69+
"fancy", ValueSchema.ofSeries(ValueSchema.ofStruct(Map.of(
70+
"key", ValueSchema.STRING,
71+
"value", ValueSchema.ofSeries(ValueSchema.BOOLEAN))))))),
72+
valueSchema);
73+
}
74+
75+
@Test
76+
void getValueSchema_nested() {
77+
final var valueSchema = new RecordValueMapper<>(
78+
NestedMultiValueRecord.class,
79+
List.of(
80+
new RecordValueMapper.Component<>(
81+
"aString",
82+
NestedMultiValueRecord::aString,
83+
new StringValueMapper()
84+
),
85+
new RecordValueMapper.Component<>(
86+
"fancy",
87+
NestedMultiValueRecord::fancy,
88+
new MapValueMapper<>(new StringValueMapper(), new ListValueMapper<>(new BooleanValueMapper()))
89+
),
90+
new RecordValueMapper.Component<>(
91+
"record",
92+
NestedMultiValueRecord::record,
93+
getMultiValueRecordValueMapper()
5294
))).getValueSchema();
53-
assertEquals(ValueSchema.ofStruct(Map.of(
54-
"aString", ValueSchema.STRING,
55-
"fancy", ValueSchema.ofSeries(
95+
assertEquals(
96+
ValueSchema.withMeta(
97+
"item_order",
98+
SerializedValue.of(List.of(
99+
SerializedValue.of("aString"),
100+
SerializedValue.of("fancy"),
101+
SerializedValue.of("record"))),
56102
ValueSchema.ofStruct(Map.of(
57-
"key", ValueSchema.STRING,
58-
"value", ValueSchema.ofSeries(ValueSchema.BOOLEAN)
59-
))
60-
))), valueSchema);
103+
"aString", ValueSchema.STRING,
104+
"fancy", ValueSchema.ofSeries(ValueSchema.ofStruct(Map.of(
105+
"key", ValueSchema.STRING,
106+
"value", ValueSchema.ofSeries(ValueSchema.BOOLEAN)))),
107+
"record", ValueSchema.withMeta(
108+
"item_order",
109+
SerializedValue.of(List.of(SerializedValue.of("aString"), SerializedValue.of("fancy"))),
110+
ValueSchema.ofStruct(Map.of(
111+
"aString", ValueSchema.STRING,
112+
"fancy", ValueSchema.ofSeries(ValueSchema.ofStruct(Map.of(
113+
"key", ValueSchema.STRING,
114+
"value", ValueSchema.ofSeries(ValueSchema.BOOLEAN)))))))))),
115+
valueSchema);
61116
}
62117

63118
@Test

e2e-tests/src/test/java/gov/nasa/jpl/aerie/e2e/MissionModelTests.java

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import gov.nasa.jpl.aerie.e2e.types.ValueSchema;
88
import gov.nasa.jpl.aerie.e2e.utils.GatewayRequests;
99
import gov.nasa.jpl.aerie.e2e.utils.HasuraRequests;
10+
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue;
1011
import org.junit.jupiter.api.AfterAll;
1112
import org.junit.jupiter.api.BeforeAll;
1213
import org.junit.jupiter.api.Test;
1314
import org.junit.jupiter.api.TestInstance;
1415

1516
import javax.json.Json;
17+
import javax.json.JsonObject;
1618
import java.io.IOException;
1719
import java.util.ArrayList;
20+
import java.util.Arrays;
1821
import java.util.List;
1922
import java.util.Map;
2023

@@ -97,7 +100,7 @@ private ArrayList<ActivityType> expectedActivityTypesBanananation() {
97100
activityTypes.add(new ActivityType(
98101
"BiteBanana",
99102
Map.of("biteSize", new Parameter(0, new ValueSchemaMeta(Map.of("unit", Json.createObjectBuilder(Map.of("value", "m")).build(), "banannotation", Json.createObjectBuilder().add("value", Json.createValue("Specifies the size of bite to take")).build()), VALUE_SCHEMA_REAL))),
100-
new ValueSchemaStruct(Map.of("biteSizeWasBig", VALUE_SCHEMA_BOOLEAN, "newFlag", new ValueSchemaVariant(List.of(new Variant("A", "A"), new Variant("B", "B"))))),
103+
new ValueSchemaMeta(Map.of("item_order", Json.createArrayBuilder().add("biteSizeWasBig").add("newFlag").build()), new ValueSchemaStruct(Map.of("biteSizeWasBig", VALUE_SCHEMA_BOOLEAN, "newFlag", new ValueSchemaVariant(List.of(new Variant("A", "A"), new Variant("B", "B")))))),
101104
"Eat"
102105
));
103106
activityTypes.add(new ActivityType("ChangeProducer", Map.of("producer", new Parameter(0, VALUE_SCHEMA_STRING))));
@@ -115,17 +118,81 @@ private ArrayList<ActivityType> expectedActivityTypesBanananation() {
115118
activityTypes.add(new ActivityType(
116119
"DurationParameterActivity",
117120
Map.of("duration", new Parameter(0, VALUE_SCHEMA_DURATION)),
118-
new ValueSchemaStruct(Map.of(
119-
"duration", VALUE_SCHEMA_DURATION,
120-
"durationInSeconds", new ValueSchemaMeta(Map.of("unit", Json.createObjectBuilder(Map.of("value", "s")).build()), VALUE_SCHEMA_REAL))),
121-
null
122-
));
121+
new ValueSchemaMeta(
122+
Map.of("item_order", Json.createArrayBuilder().add("duration").add("durationInSeconds").build()),
123+
new ValueSchemaStruct(Map.of("duration", VALUE_SCHEMA_DURATION,
124+
"durationInSeconds", new ValueSchemaMeta(Map.of("unit", Json.createObjectBuilder(Map.of("value", "s")).build()), VALUE_SCHEMA_REAL)))),
125+
null));
123126
activityTypes.add(new ActivityType("ExceptionActivity", Map.of("throwException", new Parameter(0, VALUE_SCHEMA_BOOLEAN))));
124127
activityTypes.add(new ActivityType("grandchild", Map.of("counter", new Parameter(0, VALUE_SCHEMA_INT))));
125128
activityTypes.add(new ActivityType("GrowBanana", Map.of(
126129
"quantity", new Parameter(0, VALUE_SCHEMA_INT),
127130
"growingDuration", new Parameter(1, VALUE_SCHEMA_DURATION))));
128131
activityTypes.add(new ActivityType("LineCount", Map.of("path", new Parameter(0, VALUE_SCHEMA_PATH))));
132+
final var allSubs = new String[] {"primitiveDouble",
133+
"primitiveFloat",
134+
"primitiveByte",
135+
"primitiveShort",
136+
"primitiveInt",
137+
"primitiveLong",
138+
"primitiveChar",
139+
"primitiveBoolean",
140+
"boxedDouble",
141+
"boxedFloat",
142+
"boxedByte",
143+
"boxedShort",
144+
"boxedInt",
145+
"boxedLong",
146+
"boxedChar",
147+
"boxedBoolean",
148+
"string",
149+
"doubleArray",
150+
"floatArray",
151+
"byteArray",
152+
"shortArray",
153+
"intArray",
154+
"longArray",
155+
"charArray",
156+
"booleanArray",
157+
"stringArray",
158+
"primDoubleArray",
159+
"primFloatArray",
160+
"primByteArray",
161+
"primShortArray",
162+
"primIntArray",
163+
"primLongArray",
164+
"primCharArray",
165+
"primBooleanArray",
166+
"doubleList",
167+
"floatList",
168+
"byteList",
169+
"shortList",
170+
"intList",
171+
"longList",
172+
"charList",
173+
"booleanList",
174+
"stringList",
175+
"doubleMap",
176+
"floatMap",
177+
"byteMap",
178+
"shortMap",
179+
"intMap",
180+
"longMap",
181+
"charMap",
182+
"booleanMap",
183+
"stringMap",
184+
"testDuration",
185+
"testEnum",
186+
"mappyBoi",
187+
"doublePrimIntArray",
188+
"intListArrayArray",
189+
"obnoxious",
190+
"nested",
191+
"genericParameter"};
192+
final var parameterTestItemOrder = Json.createArrayBuilder();
193+
for(final var param: allSubs){
194+
parameterTestItemOrder.add(param);
195+
}
129196
activityTypes.add(new ActivityType(
130197
"ParameterTest",
131198
//region ParameterTest Parameters
@@ -135,15 +202,17 @@ private ArrayList<ActivityType> expectedActivityTypesBanananation() {
135202
entry("record",
136203
new Parameter(
137204
58,
138-
new ValueSchemaStruct(Map.<String, ValueSchema>ofEntries(
205+
new ValueSchemaMeta(Map.of("item_order", parameterTestItemOrder.build()), new ValueSchemaStruct(Map.<String, ValueSchema>ofEntries(
139206
entry("intMap", new ValueSchemaSeries(new ValueSchemaStruct(Map.of(
140207
"key", VALUE_SCHEMA_INT,
141208
"value", VALUE_SCHEMA_INT)))),
142-
entry("nested", new ValueSchemaStruct(Map.of(
209+
entry("nested", new ValueSchemaMeta(
210+
Map.of( "item_order", Json.createArrayBuilder().add("a").add("b").build()),
211+
new ValueSchemaStruct(Map.of(
143212
"a", VALUE_SCHEMA_STRING,
144213
"b", new ValueSchemaSeries(new ValueSchemaStruct(Map.of(
145214
"key", VALUE_SCHEMA_INT,
146-
"value", VALUE_SCHEMA_STRING)))))),
215+
"value", VALUE_SCHEMA_STRING))))))),
147216
entry("string", VALUE_SCHEMA_STRING),
148217
entry("byteMap", new ValueSchemaSeries(new ValueSchemaStruct(Map.of(
149218
"key", VALUE_SCHEMA_INT,
@@ -226,7 +295,7 @@ private ArrayList<ActivityType> expectedActivityTypesBanananation() {
226295
entry("primBooleanArray", new ValueSchemaSeries(VALUE_SCHEMA_BOOLEAN)),
227296
entry("primitiveBoolean", VALUE_SCHEMA_BOOLEAN),
228297
entry("intListArrayArray", new ValueSchemaSeries(new ValueSchemaSeries(new ValueSchemaSeries(VALUE_SCHEMA_INT)))),
229-
entry("doublePrimIntArray", new ValueSchemaSeries(new ValueSchemaSeries(VALUE_SCHEMA_INT))))))),
298+
entry("doublePrimIntArray", new ValueSchemaSeries(new ValueSchemaSeries(VALUE_SCHEMA_INT)))))))),
230299
//endregion record
231300
entry("string", new Parameter(16, VALUE_SCHEMA_STRING)),
232301
entry("byteMap", new Parameter(45,new ValueSchemaSeries(new ValueSchemaStruct(Map.of(

0 commit comments

Comments
 (0)