Skip to content

Commit 1c4f393

Browse files
flat-record: objectMapper.readFlat can parse an enum that had custom properties set on it (#630)
1 parent 9d73930 commit 1c4f393

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

hollow/src/main/java/com/netflix/hollow/core/write/objectmapper/HollowObjectMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ public <T> T readFlat(FlatRecord record) {
9494
recordReader.skipSchema(schema);
9595
} else {
9696
Object obj = mapper.parseFlatRecord(schema, recordReader, parsedObjects);
97-
parsedObjects.put(ordinal++, obj);
97+
parsedObjects.put(ordinal, obj);
9898
}
99+
ordinal++;
99100
}
100101

101102
return (T) parsedObjects.get(ordinal - 1);

hollow/src/main/java/com/netflix/hollow/core/write/objectmapper/HollowObjectTypeMapper.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected Object parseFlatRecord(HollowSchema recordSchema, FlatRecordReader rea
195195
HollowObjectSchema recordObjectSchema = (HollowObjectSchema) recordSchema;
196196

197197
Object obj = null;
198-
if (BOXED_WRAPPERS.contains(clazz) || clazz.isEnum()) {
198+
if (BOXED_WRAPPERS.contains(clazz)) {
199199
// if `clazz` is a BoxedWrapper then by definition its OBJECT schema will have a single primitive
200200
// field so find it in the FlatRecord and ignore all other fields.
201201
for (int i = 0; i < recordObjectSchema.numFields(); i++) {
@@ -206,7 +206,20 @@ protected Object parseFlatRecord(HollowSchema recordSchema, FlatRecordReader rea
206206
reader.skipField(recordObjectSchema.getFieldType(i));
207207
}
208208
}
209-
} else {
209+
} else if (clazz.isEnum()) {
210+
// if `clazz` is an enum, then we should expect to find a field called `_name` in the FlatRecord.
211+
// There may be other fields if the producer enum contained custom properties, we ignore them
212+
// here assuming the enum constructor will set them if needed.
213+
for (int i = 0; i < recordObjectSchema.numFields(); i++) {
214+
String fieldName = recordObjectSchema.getFieldName(i);
215+
int posInPojoSchema = schema.getPosition(fieldName);
216+
if (fieldName.equals(MappedFieldType.ENUM_NAME.getSpecialFieldName()) && posInPojoSchema != -1) {
217+
obj = mappedFields.get(posInPojoSchema).parseBoxedWrapper(reader);
218+
} else {
219+
reader.skipField(recordObjectSchema.getFieldType(i));
220+
}
221+
}
222+
} else {
210223
obj = unsafe.allocateInstance(clazz);
211224
for (int i = 0; i < recordObjectSchema.numFields(); i++) {
212225
int posInPojoSchema = schema.getPosition(recordObjectSchema.getFieldName(i));

hollow/src/test/java/com/netflix/hollow/core/write/objectmapper/HollowObjectMapperFlatRecordParserTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public void testSpecialWrapperTypes() {
3838
SpecialWrapperTypesTest wrapperTypesTest = new SpecialWrapperTypesTest();
3939
wrapperTypesTest.id = 8797182L;
4040
wrapperTypesTest.type = AnEnum.SOME_VALUE_C;
41+
wrapperTypesTest.complexEnum = ComplexEnum.SOME_VALUE_A;
4142
wrapperTypesTest.dateCreated = new Date();
4243

4344
flatRecordWriter.reset();
@@ -47,6 +48,8 @@ public void testSpecialWrapperTypes() {
4748
SpecialWrapperTypesTest result = mapper.readFlat(fr);
4849

4950
Assert.assertEquals(wrapperTypesTest, result);
51+
Assert.assertEquals(wrapperTypesTest.complexEnum.value, result.complexEnum.value);
52+
Assert.assertEquals(wrapperTypesTest.complexEnum.anotherValue, result.complexEnum.anotherValue);
5053
}
5154

5255
@Test
@@ -558,19 +561,35 @@ enum AnEnum {
558561
SOME_VALUE_C,
559562
}
560563

564+
enum ComplexEnum {
565+
SOME_VALUE_A("A", 1),
566+
SOME_VALUE_B("B", 2),
567+
SOME_VALUE_C("C", 3);
568+
569+
final String value;
570+
final int anotherValue;
571+
572+
ComplexEnum(String value, int anotherValue) {
573+
this.value = value;
574+
this.anotherValue = anotherValue;
575+
}
576+
}
577+
561578
@HollowTypeName(name = "SpecialWrapperTypesTest")
562579
@HollowPrimaryKey(fields = {"id"})
563580
static class SpecialWrapperTypesTest {
564581
long id;
565582
@HollowTypeName(name = "AnEnum")
566583
AnEnum type;
584+
@HollowTypeName(name = "ComplexEnum")
585+
ComplexEnum complexEnum;
567586
Date dateCreated;
568587

569588
@Override
570589
public boolean equals(Object o) {
571590
if(o instanceof SpecialWrapperTypesTest) {
572591
SpecialWrapperTypesTest other = (SpecialWrapperTypesTest)o;
573-
return id == other.id && type == other.type && dateCreated.equals(other.dateCreated);
592+
return id == other.id && complexEnum == other.complexEnum && type == other.type && dateCreated.equals(other.dateCreated);
574593
}
575594
return false;
576595
}
@@ -580,6 +599,7 @@ public String toString() {
580599
return "SpecialWrapperTypesTest{" +
581600
"id=" + id +
582601
", type='" + type + '\'' +
602+
", complexEnum='" + complexEnum + '\'' +
583603
", dateCreated=" + dateCreated +
584604
'}';
585605
}

0 commit comments

Comments
 (0)