Search in sources :

Example 31 with TyperefDataSchema

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

the class JavaRequestBuilderGenerator method getJavaBindingType.

private JavaBinding getJavaBindingType(DataSchema schema, JDefinedClass enclosingClass) {
    final JavaBinding binding = new JavaBinding();
    if (_generateDataTemplates || schema instanceof ArrayDataSchema) {
        final ClassTemplateSpec classSpec = generateClassSpec(schema, enclosingClass);
        binding.schemaClass = _javaDataTemplateGenerator.generate(classSpec);
        {
            _generatedArrayClasses.add(binding.schemaClass);
        }
    }
    binding.schemaClass = getClassRefForSchema(schema, enclosingClass);
    binding.valueClass = getClassRefForSchema(schema, enclosingClass);
    if (schema instanceof TyperefDataSchema) {
        final TyperefDataSchema typerefDataSchema = (TyperefDataSchema) schema;
        if (typerefDataSchema.getDereferencedDataSchema().getType() != DataSchema.Type.UNION) {
            final String javaClassNameFromSchema = CustomTypeUtil.getJavaCustomTypeClassNameFromSchema(typerefDataSchema);
            if (javaClassNameFromSchema != null) {
                binding.valueClass = getCodeModel().directClass(javaClassNameFromSchema);
            } else {
                binding.valueClass = getJavaBindingType(typerefDataSchema.getRef(), enclosingClass).valueClass;
            }
        }
    }
    return binding;
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema)

Example 32 with TyperefDataSchema

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

the class TestCopySchemaUtil method testBuildSkeletonSchema.

