Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nullable structs + test. #439

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
public class StructToJsonMap {

public static Map<String, Object> toJsonMap(Struct struct) {
if (struct == null) {
return null;
}
Map<String, Object> jsonMap = new HashMap<String, Object>(0);
List<Field> fields = struct.schema().fields();
for (Field field : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,60 @@
import static org.junit.Assert.assertNull;

public class StructToJsonMapTest {

@Test
public void emptyStructToEmptyMap() {
Schema schema = SchemaBuilder.struct()
.build();
.build();
Struct struct = new Struct(schema);
assertEquals(ImmutableMap.of(), StructToJsonMap.toJsonMap(struct));
}

@Test
public void complextStructToMap() {
public void complexStructToMap() {
Schema embeddedSchema = SchemaBuilder.struct()
.field("embedded_string", Schema.STRING_SCHEMA)
.build();
.field("embedded_string", Schema.STRING_SCHEMA)
.build();
Schema schema = SchemaBuilder.struct()
.field("boolean", Schema.BOOLEAN_SCHEMA)
.field("int8", Schema.INT8_SCHEMA)
.field("int16", Schema.INT16_SCHEMA)
.field("int32", Schema.INT32_SCHEMA)
.field("int64", Schema.INT64_SCHEMA)
.field("float32", Schema.FLOAT32_SCHEMA)
.field("float64", Schema.FLOAT64_SCHEMA)
.field("date", Date.SCHEMA)
.field("time", Time.SCHEMA)
.field("timestamp", Timestamp.SCHEMA)
.field("decimal", Decimal.schema(10))
.field("string", Schema.STRING_SCHEMA)
.field("struct", embeddedSchema)
.field("array_of_boolean", SchemaBuilder.array(Schema.BOOLEAN_SCHEMA).build())
.field("array_of_struct", SchemaBuilder.array(embeddedSchema).build())
.field("optional_string", Schema.OPTIONAL_STRING_SCHEMA)
.build();
.field("boolean", Schema.BOOLEAN_SCHEMA)
.field("int8", Schema.INT8_SCHEMA)
.field("int16", Schema.INT16_SCHEMA)
.field("int32", Schema.INT32_SCHEMA)
.field("int64", Schema.INT64_SCHEMA)
.field("float32", Schema.FLOAT32_SCHEMA)
.field("float64", Schema.FLOAT64_SCHEMA)
.field("date", Date.SCHEMA)
.field("time", Time.SCHEMA)
.field("timestamp", Timestamp.SCHEMA)
.field("decimal", Decimal.schema(10))
.field("string", Schema.STRING_SCHEMA)
.field("struct", embeddedSchema)
.field("array_of_boolean", SchemaBuilder.array(Schema.BOOLEAN_SCHEMA).build())
.field("array_of_struct", SchemaBuilder.array(embeddedSchema).build())
.field("optional_string", Schema.OPTIONAL_STRING_SCHEMA)
.build();

java.util.Date now = new java.util.Date();
BigDecimal ten = new BigDecimal(10).setScale(10);
String quickBrownFox = "The quick brown fox jumps over the lazy dog";
Struct struct = new Struct(schema)
.put("boolean", false)
.put("int8", (byte) 0)
.put("int16", (short) 1)
.put("int32", 2)
.put("int64", (long) 3)
.put("float32", (float) 4.0)
.put("float64", (double) 5.0)
.put("date", now)
.put("time", now)
.put("timestamp", now)
.put("decimal", ten)
.put("string", quickBrownFox)
.put("struct", new Struct(embeddedSchema)
.put("embedded_string", quickBrownFox))
.put("array_of_boolean", ImmutableList.of(false))
.put("array_of_struct", ImmutableList.of(
new Struct(embeddedSchema).put("embedded_string", quickBrownFox)));
.put("boolean", false)
.put("int8", (byte) 0)
.put("int16", (short) 1)
.put("int32", 2)
.put("int64", (long) 3)
.put("float32", (float) 4.0)
.put("float64", (double) 5.0)
.put("date", now)
.put("time", now)
.put("timestamp", now)
.put("decimal", ten)
.put("string", quickBrownFox)
.put("struct", new Struct(embeddedSchema)
.put("embedded_string", quickBrownFox))
.put("array_of_boolean", ImmutableList.of(false))
.put("array_of_struct", ImmutableList.of(
new Struct(embeddedSchema).put("embedded_string", quickBrownFox)));

Map<String, Object> converted = StructToJsonMap.toJsonMap(struct);
assertEquals(false, converted.get("boolean"));
Expand All @@ -91,4 +92,26 @@ public void complextStructToMap() {
assertEquals(ImmutableMap.of("embedded_string", quickBrownFox), ((List<Struct>) converted.get("array_of_struct")).get(0));
assertNull(converted.get("optional_string"));
}

@Test
public void nullableStructTest() {
Schema embeddedSchema = SchemaBuilder.struct()
.optional()
.field("embedded_string", Schema.STRING_SCHEMA)
.field("embedded_int", Schema.INT32_SCHEMA)
.build();
Schema schema = SchemaBuilder.struct()
.field("struct", embeddedSchema)
.field("string", Schema.STRING_SCHEMA)
.build();

Struct struct = new Struct(schema)
.put("struct", null)
.put("string", "string_value");

Map<String, Object> converted = StructToJsonMap.toJsonMap(struct);
assertEquals("string_value", converted.get("string"));
assertNull(converted.get("struct"));
}

}