use of com.linkedin.data.schema.UnionDataSchema in project rest.li by linkedin.
the class CompatibilityChecker method checkUnion.
private void checkUnion(UnionDataSchema older, UnionDataSchema newer) {
// using list to preserve union member order
List<DataSchema> commonMembers = new CheckerArrayList<DataSchema>(newer.getTypes().size());
List<String> newerAdded = new CheckerArrayList<String>();
List<String> olderAdded = new CheckerArrayList<String>();
computeAddedUnionMembers(older, newer, newerAdded, commonMembers);
computeAddedUnionMembers(newer, older, olderAdded, null);
if (newerAdded.isEmpty() == false) {
appendMessage(CompatibilityMessage.Impact.BREAKS_OLD_READER, "new union added members %s", newerAdded);
}
if (olderAdded.isEmpty() == false) {
appendMessage(CompatibilityMessage.Impact.BREAKS_NEW_READER, "new union removed members %s", olderAdded);
}
for (DataSchema newerSchema : commonMembers) {
String memberKey = newerSchema.getUnionMemberKey();
DataSchema olderSchema = older.getType(memberKey);
assert (olderSchema != null);
check(olderSchema, newerSchema);
}
}
use of com.linkedin.data.schema.UnionDataSchema in project rest.li by linkedin.
the class SchemaSampleDataGenerator method buildData.
private static Object buildData(ParentSchemas parentSchemas, DataSchema schema, String fieldName, DataGenerationOptions spec) {
spec = preventRecursionIntoAlreadyTraversedSchemas(parentSchemas, spec, schema);
parentSchemas.incrementReferences(schema);
final DataSchema derefSchema = schema.getDereferencedDataSchema();
final SampleDataCallback callback = spec.getCallback();
Object data = null;
switch(derefSchema.getType()) {
case BOOLEAN:
data = callback.getBoolean(fieldName);
break;
case INT:
data = callback.getInteger(fieldName);
break;
case LONG:
data = callback.getLong(fieldName);
break;
case FLOAT:
data = callback.getFloat(fieldName);
break;
case DOUBLE:
data = callback.getDouble(fieldName);
break;
case BYTES:
data = callback.getBytes(fieldName);
break;
case STRING:
data = callback.getString(fieldName);
break;
case NULL:
data = Data.NULL;
break;
case FIXED:
data = callback.getFixed(fieldName, (FixedDataSchema) derefSchema);
break;
case ENUM:
data = callback.getEnum(fieldName, (EnumDataSchema) derefSchema);
break;
case ARRAY:
final DataList dataList = new DataList(spec.getArraySize());
for (int i = 0; i < spec.getArraySize(); i++) {
final Object item = buildData(parentSchemas, ((ArrayDataSchema) derefSchema).getItems(), fieldName, spec);
dataList.add(item);
}
data = dataList;
break;
case RECORD:
data = buildRecordData(parentSchemas, (RecordDataSchema) derefSchema, spec);
break;
case MAP:
final DataMap dataMap = new DataMap();
for (int i = 0; i < spec.getArraySize(); i++) {
final Object item = buildData(parentSchemas, ((MapDataSchema) derefSchema).getValues(), fieldName, spec);
dataMap.put("mapField_" + _random.nextInt(), item);
}
data = dataMap;
break;
case UNION:
final UnionDataSchema unionSchema = (UnionDataSchema) derefSchema;
final List<DataSchema> types = removeAlreadyTraversedSchemasFromUnionMemberList(parentSchemas, unionSchema.getTypes());
final int unionIndex = _random.nextInt(types.size());
final DataSchema unionItemSchema = types.get(unionIndex);
data = buildData(parentSchemas, unionItemSchema, fieldName, spec);
if (data != null) {
final DataMap unionMap = new DataMap();
unionMap.put(unionItemSchema.getUnionMemberKey(), data);
data = unionMap;
}
break;
case TYPEREF:
data = buildData(parentSchemas, derefSchema, fieldName, spec);
break;
}
parentSchemas.decrementReferences(schema);
return data;
}
use of com.linkedin.data.schema.UnionDataSchema in project rest.li by linkedin.
the class PdlSchemaParser method parseUnion.
private UnionDataSchema parseUnion(UnionDeclarationContext union, boolean withinTypref) throws ParseException {
UnionDataSchema schema = new UnionDataSchema();
List<UnionMemberDeclarationContext> members = union.typeParams.members;
List<DataSchema> types = new ArrayList<>(members.size());
Set<DataSchema> typesDeclaredInline = new HashSet<>();
for (UnionMemberDeclarationContext memberDecl : members) {
TypeAssignmentContext memberType = memberDecl.member;
DataSchema dataSchema = toDataSchema(memberType);
if (dataSchema != null) {
types.add(dataSchema);
if (isDeclaredInline(memberDecl.member)) {
typesDeclaredInline.add(dataSchema);
}
}
}
schema.setTypes(types, errorMessageBuilder());
schema.setTypesDeclaredInline(typesDeclaredInline);
return schema;
}
use of com.linkedin.data.schema.UnionDataSchema in project rest.li by linkedin.
the class TestTemplateSpecGenerator method testCustomInfoForUnionMembers.
@Test(dataProvider = "customTypeDataForUnion")
public void testCustomInfoForUnionMembers(final List<DataSchema> customTypedSchemas) {
final UnionDataSchema union = new UnionDataSchema();
union.setTypes(customTypedSchemas, null);
final TyperefDataSchema typeref = new TyperefDataSchema(new Name(INPUT_SCHEMA_NAME));
typeref.setReferencedType(union);
final TemplateSpecGenerator generator = new TemplateSpecGenerator(_resolver);
final UnionTemplateSpec spec = (UnionTemplateSpec) generator.generate(typeref, _location);
for (int i = 0; i < customTypedSchemas.size(); ++i) {
Assert.assertNotNull(spec.getMembers().get(i).getCustomInfo());
Assert.assertEquals(spec.getMembers().get(i).getCustomInfo().getCustomClass().getClassName(), CustomTypeUtil.getJavaCustomTypeClassNameFromSchema((TyperefDataSchema) customTypedSchemas.get(i)));
}
}
Aggregations