Search in sources :

Example 11 with UnionDataSchema

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

the class CompatibilityChecker method checkUnion.

private void checkUnion(UnionDataSchema older, UnionDataSchema newer) {
    // using list to preserve union member order
    List<DataSchema> commonMembers = new CheckerArrayList<DataSchema>(newer.getTypes().size());
    List<String> newerAdded = new CheckerArrayList<String>();
    List<String> olderAdded = new CheckerArrayList<String>();
    computeAddedUnionMembers(older, newer, newerAdded, commonMembers);
    computeAddedUnionMembers(newer, older, olderAdded, null);
    if (newerAdded.isEmpty() == false) {
        appendMessage(CompatibilityMessage.Impact.BREAKS_OLD_READER, "new union added members %s", newerAdded);
    }
    if (olderAdded.isEmpty() == false) {
        appendMessage(CompatibilityMessage.Impact.BREAKS_NEW_READER, "new union removed members %s", olderAdded);
    }
    for (DataSchema newerSchema : commonMembers) {
        String memberKey = newerSchema.getUnionMemberKey();
        DataSchema olderSchema = older.getType(memberKey);
        assert (olderSchema != null);
        check(olderSchema, newerSchema);
    }
}
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) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema)

Example 12 with UnionDataSchema

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

the class SchemaSampleDataGenerator method buildData.

private static Object buildData(ParentSchemas parentSchemas, DataSchema schema, String fieldName, DataGenerationOptions spec) {
    spec = preventRecursionIntoAlreadyTraversedSchemas(parentSchemas, spec, schema);
    parentSchemas.incrementReferences(schema);
    final DataSchema derefSchema = schema.getDereferencedDataSchema();
    final SampleDataCallback callback = spec.getCallback();
    Object data = null;
    switch(derefSchema.getType()) {
        case BOOLEAN:
            data = callback.getBoolean(fieldName);
            break;
        case INT:
            data = callback.getInteger(fieldName);
            break;
        case LONG:
            data = callback.getLong(fieldName);
            break;
        case FLOAT:
            data = callback.getFloat(fieldName);
            break;
        case DOUBLE:
            data = callback.getDouble(fieldName);
            break;
        case BYTES:
            data = callback.getBytes(fieldName);
            break;
        case STRING:
            data = callback.getString(fieldName);
            break;
        case NULL:
            data = Data.NULL;
            break;
        case FIXED:
            data = callback.getFixed(fieldName, (FixedDataSchema) derefSchema);
            break;
        case ENUM:
            data = callback.getEnum(fieldName, (EnumDataSchema) derefSchema);
            break;
        case ARRAY:
            final DataList dataList = new DataList(spec.getArraySize());
            for (int i = 0; i < spec.getArraySize(); i++) {
                final Object item = buildData(parentSchemas, ((ArrayDataSchema) derefSchema).getItems(), fieldName, spec);
                dataList.add(item);
            }
            data = dataList;
            break;
        case RECORD:
            data = buildRecordData(parentSchemas, (RecordDataSchema) derefSchema, spec);
            break;
        case MAP:
            final DataMap dataMap = new DataMap();
            for (int i = 0; i < spec.getArraySize(); i++) {
                final Object item = buildData(parentSchemas, ((MapDataSchema) derefSchema).getValues(), fieldName, spec);
                dataMap.put("mapField_" + _random.nextInt(), item);
            }
            data = dataMap;
            break;
        case UNION:
            final UnionDataSchema unionSchema = (UnionDataSchema) derefSchema;
            final List<DataSchema> types = removeAlreadyTraversedSchemasFromUnionMemberList(parentSchemas, unionSchema.getTypes());
            final int unionIndex = _random.nextInt(types.size());
            final DataSchema unionItemSchema = types.get(unionIndex);
            data = buildData(parentSchemas, unionItemSchema, fieldName, spec);
            if (data != null) {
                final DataMap unionMap = new DataMap();
                unionMap.put(unionItemSchema.getUnionMemberKey(), data);
                data = unionMap;
            }
            break;
        case TYPEREF:
            data = buildData(parentSchemas, derefSchema, fieldName, spec);
            break;
    }
    parentSchemas.decrementReferences(schema);
    return data;
}
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) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Example 13 with UnionDataSchema

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

the class PdlSchemaParser method parseUnion.

private UnionDataSchema parseUnion(UnionDeclarationContext union, boolean withinTypref) throws ParseException {
    UnionDataSchema schema = new UnionDataSchema();
    List<UnionMemberDeclarationContext> members = union.typeParams.members;
    List<DataSchema> types = new ArrayList<>(members.size());
    Set<DataSchema> typesDeclaredInline = new HashSet<>();
    for (UnionMemberDeclarationContext memberDecl : members) {
        TypeAssignmentContext memberType = memberDecl.member;
        DataSchema dataSchema = toDataSchema(memberType);
        if (dataSchema != null) {
            types.add(dataSchema);
            if (isDeclaredInline(memberDecl.member)) {
                typesDeclaredInline.add(dataSchema);
            }
        }
    }
    schema.setTypes(types, errorMessageBuilder());
    schema.setTypesDeclaredInline(typesDeclaredInline);
    return schema;
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TypeAssignmentContext(com.linkedin.data.grammar.PdlParser.TypeAssignmentContext) ArrayList(java.util.ArrayList) UnionMemberDeclarationContext(com.linkedin.data.grammar.PdlParser.UnionMemberDeclarationContext) HashSet(java.util.HashSet)

Example 14 with UnionDataSchema

use of com.linkedin.data.schema.UnionDataSchema 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();
    union.setTypes(customTypedSchemas, 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

UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)14 DataSchema (com.linkedin.data.schema.DataSchema)11 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)11 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)11 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)10 MapDataSchema (com.linkedin.data.schema.MapDataSchema)10 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)7 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)7 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)7 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)6 DataMap (com.linkedin.data.DataMap)5 DataList (com.linkedin.data.DataList)2 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)2 CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)2 UnionTemplateSpec (com.linkedin.pegasus.generator.spec.UnionTemplateSpec)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Test (org.testng.annotations.Test)2 ByteString (com.linkedin.data.ByteString)1 TypeAssignmentContext (com.linkedin.data.grammar.PdlParser.TypeAssignmentContext)1