Search in sources :

Example 21 with TyperefDataSchema

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();
        }
    }
}
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 22 with TyperefDataSchema

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;
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) Name(com.linkedin.data.schema.Name)

Example 23 with TyperefDataSchema

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!");
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 24 with TyperefDataSchema

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)));
    }
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) Mock(org.mockito.Mock) DataSchema(com.linkedin.data.schema.DataSchema) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) StringDataSchemaLocation(com.linkedin.data.schema.StringDataSchemaLocation) CustomTypeUtil(com.linkedin.util.CustomTypeUtil) ArrayList(java.util.ArrayList) MockitoAnnotations(org.mockito.MockitoAnnotations) DataMap(com.linkedin.data.DataMap) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) BeforeMethod(org.testng.annotations.BeforeMethod) Collectors(java.util.stream.Collectors) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) Mockito(org.mockito.Mockito) List(java.util.List) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) DataSchemaUtil(com.linkedin.data.schema.DataSchemaUtil) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) Collections(java.util.Collections) Name(com.linkedin.data.schema.Name) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) Name(com.linkedin.data.schema.Name) Test(org.testng.annotations.Test)

Example 25 with TyperefDataSchema

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)));
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) Name(com.linkedin.data.schema.Name) Test(org.testng.annotations.Test)

Aggregations

TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)35 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)26 DataSchema (com.linkedin.data.schema.DataSchema)23 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)21 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)18 MapDataSchema (com.linkedin.data.schema.MapDataSchema)14 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)9 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)9 DataMap (com.linkedin.data.DataMap)8 Test (org.testng.annotations.Test)8 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)7 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 Name (com.linkedin.data.schema.Name)6 ArrayList (java.util.ArrayList)4 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)3 StringDataSchema (com.linkedin.data.schema.StringDataSchema)3 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)3 DataList (com.linkedin.data.DataList)2 JsonBuilder (com.linkedin.data.schema.JsonBuilder)2 PathSpec (com.linkedin.data.schema.PathSpec)2