@Test
public void testBuildSkeletonSchema() throws Exception {
    DataSchema oldSchema = null;
    RecordDataSchema fooSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(fooSchemaText);
    // Test Record
    RecordDataSchema newRecordSchema = (RecordDataSchema) CopySchemaUtil.buildSkeletonSchema(fooSchema);
    assert ((newRecordSchema.getFields().size() == 0) && Objects.equals(newRecordSchema.getDoc(), fooSchema.getDoc()) && Objects.equals(newRecordSchema.getProperties(), fooSchema.getProperties()) && Objects.equals(newRecordSchema.getAliases(), fooSchema.getAliases()));
    // Test TypeRef
    oldSchema = fooSchema.getField("typeRefField").getType();
    TyperefDataSchema newTypeRefDataSchema = (TyperefDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
    assert (Objects.equals(newTypeRefDataSchema.getDoc(), ((TyperefDataSchema) oldSchema).getDoc()) && Objects.equals(newTypeRefDataSchema.getProperties(), oldSchema.getProperties()) && Objects.equals(newTypeRefDataSchema.getAliases(), ((TyperefDataSchema) oldSchema).getAliases()));
    // Test Union
    oldSchema = fooSchema.getField("unionField").getType();
    UnionDataSchema newUnionDataSchema = (UnionDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
    assert (newUnionDataSchema.getMembers().size() == 0 && Objects.equals(newUnionDataSchema.getProperties(), oldSchema.getProperties()));
    // Test map
    oldSchema = fooSchema.getField("mapField").getType();
    MapDataSchema mapDataSchema = (MapDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
    assert (Objects.equals(mapDataSchema.getProperties(), oldSchema.getProperties()) && Objects.equals(mapDataSchema.getValues(), DataSchemaConstants.NULL_DATA_SCHEMA));
    // Test array
    oldSchema = fooSchema.getField("arrayField").getType();
    ArrayDataSchema arrayDataSchema = (ArrayDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
    assert (Objects.equals(arrayDataSchema.getProperties(), oldSchema.getProperties()) && Objects.equals(arrayDataSchema.getItems(), DataSchemaConstants.NULL_DATA_SCHEMA));
    // Test ENUM
    oldSchema = fooSchema.getField("enumField").getType();
    EnumDataSchema enumDataSchema = (EnumDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
    Assert.assertEquals(enumDataSchema, oldSchema);
    // Test FIXED
    oldSchema = fooSchema.getField("fixedField").getType();
    FixedDataSchema fixedDataSchema = (FixedDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
    Assert.assertEquals(fixedDataSchema, oldSchema);
    // Test primitive
    oldSchema = fooSchema.getField("intField").getType();
    PrimitiveDataSchema primitiveDataSchema = (PrimitiveDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
    Assert.assertEquals(primitiveDataSchema, oldSchema);
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) Test(org.testng.annotations.Test)

Example 33 with TyperefDataSchema

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

the class SchemaToAvroJsonEncoder method produceFieldProperties.

@SuppressWarnings("unchecked")
static /* package private */
Map<String, ?> produceFieldProperties(RecordDataSchema.Field field, DataToAvroSchemaTranslationOptions options) {
    Stream<Map.Entry<String, Object>> toBeFiltered = field.getProperties().entrySet().stream();
    // and merge with record field's properties.
    if (field.getType().getType() == DataSchema.Type.TYPEREF) {
        toBeFiltered = Stream.concat(toBeFiltered, ((TyperefDataSchema) field.getType()).getMergedTyperefProperties().entrySet().stream()).filter(entry -> !options.getTyperefPropertiesExcludeSet().contains(entry.getKey()));
    }
    // Property merge rule:
    // For property content inherited from TypeRef that appears to be have same property name as the record field:
    // if the two property contents are Map type, they will be merged at this level,
    // otherwise Typeref field property content will be overridden by record field property's content.
    BinaryOperator<Object> propertyMergeLogic = (originalPropertyContent, inheritedPropertyContent) -> {
        if (originalPropertyContent instanceof Map && inheritedPropertyContent instanceof Map) {
            Map<String, Object> mergedMap = new DataMap((Map<String, Object>) originalPropertyContent);
            ((Map<String, Object>) inheritedPropertyContent).forEach(mergedMap::putIfAbsent);
            return mergedMap;
        } else {
            return originalPropertyContent;
        }
    };
    return toBeFiltered.filter(entry -> !RESERVED_DATA_PROPERTIES.contains(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, propertyMergeLogic));
}
Also used : Arrays(java.util.Arrays) DataSchema(com.linkedin.data.schema.DataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) ArrayList(java.util.ArrayList) Formatter(java.util.Formatter) Data(com.linkedin.data.Data) HashSet(java.util.HashSet) DataMap(com.linkedin.data.DataMap) Named(com.linkedin.data.schema.Named) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) Map(java.util.Map) AVRO_PREFIX(com.linkedin.data.avro.SchemaTranslator.AVRO_PREFIX) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) Set(java.util.Set) TYPE_KEY(com.linkedin.data.schema.DataSchemaConstants.TYPE_KEY) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) List(java.util.List) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Stream(java.util.stream.Stream) DEFAULT_KEY(com.linkedin.data.schema.DataSchemaConstants.DEFAULT_KEY) DataSchemaConstants(com.linkedin.data.schema.DataSchemaConstants) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 34 with TyperefDataSchema

use of com.linkedin.data.schema.TyperefDataSchema 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(), "com.linkedin.pegasus.generator.testpackage.Union");
    assertEquals(typerefDataSchema.getBindingName(), Union.class.getName());
    assertEquals(typerefDataSchema.getRef(), DataTemplateUtil.getSchema(Union.class));
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) Test(org.testng.annotations.Test)

Example 35 with TyperefDataSchema

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

the class RestLiAnnotationReader method getActionTyperefDataSchema.

private static TyperefDataSchema getActionTyperefDataSchema(ResourceModel model, Action actionAnno, String actionName) {
    TyperefDataSchema returnTyperefSchema = null;
    Class<? extends TyperefInfo> typerefInfoClass = actionAnno.returnTyperef();
    try {
        returnTyperefSchema = getSchemaFromTyperefInfo(typerefInfoClass);
    } catch (Exception e) {
        throw new ResourceConfigException("Typeref @Action method named '" + actionName + "' on class '" + model.getResourceClass().getName() + "' cannot be instantiated, " + e.getMessage());
    }
    return returnTyperefSchema;
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

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