Search in sources :

Example 1 with PrimitiveDataSchema

use of com.linkedin.data.schema.PrimitiveDataSchema in project rest.li by linkedin.

the class TemplateSpecGenerator method processSchema.

private ClassTemplateSpec processSchema(DataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
    final CustomInfoSpec customInfo = getImmediateCustomInfo(schema);
    ClassTemplateSpec result = null;
    TyperefDataSchema originalTyperefSchema = null;
    while (schema.getType() == DataSchema.Type.TYPEREF) {
        final TyperefDataSchema typerefSchema = (TyperefDataSchema) schema;
        if (originalTyperefSchema == null) {
            originalTyperefSchema = typerefSchema;
        }
        final ClassTemplateSpec found = _schemaToClassMap.get(schema);
        schema = typerefSchema.getRef();
        if (schema.getType() == DataSchema.Type.UNION) {
            result = (found != null) ? found : generateUnion((UnionDataSchema) schema, typerefSchema);
            break;
        } else if (found == null) {
            generateTyperef(typerefSchema, originalTyperefSchema);
        }
    }
    if (result == null) {
        assert schema == schema.getDereferencedDataSchema();
        if (schema instanceof ComplexDataSchema) {
            final ClassTemplateSpec found = _schemaToClassMap.get(schema);
            if (found == null) {
                if (schema instanceof NamedDataSchema) {
                    result = generateNamedSchema((NamedDataSchema) schema);
                } else {
                    result = generateUnnamedComplexSchema(schema, enclosingClass, memberName);
                }
            } else {
                result = found;
            }
            if (customInfo != null) {
                result = customInfo.getCustomClass();
            }
        } else if (schema instanceof PrimitiveDataSchema) {
            result = (customInfo != null) ? customInfo.getCustomClass() : getPrimitiveClassForSchema((PrimitiveDataSchema) schema, enclosingClass, memberName);
        }
    }
    if (result == null) {
        throw unrecognizedSchemaType(enclosingClass, memberName, schema);
    }
    result.setOriginalTyperefSchema(originalTyperefSchema);
    return result;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema)

Example 2 with PrimitiveDataSchema

use of com.linkedin.data.schema.PrimitiveDataSchema in project rest.li by linkedin.

the class TemplateSpecGenerator method determineDataClass.

private ClassTemplateSpec determineDataClass(DataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
    final ClassTemplateSpec result;
    final DataSchema dereferencedSchema = schema.getDereferencedDataSchema();
    if (dereferencedSchema.getType() == DataSchema.Type.ENUM) {
        result = PrimitiveTemplateSpec.getInstance(DataSchema.Type.STRING);
    } else if (CodeUtil.isDirectType(dereferencedSchema)) {
        result = getPrimitiveClassForSchema((PrimitiveDataSchema) dereferencedSchema, enclosingClass, memberName);
    } else {
        result = null;
    }
    return result;
}
Also used : FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec)

Example 3 with PrimitiveDataSchema

use of com.linkedin.data.schema.PrimitiveDataSchema in project rest.li by linkedin.

the class TestSchemaSampleDataGenerator method testArraySchema.

@Test
public void testArraySchema() {
    for (Map.Entry<DataSchema.Type, Class<? extends DirectArrayTemplate<?>>> entry : _dataSchemaTypeToprimitiveArrayMap.entrySet()) {
        final PrimitiveDataSchema itemsSchema = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchema(entry.getKey());
        final ArrayDataSchema arraySchema = new ArrayDataSchema(itemsSchema);
        final DataList value = (DataList) SchemaSampleDataGenerator.buildData(arraySchema, _spec);
        final ParameterizedType arrayType = (ParameterizedType) entry.getValue().getGenericSuperclass();
        assert (arrayType.getRawType() == DirectArrayTemplate.class);
        Assert.assertSame(value.get(0).getClass(), arrayType.getActualTypeArguments()[0]);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) DirectArrayTemplate(com.linkedin.data.template.DirectArrayTemplate) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) ParameterizedType(java.lang.reflect.ParameterizedType) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) BooleanMap(com.linkedin.data.template.BooleanMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) DoubleMap(com.linkedin.data.template.DoubleMap) BytesMap(com.linkedin.data.template.BytesMap) FloatMap(com.linkedin.data.template.FloatMap) IntegerMap(com.linkedin.data.template.IntegerMap) DataMap(com.linkedin.data.DataMap) StringMap(com.linkedin.data.template.StringMap) LongMap(com.linkedin.data.template.LongMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 4 with PrimitiveDataSchema

use of com.linkedin.data.schema.PrimitiveDataSchema in project rest.li by linkedin.

the class TestSchemaSampleDataGenerator method testPrimitiveSchema.

@Test
public void testPrimitiveSchema() {
    for (Map.Entry<DataSchema.Type, Class<?>> entry : _dataSchemaTypeToPrimitiveJavaTypeMap.entrySet()) {
        final PrimitiveDataSchema schema = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchema(entry.getKey());
        final Object value = SchemaSampleDataGenerator.buildData(schema, _spec);
        Assert.assertSame(value.getClass(), entry.getValue());
    }
    final PrimitiveDataSchema nullSchema = DataSchemaConstants.NULL_DATA_SCHEMA;
    final Object nullData = SchemaSampleDataGenerator.buildData(nullSchema, _spec);
    Assert.assertEquals(nullData, Data.NULL);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) BooleanMap(com.linkedin.data.template.BooleanMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) DoubleMap(com.linkedin.data.template.DoubleMap) BytesMap(com.linkedin.data.template.BytesMap) FloatMap(com.linkedin.data.template.FloatMap) IntegerMap(com.linkedin.data.template.IntegerMap) DataMap(com.linkedin.data.DataMap) StringMap(com.linkedin.data.template.StringMap) LongMap(com.linkedin.data.template.LongMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 5 with PrimitiveDataSchema

use of com.linkedin.data.schema.PrimitiveDataSchema 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();
        }
    }
}
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)

Aggregations

PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)10 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)6 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)5 MapDataSchema (com.linkedin.data.schema.MapDataSchema)5 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)5 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)5 DataSchema (com.linkedin.data.schema.DataSchema)4 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)4 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)4 Test (org.testng.annotations.Test)4 DataMap (com.linkedin.data.DataMap)3 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)3 BooleanMap (com.linkedin.data.template.BooleanMap)3 BytesMap (com.linkedin.data.template.BytesMap)3 DoubleMap (com.linkedin.data.template.DoubleMap)3 FloatMap (com.linkedin.data.template.FloatMap)3 IntegerMap (com.linkedin.data.template.IntegerMap)3 LongMap (com.linkedin.data.template.LongMap)3 StringMap (com.linkedin.data.template.StringMap)3