Search in sources :

Example 46 with ArrayDataSchema

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

the class DataSchemaRichContextTraverser method doRecursiveTraversal.

private void doRecursiveTraversal(TraverserContextImpl context) {
    // Add full name to the context's TraversePath
    DataSchema schema = context.getCurrentSchema();
    ArrayDeque<String> path = context.getTraversePath();
    path.add(schema.getUnionMemberKey());
    // visitors
    _schemaVisitor.callbackOnContext(context, DataSchemaTraverse.Order.PRE_ORDER);
    /**
     * By default {@link DataSchemaRichContextTraverser} will only decide whether or not keep traversing based on whether the new
     * data schema has been seen.
     *
     * But the {@link SchemaVisitor} has the chance to override this control by setting {@link TraverserContext#_shouldContinue}
     * If this variable set to be {@link Boolean#TRUE}, the {@link DataSchemaRichContextTraverser} will traverse to next level (if applicable)
     * If this variable set to be {@link Boolean#FALSE}, the {@link DataSchemaRichContextTraverser} will stop traversing to next level
     * If this variable not set, the {@link DataSchemaRichContextTraverser} will decide whether or not to continue traversing based on whether
     * this data schema has been seen.
     */
    if (context.shouldContinue() == Boolean.TRUE || !(context.shouldContinue() == Boolean.FALSE || _seenAncestorsDataSchema.containsKey(schema))) {
        _seenAncestorsDataSchema.put(schema, Boolean.TRUE);
        // Pass new context in every recursion
        TraverserContextImpl nextContext = null;
        switch(schema.getType()) {
            case TYPEREF:
                TyperefDataSchema typerefDataSchema = (TyperefDataSchema) schema;
                nextContext = context.getNextContext(DataSchemaConstants.REF_KEY, null, typerefDataSchema.getRef(), CurrentSchemaEntryMode.TYPEREF_REF);
                doRecursiveTraversal(nextContext);
                break;
            case MAP:
                // traverse key
                MapDataSchema mapDataSchema = (MapDataSchema) schema;
                nextContext = context.getNextContext(DataSchemaConstants.MAP_KEY_REF, DataSchemaConstants.MAP_KEY_REF, mapDataSchema.getKey(), CurrentSchemaEntryMode.MAP_KEY);
                doRecursiveTraversal(nextContext);
                // then traverse values
                nextContext = context.getNextContext(PathSpec.WILDCARD, PathSpec.WILDCARD, mapDataSchema.getValues(), CurrentSchemaEntryMode.MAP_VALUE);
                doRecursiveTraversal(nextContext);
                break;
            case ARRAY:
                ArrayDataSchema arrayDataSchema = (ArrayDataSchema) schema;
                nextContext = context.getNextContext(PathSpec.WILDCARD, PathSpec.WILDCARD, arrayDataSchema.getItems(), CurrentSchemaEntryMode.ARRAY_VALUE);
                doRecursiveTraversal(nextContext);
                break;
            case RECORD:
                RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
                for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
                    nextContext = context.getNextContext(field.getName(), field.getName(), field.getType(), CurrentSchemaEntryMode.FIELD);
                    nextContext.setEnclosingField(field);
                    doRecursiveTraversal(nextContext);
                }
                break;
            case UNION:
                UnionDataSchema unionDataSchema = (UnionDataSchema) schema;
                for (UnionDataSchema.Member member : unionDataSchema.getMembers()) {
                    nextContext = context.getNextContext(member.getUnionMemberKey(), member.getUnionMemberKey(), member.getType(), CurrentSchemaEntryMode.UNION_MEMBER);
                    nextContext.setEnclosingUnionMember(member);
                    doRecursiveTraversal(nextContext);
                }
                break;
            default:
                // will stop recursively traversing if the current schema is a leaf node.
                assert isLeafSchema(schema);
                break;
        }
        _seenAncestorsDataSchema.remove(schema);
    }
    _schemaVisitor.callbackOnContext(context, DataSchemaTraverse.Order.POST_ORDER);
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 47 with ArrayDataSchema

use of com.linkedin.data.schema.ArrayDataSchema 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 48 with ArrayDataSchema

use of com.linkedin.data.schema.ArrayDataSchema 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 49 with ArrayDataSchema

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

the class TestParameterDefaultValue method testCustomParamArray.

