Search in sources :

Example 1 with TyperefInfo

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));
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) TyperefInfo(com.linkedin.data.template.TyperefInfo) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) Test(org.testng.annotations.Test)

Example 2 with TyperefInfo

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);
}
Also used : HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) TestRecordAndUnionTemplate(com.linkedin.data.template.TestRecordAndUnionTemplate) TyperefInfo(com.linkedin.data.template.TyperefInfo) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) Test(org.testng.annotations.Test)

Example 3 with TyperefInfo

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();
        }
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) DataTemplate(com.linkedin.data.template.DataTemplate) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) IOException(java.io.IOException) TyperefInfo(com.linkedin.data.template.TyperefInfo) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo)

Example 4 with TyperefInfo

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;
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataTemplate(com.linkedin.data.template.DataTemplate) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) TyperefInfo(com.linkedin.data.template.TyperefInfo)

Aggregations

TyperefInfo (com.linkedin.data.template.TyperefInfo)4 HasTyperefInfo (com.linkedin.data.template.HasTyperefInfo)3 DataSchema (com.linkedin.data.schema.DataSchema)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 DataTemplate (com.linkedin.data.template.DataTemplate)2 Test (org.testng.annotations.Test)2 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)1 JsonBuilder (com.linkedin.data.schema.JsonBuilder)1 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)1 SchemaToJsonEncoder (com.linkedin.data.schema.SchemaToJsonEncoder)1 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)1 TestRecordAndUnionTemplate (com.linkedin.data.template.TestRecordAndUnionTemplate)1 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)1 IOException (java.io.IOException)1