use of com.linkedin.data.schema.UnionDataSchema in project rest.li by linkedin.
the class ExampleRequestResponseGenerator method generateFieldDefValue.
private Object generateFieldDefValue(FieldDef<?> fieldDef) {
Object value = _dataGenerator.buildData(fieldDef.getName(), fieldDef.getDataSchema());
DataSchema dereferencedDataSchema = fieldDef.getDataSchema().getDereferencedDataSchema();
if (!dereferencedDataSchema.isPrimitive()) {
switch(dereferencedDataSchema.getType()) {
case FIXED:
value = new FixedTemplatePlaceholder(value, (FixedDataSchema) dereferencedDataSchema);
break;
case ENUM:
// just use the string value already generated. Will be coerced by DataTemplateUtil.DynamicEnumCoercer.
break;
case ARRAY:
value = new ArrayTemplatePlaceholder<Object>((DataList) value, (ArrayDataSchema) dereferencedDataSchema, Object.class);
break;
case RECORD:
value = new RecordTemplatePlaceholder((DataMap) value, (RecordDataSchema) dereferencedDataSchema);
break;
case MAP:
value = new MapTemplatePlaceholder<Object>((DataMap) value, (MapDataSchema) dereferencedDataSchema, Object.class);
break;
case UNION:
value = new UnionTemplatePlaceholder(value, (UnionDataSchema) dereferencedDataSchema);
break;
case TYPEREF:
throw new IllegalStateException("TYPEREF should not be returned for a dereferenced byte. schema: " + fieldDef.getDataSchema());
default:
throw new IllegalStateException("Unrecognized enum value: " + dereferencedDataSchema.getType());
}
}
return value;
}
use of com.linkedin.data.schema.UnionDataSchema in project rest.li by linkedin.
the class SnapshotGenerator method recordType.
private void recordType(DataSchema schema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
if (schema instanceof NamedDataSchema) {
NamedDataSchema namedDataSchema = (NamedDataSchema) schema;
if (!foundTypes.containsKey(namedDataSchema.getFullName())) {
foundTypes.put(namedDataSchema.getFullName(), namedDataSchema);
if (// recurse into record, record any contained types.
schema instanceof RecordDataSchema) {
RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
for (NamedDataSchema includedSchema : recordDataSchema.getInclude()) {
recordType(includedSchema, foundTypes, typeOrder);
}
for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
recordType(field.getType(), foundTypes, typeOrder);
}
} else if (schema instanceof TyperefDataSchema) {
recordType(schema.getDereferencedDataSchema(), foundTypes, typeOrder);
}
typeOrder.add(namedDataSchema);
}
} else if (schema instanceof ArrayDataSchema) {
ArrayDataSchema arraySchema = (ArrayDataSchema) schema;
recordType(arraySchema.getItems(), foundTypes, typeOrder);
} else if (schema instanceof MapDataSchema) {
MapDataSchema mapSchema = (MapDataSchema) schema;
recordType(mapSchema.getValues(), foundTypes, typeOrder);
} else if (schema instanceof UnionDataSchema) {
UnionDataSchema unionSchema = (UnionDataSchema) schema;
for (DataSchema type : unionSchema.getTypes()) {
recordType(type, foundTypes, typeOrder);
}
}
}
use of com.linkedin.data.schema.UnionDataSchema in project rest.li by linkedin.
the class ResourceModelEncoder method buildDataSchemaType.
private static String buildDataSchemaType(final Class<?> type, final DataSchema dataSchema) {
final DataSchema schemaToEncode;
if (dataSchema instanceof TyperefDataSchema) {
return ((TyperefDataSchema) dataSchema).getFullName();
} else if (dataSchema instanceof PrimitiveDataSchema || dataSchema instanceof NamedDataSchema) {
return dataSchema.getUnionMemberKey();
} else if (dataSchema instanceof UnionDataSchema && HasTyperefInfo.class.isAssignableFrom(type)) {
final TyperefInfo unionRef = DataTemplateUtil.getTyperefInfo(type.asSubclass(DataTemplate.class));
schemaToEncode = unionRef.getSchema();
} else {
schemaToEncode = dataSchema;
}
JsonBuilder builder = null;
try {
builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
encoder.encode(schemaToEncode);
return builder.result();
} catch (IOException e) {
throw new RestLiInternalException("could not encode schema for '" + type.getName() + "'", e);
} finally {
if (builder != null) {
builder.closeQuietly();
}
}
}
use of com.linkedin.data.schema.UnionDataSchema in project rest.li by linkedin.
the class CompatibilityChecker method check.
private void check(DataSchema older, DataSchema newer) {
Checked toCheck = new Checked(older, newer);
if (_checked.contains(toCheck)) {
return;
}
_checked.add(toCheck);
if (older == newer) {
return;
}
int pathCount = 1;
if (_options.getMode() == CompatibilityOptions.Mode.DATA) {
older = older.getDereferencedDataSchema();
while (newer.getType() == DataSchema.Type.TYPEREF) {
TyperefDataSchema typerefDataSchema = ((TyperefDataSchema) newer);
_path.addLast(typerefDataSchema.getFullName());
_path.addLast(DataSchemaConstants.REF_KEY);
pathCount++;
newer = typerefDataSchema.getRef();
}
}
if (newer.getType() == DataSchema.Type.TYPEREF) {
_path.addLast(((TyperefDataSchema) newer).getFullName());
} else {
_path.addLast(newer.getUnionMemberKey());
}
switch(newer.getType()) {
case TYPEREF:
if (isSameType(older, newer))
checkTyperef((TyperefDataSchema) older, (TyperefDataSchema) newer);
break;
case RECORD:
if (isSameType(older, newer))
checkRecord((RecordDataSchema) older, (RecordDataSchema) newer);
break;
case ARRAY:
if (isSameType(older, newer))
checkArray((ArrayDataSchema) older, (ArrayDataSchema) newer);
break;
case MAP:
if (isSameType(older, newer))
checkMap((MapDataSchema) older, (MapDataSchema) newer);
break;
case ENUM:
if (isSameType(older, newer))
checkEnum((EnumDataSchema) older, (EnumDataSchema) newer);
break;
case FIXED:
if (isSameType(older, newer))
checkFixed((FixedDataSchema) older, (FixedDataSchema) newer);
break;
case UNION:
if (isSameType(older, newer))
checkUnion((UnionDataSchema) older, (UnionDataSchema) newer);
break;
default:
if (newer instanceof PrimitiveDataSchema)
checkPrimitive(older, newer);
else
throw new IllegalStateException("Unknown schema type " + newer.getType() + ", checking old schema " + older + ", new schema " + newer);
break;
}
for (; pathCount > 0; pathCount--) {
_path.removeLast();
}
return;
}
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);
}
}
Aggregations