@Override protected Schema schema() { return Schema.createUnion(Arrays.asList(firstSchema, secondSchema)); } }
@Override protected FieldAssembler<R> complete(Schema schema) { // wrap the schema as a union of null and the schema Schema optional = Schema.createUnion(Arrays.asList(NULL_SCHEMA, schema)); return bldr.completeField(optional, (Object)null); } }
private Schema removeFieldsFromUnion(Schema schema, Map<String, Schema> schemaMap) { List<Schema> newUnion = Lists.newArrayList(); for (Schema unionType : schema.getTypes()) { newUnion.add(this.removeFields(unionType, schemaMap)); } return Schema.createUnion(newUnion); }
private static org.apache.avro.Schema unionWithNull(final org.apache.avro.Schema schema) { return createUnion(org.apache.avro.Schema.create(org.apache.avro.Schema.Type.NULL), schema); }
private Schema wrapInUnionWithNull(Schema schema) { Schema wrappedSchema = schema; switch (schema.getType()) { case NULL: break; case UNION: List<Schema> existingSchemas = removeDuplicateNullSchemas(schema.getTypes()); wrappedSchema = Schema.createUnion(existingSchemas); break; default: wrappedSchema = Schema.createUnion(Arrays.asList(Schema.create(Schema.Type.NULL), schema)); } return wrappedSchema; }
private Schema createOptional(Schema schema) { return Schema.createUnion(Arrays.asList( new Schema[] { schema, Schema.create(Type.NULL) })); }
private static Schema nullable(final Schema schema) { if (schema.getType() == Type.UNION) { final List<Schema> unionTypes = new ArrayList<>(schema.getTypes()); final Schema nullSchema = Schema.create(Type.NULL); if (unionTypes.contains(nullSchema)) { return schema; } unionTypes.add(nullSchema); return Schema.createUnion(unionTypes); } return Schema.createUnion(Schema.create(Type.NULL), schema); }
/** Complete this union **/ public R endUnion() { Schema schema = Schema.createUnion(schemas); return context.complete(schema); } }
@Override void toJson1(JsonGenerator gen) throws IOException { gen.writeFieldName("response"); response.toJson(types, gen); List<Schema> errs = errors.getTypes(); // elide system error if (errs.size() > 1) { Schema union = Schema.createUnion(errs.subList(1, errs.size())); gen.writeFieldName("errors"); union.toJson(types, gen); } }
private Schema createAvroUnion(TypeInfo typeInfo) { List<Schema> childSchemas = new ArrayList<Schema>(); for (TypeInfo childTypeInfo : ((UnionTypeInfo) typeInfo).getAllUnionObjectTypeInfos()) { final Schema childSchema = createAvroSchema(childTypeInfo); if (childSchema.getType() == Schema.Type.UNION) { childSchemas.addAll(childSchema.getTypes()); } else { childSchemas.add(childSchema); } } return Schema.createUnion(removeDuplicateNullSchemas(childSchemas)); }
private Schema getAnnotatedUnion(Union union, Map<String,Schema> names) { List<Schema> branches = new ArrayList<>(); for (Class branch : union.value()) branches.add(createSchema(branch, names)); return Schema.createUnion(branches); }
protected Schema buildUnionIfNullable(Schema schema) { if (this.isNullable()) { return Schema.createUnion(Arrays.asList(Schema.create(Schema.Type.NULL), schema)); } return schema; } }
/** * avro schema for the converted type * @return */ public Schema getSchema() { if (isNullable()) { List<Schema> list = new ArrayList<>(); list.add(Schema.create(Schema.Type.NULL)); list.add(schema()); return Schema.createUnion(list); } return schema(); }
private static void checkUnionError(Schema[] branches) { List<Schema> branchList = Arrays.asList(branches); try { Schema.createUnion(branchList); fail("Union should not have constructed from: " + branchList); } catch (AvroRuntimeException ignored) { } }
private static Schema createArraySchema() { List<Schema> schemas = new ArrayList<>(); for (int i = 0; i < 5; i++) { schemas.add(createInnerSchema("optional_field_" + i)); } Schema unionSchema = Schema.createUnion(schemas); return Schema.createArray(unionSchema); }
@Test public void testUnionWithIncompatibleElements() throws SchemaValidationException { Schema union1 = Schema.createUnion(Arrays.asList(rec)); Schema union2 = Schema.createUnion(Arrays.asList(rec4)); testValidatorFails(builder.canReadStrategy().validateAll(), union2, union1); }
private Schema nullableSchema(Class<?> type) { return Schema.createUnion(Arrays.asList( Schema.create(Schema.Type.NULL), ReflectData.get().getSchema(type))); } }
/** Reader union schema must contain all writer union branches. */ @Test public void testUnionReaderWriterSubsetIncompatibility() { final Schema unionWriter = Schema.createUnion(list(INT_SCHEMA, STRING_SCHEMA, LONG_SCHEMA)); final Schema unionReader = Schema.createUnion(list(INT_SCHEMA, STRING_SCHEMA)); final SchemaPairCompatibility result = checkReaderWriterCompatibility(unionReader, unionWriter); assertEquals(SchemaCompatibilityType.INCOMPATIBLE, result.getType()); }
@Test public void testIsNullableOnUnionWithNull() { Schema schema = Schema.createUnion(Schema.create(Type.NULL), Schema.create(Type.LONG)); assertTrue(schema.isNullable()); }
@Test public void testIsUnionOnUnionWithMultipleElements() { Schema schema = Schema.createUnion(Schema.create(Type.NULL), Schema.create(Type.LONG)); assertTrue(schema.isUnion()); }