/** Set the Java type to be used when reading this schema. Meaningful only * only string schemas and map schemas (for the keys). */ public static void setStringType(Schema s, StringType stringType) { // Utf8 is the default and implements CharSequence, so we only need to add // a property when the type is String if (stringType == StringType.String) s.addProp(GenericData.STRING_PROP, GenericData.STRING_TYPE_STRING); }
private void setElement(Schema schema, Type element) { if (!(element instanceof Class)) return; Class<?> c = (Class<?>)element; Union union = c.getAnnotation(Union.class); if (union != null) // element is annotated union schema.addProp(ELEMENT_PROP, c.getName()); }
@Override public Schema schema() { this.schema = Schema.createEnum(this.enumName, "", "", this.enumSet); this.schema.addProp("source.type", "enum"); return this.schema; } }
@Override protected void customizeFieldSchema(Schema fieldSchema, GenericRecord fieldType) { if (eventClassTypes.contains(fieldType.getSchema().getName())) { GenericData.EnumSymbol classType = (GenericData.EnumSymbol) fieldType.get(CLASS_TYPE); if (classType != null) { fieldSchema.addProp(CLASS_TYPE, classType.toString().toLowerCase()); } else { fieldSchema.addProp(CLASS_TYPE, OBJECT.toLowerCase()); } } }
@Override public Schema schema() { Schema schema = _schema; schema.addProp(SOURCE_TYPE, RECORD.toString().toLowerCase()); return buildUnionIfNullable(schema); } }
@Override public Schema schema() { this.schema = Schema.createEnum(this.enumName, "", namespace, this.enumSet); this.schema.addProp(SOURCE_TYPE, ENUM.toString().toLowerCase()); return buildUnionIfNullable(this.schema); } }
private Schema arraySchema() { Schema schema = Schema.createArray(getElementConverter().schema()); schema.addProp(SOURCE_TYPE, ARRAY.toString().toLowerCase()); return schema; }
@Override public Schema schema() { Schema schema = Schema.createArray(getElementConverter().schema()); schema.addProp("source.type", "array"); return schema; } }
@Override public Schema schema() { Schema schema = Schema.createMap(getElementConverter().schema()); schema.addProp("source.type", "map"); return schema; } }
@Override public Schema addToSchema(Schema schema) { super.addToSchema(schema); schema.addProp(REF_FIELD_NAME, refFieldName); return schema; }
@Test public void testCopy() throws CopyNotSupportedException { Schema schema = new Schema.Parser().parse(AVRO_SCHEMA); CopyableSchema copyableSchema = new CopyableSchema(schema); Schema copy = copyableSchema.copy(); Assert.assertEquals(schema, copy); copy.addProp("foo", "bar"); Assert.assertNotEquals(schema, copy); } }
protected Schema schema() { Schema schema = Schema.create(getTargetType()); schema.addProp("source.type", this.jsonSchema.getType().toString().toLowerCase()); return buildUnionIfNullable(schema); }
@Override public Schema schema() { Schema schema = Schema.createMap(getElementConverter().schema()); schema.addProp(SOURCE_TYPE, MAP.toString().toLowerCase()); return buildUnionIfNullable(schema); } }
@Test public void testInvalidLogicalTypeIgnored() { final Schema schema = Schema.createFixed("aFixed", null, null, 2); schema.addProp("logicalType", "decimal"); schema.addProp("precision", 9); schema.addProp("scale", 2); Assert.assertNull("Should ignore invalid logical type", LogicalTypes.fromSchemaIgnoreInvalid(schema)); }
@Test(expected=AvroRuntimeException.class) public void testImmutability2() { Schema s = enumSchema(); s.addProp("p1", (String)null); }
@Test public void testEnum() { List<String> symbols = Arrays.asList("a", "b"); Schema expected = Schema.createEnum("myenum", null, null, symbols); expected.addProp("p", "v"); Schema schema = SchemaBuilder.enumeration("myenum") .prop("p", "v").symbols("a", "b"); Assert.assertEquals(expected, schema); }
@Test public void testEnumWithDefault() { List<String> symbols = Arrays.asList("a", "b"); String enumDefault = "a"; Schema expected = Schema.createEnum("myenum", null, null, symbols, enumDefault); expected.addProp("p", "v"); Schema schema = SchemaBuilder.enumeration("myenum") .prop("p", "v").defaultSymbol(enumDefault).symbols("a", "b"); Assert.assertEquals(expected, schema); }
private static void checkProp(Schema s0) throws Exception { if(s0.getType().equals(Schema.Type.UNION)) return; // unions have no props assertNull(s0.getProp("foo")); Schema s1 = Schema.parse(s0.toString()); s1.addProp("foo", "bar"); assertEquals("bar", s1.getProp("foo")); assertNotEquals(s0, s1); Schema s2 = Schema.parse(s1.toString()); assertEquals("bar", s2.getProp("foo")); assertEquals(s1, s2); assertNotEquals(s0, s2); }
private Schema primitive(Schema.Type type, Schema bare) { // test creation of bare schema by name Schema bareByName = SchemaBuilder.builder().type(type.getName()); Assert.assertEquals(Schema.create(type), bareByName); Assert.assertEquals(bareByName, bare); // return a schema with custom prop set Schema p = Schema.create(type); p.addProp("p", "v"); return p; }