@Test
public void testCustomParamArray() {
    // Initialize the custom class to ensure the coercer is registered.
    Custom.initializeCustomClass(CustomLong.class);
    final ArrayDataSchema customLongRefArraySchema = ((ArrayDataSchema) DataTemplateUtil.parseSchema("{\"type\":\"array\",\"items\":{\"type\":\"typeref\",\"name\":\"CustomLongRef\",\"namespace\":\"com.linkedin.restli.examples.typeref.api\",\"ref\":\"long\",\"java\":{\"class\":\"com.linkedin.restli.examples.custom.types.CustomLong\"}}}"));
    Object result = test("[12345, 6789]", CustomLong[].class, customLongRefArraySchema);
    final CustomLong[] expectedCustomLongs = { new CustomLong(12345L), new CustomLong(6789L) };
    Assert.assertEquals(result, expectedCustomLongs);
    Assert.assertSame(result.getClass(), CustomLong[].class);
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) CustomLong(com.linkedin.restli.server.custom.types.CustomLong) Test(org.testng.annotations.Test) ArrayTest(com.linkedin.pegasus.generator.test.ArrayTest)

Example 50 with ArrayDataSchema

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

the class PegasusUnionToAvroRecordConvertCallback method modifyFieldSchema.

/**
 * Modify the schema for the specified field. The schema modification happens only for fields whose type is either a
 * union that uses aliases for its members or an array or map that has a similar union as its element type (either
 * direct or nested).
 *
 * @param recordSchema An instance of {@link RecordDataSchema} for the record that contains this field
 * @param field An instance of {@link com.linkedin.data.schema.RecordDataSchema.Field} whose schema needs to be translated
 * @param dataSchema An instance of {@link DataSchema} that is being translated. The initial call will have the field's
 *                   schema but when called recursively for arrays and maps, the schema will be of it's elements.
 * @param modifiedDefaultValue An instance of {@link DataMap} which is the modified default value for the field
 * @return An instance of {@link DataSchema} which is the translated schema for the field or null, if the schema
 * doesn't have to be translated
 */
private DataSchema modifyFieldSchema(RecordDataSchema recordSchema, RecordDataSchema.Field field, DataSchema dataSchema, DataMap modifiedDefaultValue) {
    DataSchema modifiedSchema = null;
    switch(dataSchema.getType()) {
        case ARRAY:
            DataSchema itemsSchema = ((ArrayDataSchema) dataSchema).getItems().getDereferencedDataSchema();
            // Stop propagating the default value if the field type is an array
            DataSchema modifiedItemsSchema = modifyFieldSchema(recordSchema, field, itemsSchema, null);
            if (modifiedItemsSchema != null) {
                modifiedSchema = new ArrayDataSchema(modifiedItemsSchema);
            }
            break;
        case MAP:
            DataSchema valuesSchema = ((MapDataSchema) dataSchema).getValues().getDereferencedDataSchema();
            // Stop propagating the default value if the field type is a map
            DataSchema modifiedValuesSchema = modifyFieldSchema(recordSchema, field, valuesSchema, null);
            if (modifiedValuesSchema != null) {
                modifiedSchema = new MapDataSchema(modifiedValuesSchema);
            }
            break;
        case UNION:
            UnionDataSchema unionDataSchema = (UnionDataSchema) dataSchema;
            if (unionDataSchema.areMembersAliased()) {
                modifiedSchema = buildContainerRecordFromUnion(unionDataSchema, field.getName(), recordSchema.getFullName(), modifiedDefaultValue);
            }
            break;
    }
    return modifiedSchema;
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema)

Aggregations

ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)57 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)27 DataSchema (com.linkedin.data.schema.DataSchema)24 Test (org.testng.annotations.Test)22 MapDataSchema (com.linkedin.data.schema.MapDataSchema)21 DataList (com.linkedin.data.DataList)20 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)20 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)19 DataMap (com.linkedin.data.DataMap)17 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)11 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)8 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)8 ArrayList (java.util.ArrayList)8 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)7 ByteString (com.linkedin.data.ByteString)5 Name (com.linkedin.data.schema.Name)4 TestDataTemplateUtil (com.linkedin.data.template.TestDataTemplateUtil)4 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)4 DataTemplate (com.linkedin.data.template.DataTemplate)3 TemplateRuntimeException (com.linkedin.data.template.TemplateRuntimeException)3