use of com.linkedin.data.schema.TyperefDataSchema 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));
return unionRef.getSchema().getFullName();
} 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.TyperefDataSchema in project rest.li by linkedin.
the class RestLiDataValidator method buildTyperefDataSchemaByProjection.
/**
* Build a new {@link TyperefDataSchema} schema that contains only the masked fields.
*/
private static TyperefDataSchema buildTyperefDataSchemaByProjection(TyperefDataSchema originalSchema, DataMap maskMap) {
TyperefDataSchema newSchema = new TyperefDataSchema(new Name(originalSchema.getFullName()));
if (originalSchema.getProperties() != null) {
newSchema.setProperties(originalSchema.getProperties());
}
if (originalSchema.getDoc() != null) {
newSchema.setDoc(originalSchema.getDoc());
}
if (originalSchema.getAliases() != null) {
newSchema.setAliases(originalSchema.getAliases());
}
DataSchema newRefSchema = buildSchemaByProjection(originalSchema.getRef(), maskMap);
newSchema.setReferencedType(newRefSchema);
return newSchema;
}
use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class KeyValueRecord method getPrimitiveKey.
@SuppressWarnings("unchecked")
public K getPrimitiveKey(TypeSpec<K> keyType) {
StringBuilder sb = new StringBuilder(10);
DataSchema keySchema = keyType.getSchema();
RecordDataSchema.Field keyField = new RecordDataSchema.Field(keySchema);
keyField.setName(KEY_FIELD_NAME, sb);
if (keySchema.isPrimitive() || keySchema.getType() == DataSchema.Type.ENUM) {
return obtainDirect(keyField, keyType.getType(), GetMode.DEFAULT);
} else if (keySchema.getType() == DataSchema.Type.TYPEREF) {
TyperefDataSchema typerefDataSchema = (TyperefDataSchema) keySchema;
Class<?> javaClass = CustomTypeUtil.getJavaCustomTypeClassFromSchema(typerefDataSchema);
if (javaClass == null) {
// typeref to a primitive. keyClass is a primitive
return obtainDirect(keyField, keyType.getType(), GetMode.DEFAULT);
} else {
// typeref to a custom type. javaClass is the custom type that the typeref refers to.
return (K) obtainDirect(keyField, javaClass, GetMode.DEFAULT);
}
} else {
throw new IllegalArgumentException("key is not a primitive, typeref, or an enum!");
}
}
use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class TestTemplateSpecGenerator method testCustomInfoForRecordFields.
@Test(dataProvider = "customTypeDataForRecord")
public void testCustomInfoForRecordFields(final List<DataSchema> customTypedSchemas) {
final List<RecordDataSchema.Field> fields = customTypedSchemas.stream().map(RecordDataSchema.Field::new).peek(field -> field.setName("field_" + _uniqueNumberGenerator.getAndIncrement(), null)).collect(Collectors.toList());
final RecordDataSchema record = new RecordDataSchema(new Name(INPUT_SCHEMA_NAME), RecordDataSchema.RecordType.RECORD);
record.setFields(fields, null);
final TemplateSpecGenerator generator = new TemplateSpecGenerator(_resolver);
final RecordTemplateSpec spec = (RecordTemplateSpec) generator.generate(record, _location);
for (int i = 0; i < customTypedSchemas.size(); ++i) {
Assert.assertNotNull(spec.getFields().get(i).getCustomInfo());
Assert.assertEquals(spec.getFields().get(i).getCustomInfo().getCustomClass().getClassName(), CustomTypeUtil.getJavaCustomTypeClassNameFromSchema((TyperefDataSchema) customTypedSchemas.get(i)));
}
}
use of com.linkedin.data.schema.TyperefDataSchema 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();
List<UnionDataSchema.Member> members = customTypedSchemas.stream().map(UnionDataSchema.Member::new).collect(Collectors.toCollection(ArrayList::new));
union.setMembers(members, 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