use of com.linkedin.data.template.TyperefInfo in project rest.li by linkedin.
the class TestTyperefUnion method testTyperefUnion.
@Test
public void testTyperefUnion() {
TyperefInfo typerefInfo = DataTemplateUtil.getTyperefInfo(Union.class);
assertNotNull(typerefInfo);
TyperefDataSchema typerefDataSchema = typerefInfo.getSchema();
Union union = new Union();
assertTrue(union instanceof HasTyperefInfo);
TyperefInfo typerefInfoFromInstance = union.typerefInfo();
assertNotNull(typerefInfoFromInstance);
TyperefDataSchema typerefDataSchemaFromInstance = typerefInfo.getSchema();
assertSame(typerefDataSchemaFromInstance, typerefDataSchema);
assertSame(typerefInfoFromInstance, typerefInfo);
assertEquals(typerefDataSchema.getFullName(), Union.class.getName());
assertEquals(typerefDataSchema.getRef(), DataTemplateUtil.getSchema(Union.class));
}
use of com.linkedin.data.template.TyperefInfo in project rest.li by linkedin.
the class TestTyperefUnion method testNonTyperefUnion.
@Test
public void testNonTyperefUnion() {
TyperefInfo typerefInfo = DataTemplateUtil.getTyperefInfo(TestRecordAndUnionTemplate.Foo.Union.class);
assertNull(typerefInfo);
TestRecordAndUnionTemplate.Foo.Union union = new TestRecordAndUnionTemplate.Foo.Union();
assertFalse(union instanceof HasTyperefInfo);
}
use of com.linkedin.data.template.TyperefInfo 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.template.TyperefInfo in project rest.li by linkedin.
the class ClassNameDataSchemaResolver method locateDataSchema.
@Override
protected NamedDataSchema locateDataSchema(String className, StringBuilder errorMessageBuilder) {
final DataSchemaLocation location = new ClassNameDataSchemaLocation(className);
if (isBadLocation(location)) {
return null;
}
final Class<?> clazz;
try {
clazz = _classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
addBadLocation(location);
errorMessageBuilder.append(String.format("Unable to locate DataSchema: class \"%s\" not found", className));
return null;
}
final DataSchema schema = DataTemplateUtil.getSchema(clazz);
if (schema instanceof NamedDataSchema) {
return (NamedDataSchema) schema;
}
if (DataTemplate.class.isAssignableFrom(clazz)) {
@SuppressWarnings("unchecked") final Class<? extends DataTemplate<?>> clazzWithTyperef = (Class<? extends DataTemplate<?>>) clazz;
final TyperefInfo typerefInfo = DataTemplateUtil.getTyperefInfo(clazzWithTyperef);
if (typerefInfo != null) {
return typerefInfo.getSchema();
}
}
addBadLocation(location);
errorMessageBuilder.append(String.format("Unable to locate DataSchema: class \"%s\" is not a NamedDataSchema", className));
return null;
}
Aggregations