@Test public void testFindNonNullUnionBranch() throws Exception { Schema withUnion = AvroCompatibilityHelper.parse(TestUtil.load("HasUnions.avsc")); Schema fieldSchema = AvroSchemaUtil.findNonNullUnionBranch(withUnion, "f1"); Assert.assertNotNull(fieldSchema); Assert.assertNotEquals(fieldSchema.getType(), Schema.Type.NULL); Schema withoutUnion = AvroCompatibilityHelper.parse(TestUtil.load("HasSymbolDocs.avsc")); fieldSchema = AvroSchemaUtil.findNonNullUnionBranch(withoutUnion, "enumField"); Assert.assertNotNull(fieldSchema); Assert.assertNotEquals(fieldSchema.getType(), Schema.Type.NULL); }
public static Schema findNonNullUnionBranch(Schema parent, String fieldName) { if (parent == null || fieldName == null || fieldName.isEmpty()) { throw new IllegalArgumentException("arguments must not be null/empty"); } Schema.Field field = parent.getField(fieldName); if (field == null) { return null; } Schema fieldSchema = field.schema(); Schema.Type fieldSchemaType = fieldSchema.getType(); if (!Schema.Type.UNION.equals(fieldSchemaType)) { return fieldSchema; } List<Schema> branches = fieldSchema.getTypes(); List<Schema> nonNullBranches = branches.stream(). filter(schema -> !Schema.Type.NULL.equals(schema.getType())).collect(Collectors.toList()); if (nonNullBranches.size() != 1) { throw new IllegalArgumentException(String.format("field %s has %d non-null union branches, where exactly 1 is expected in %s", fieldName, nonNullBranches.size(), parent)); } return nonNullBranches.get(0); }
AvroSchemaUtil { public static Schema findNonNullUnionBranch(Schema parent, String fieldName) { if (parent == null || fieldName == null || fieldName.isEmpty()) { throw new IllegalArgumentException("arguments must not be null/empty"); } Schema.Field field = parent.getField(fieldName); if (field == null) { return null; } Schema fieldSchema = field.schema(); Schema.Type fieldSchemaType = fieldSchema.getType(); if (!Schema.Type.UNION.equals(fieldSchemaType)) { return fieldSchema; } List<Schema> branches = fieldSchema.getTypes(); List<Schema> nonNullBranches = branches.stream(). filter(schema -> !Schema.Type.NULL.equals(schema.getType())).collect(Collectors.toList()); if (nonNullBranches.size() != 1) { throw new IllegalArgumentException(String.format("field %s has %d non-null union branches, where exactly 1 is expected in %s", fieldName, nonNullBranches.size(), parent)); } return nonNullBranches.get(0); } }
AvroSchemaUtil { public static Schema findNonNullUnionBranch(Schema parent, String fieldName) { if (parent == null || fieldName == null || fieldName.isEmpty()) { throw new IllegalArgumentException("arguments must not be null/empty"); } Schema.Field field = parent.getField(fieldName); if (field == null) { return null; } Schema fieldSchema = field.schema(); Schema.Type fieldSchemaType = fieldSchema.getType(); if (!Schema.Type.UNION.equals(fieldSchemaType)) { return fieldSchema; } List<Schema> branches = fieldSchema.getTypes(); List<Schema> nonNullBranches = branches.stream(). filter(schema -> !Schema.Type.NULL.equals(schema.getType())).collect(Collectors.toList()); if (nonNullBranches.size() != 1) { throw new IllegalArgumentException(String.format("field %s has %d non-null union branches, where exactly 1 is expected in %s", fieldName, nonNullBranches.size(), parent)); } return nonNullBranches.get(0); } private  AvroSchemaUtil(); }
AvroSchemaUtil { public static Schema findNonNullUnionBranch(Schema parent, String fieldName) { if (parent == null || fieldName == null || fieldName.isEmpty()) { throw new IllegalArgumentException("arguments must not be null/empty"); } Schema.Field field = parent.getField(fieldName); if (field == null) { return null; } Schema fieldSchema = field.schema(); Schema.Type fieldSchemaType = fieldSchema.getType(); if (!Schema.Type.UNION.equals(fieldSchemaType)) { return fieldSchema; } List<Schema> branches = fieldSchema.getTypes(); List<Schema> nonNullBranches = branches.stream(). filter(schema -> !Schema.Type.NULL.equals(schema.getType())).collect(Collectors.toList()); if (nonNullBranches.size() != 1) { throw new IllegalArgumentException(String.format("field %s has %d non-null union branches, where exactly 1 is expected in %s", fieldName, nonNullBranches.size(), parent)); } return nonNullBranches.get(0); } private  AvroSchemaUtil(); static void traverseSchema(Schema schema, SchemaVisitor visitor); static Schema findNonNullUnionBranch(Schema parent, String fieldName); }
AvroSchemaUtil { public static Schema findNonNullUnionBranch(Schema parent, String fieldName) { if (parent == null || fieldName == null || fieldName.isEmpty()) { throw new IllegalArgumentException("arguments must not be null/empty"); } Schema.Field field = parent.getField(fieldName); if (field == null) { return null; } Schema fieldSchema = field.schema(); Schema.Type fieldSchemaType = fieldSchema.getType(); if (!Schema.Type.UNION.equals(fieldSchemaType)) { return fieldSchema; } List<Schema> branches = fieldSchema.getTypes(); List<Schema> nonNullBranches = branches.stream(). filter(schema -> !Schema.Type.NULL.equals(schema.getType())).collect(Collectors.toList()); if (nonNullBranches.size() != 1) { throw new IllegalArgumentException(String.format("field %s has %d non-null union branches, where exactly 1 is expected in %s", fieldName, nonNullBranches.size(), parent)); } return nonNullBranches.get(0); } private  AvroSchemaUtil(); static void traverseSchema(Schema schema, SchemaVisitor visitor); static Schema findNonNullUnionBranch(Schema parent, String fieldName);  }