Search in sources :

Example 6 with UnionDataSchema

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

the class ExampleRequestResponseGenerator method generateFieldDefValue.

private Object generateFieldDefValue(FieldDef<?> fieldDef) {
    Object value = _dataGenerator.buildData(fieldDef.getName(), fieldDef.getDataSchema());
    DataSchema dereferencedDataSchema = fieldDef.getDataSchema().getDereferencedDataSchema();
    if (!dereferencedDataSchema.isPrimitive()) {
        switch(dereferencedDataSchema.getType()) {
            case FIXED:
                value = new FixedTemplatePlaceholder(value, (FixedDataSchema) dereferencedDataSchema);
                break;
            case ENUM:
                // just use the string value already generated.  Will be coerced by DataTemplateUtil.DynamicEnumCoercer.
                break;
            case ARRAY:
                value = new ArrayTemplatePlaceholder<Object>((DataList) value, (ArrayDataSchema) dereferencedDataSchema, Object.class);
                break;
            case RECORD:
                value = new RecordTemplatePlaceholder((DataMap) value, (RecordDataSchema) dereferencedDataSchema);
                break;
            case MAP:
                value = new MapTemplatePlaceholder<Object>((DataMap) value, (MapDataSchema) dereferencedDataSchema, Object.class);
                break;
            case UNION:
                value = new UnionTemplatePlaceholder(value, (UnionDataSchema) dereferencedDataSchema);
                break;
            case TYPEREF:
                throw new IllegalStateException("TYPEREF should not be returned for a dereferenced byte. schema: " + fieldDef.getDataSchema());
            default:
                throw new IllegalStateException("Unrecognized enum value: " + dereferencedDataSchema.getType());
        }
    }
    return value;
}
Also used : MapDataSchema(com.linkedin.data.schema.MapDataSchema) DataMap(com.linkedin.data.DataMap) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataList(com.linkedin.data.DataList) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 7 with UnionDataSchema

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

the class SnapshotGenerator method recordType.

private void recordType(DataSchema schema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    if (schema instanceof NamedDataSchema) {
        NamedDataSchema namedDataSchema = (NamedDataSchema) schema;
        if (!foundTypes.containsKey(namedDataSchema.getFullName())) {
            foundTypes.put(namedDataSchema.getFullName(), namedDataSchema);
            if (// recurse into record, record any contained types.
            schema instanceof RecordDataSchema) {
                RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
                for (NamedDataSchema includedSchema : recordDataSchema.getInclude()) {
                    recordType(includedSchema, foundTypes, typeOrder);
                }
                for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
                    recordType(field.getType(), foundTypes, typeOrder);
                }
            } else if (schema instanceof TyperefDataSchema) {
                recordType(schema.getDereferencedDataSchema(), foundTypes, typeOrder);
            }
            typeOrder.add(namedDataSchema);
        }
    } else if (schema instanceof ArrayDataSchema) {
        ArrayDataSchema arraySchema = (ArrayDataSchema) schema;
        recordType(arraySchema.getItems(), foundTypes, typeOrder);
    } else if (schema instanceof MapDataSchema) {
        MapDataSchema mapSchema = (MapDataSchema) schema;
        recordType(mapSchema.getValues(), foundTypes, typeOrder);
    } else if (schema instanceof UnionDataSchema) {
        UnionDataSchema unionSchema = (UnionDataSchema) schema;
        for (DataSchema type : unionSchema.getTypes()) {
            recordType(type, foundTypes, typeOrder);
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) 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 8 with UnionDataSchema

use of com.linkedin.data.schema.UnionDataSchema 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));
        schemaToEncode = unionRef.getSchema();
    } 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 9 with UnionDataSchema

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

the class CompatibilityChecker method check.

private void check(DataSchema older, DataSchema newer) {
    Checked toCheck = new Checked(older, newer);
    if (_checked.contains(toCheck)) {
        return;
    }
    _checked.add(toCheck);
    if (older == newer) {
        return;
    }
    int pathCount = 1;
    if (_options.getMode() == CompatibilityOptions.Mode.DATA) {
        older = older.getDereferencedDataSchema();
        while (newer.getType() == DataSchema.Type.TYPEREF) {
            TyperefDataSchema typerefDataSchema = ((TyperefDataSchema) newer);
            _path.addLast(typerefDataSchema.getFullName());
            _path.addLast(DataSchemaConstants.REF_KEY);
            pathCount++;
            newer = typerefDataSchema.getRef();
        }
    }
    if (newer.getType() == DataSchema.Type.TYPEREF) {
        _path.addLast(((TyperefDataSchema) newer).getFullName());
    } else {
        _path.addLast(newer.getUnionMemberKey());
    }
    switch(newer.getType()) {
        case TYPEREF:
            if (isSameType(older, newer))
                checkTyperef((TyperefDataSchema) older, (TyperefDataSchema) newer);
            break;
        case RECORD:
            if (isSameType(older, newer))
                checkRecord((RecordDataSchema) older, (RecordDataSchema) newer);
            break;
        case ARRAY:
            if (isSameType(older, newer))
                checkArray((ArrayDataSchema) older, (ArrayDataSchema) newer);
            break;
        case MAP:
            if (isSameType(older, newer))
                checkMap((MapDataSchema) older, (MapDataSchema) newer);
            break;
        case ENUM:
            if (isSameType(older, newer))
                checkEnum((EnumDataSchema) older, (EnumDataSchema) newer);
            break;
        case FIXED:
            if (isSameType(older, newer))
                checkFixed((FixedDataSchema) older, (FixedDataSchema) newer);
            break;
        case UNION:
            if (isSameType(older, newer))
                checkUnion((UnionDataSchema) older, (UnionDataSchema) newer);
            break;
        default:
            if (newer instanceof PrimitiveDataSchema)
                checkPrimitive(older, newer);
            else
                throw new IllegalStateException("Unknown schema type " + newer.getType() + ", checking old schema " + older + ", new schema " + newer);
            break;
    }
    for (; pathCount > 0; pathCount--) {
        _path.removeLast();
    }
    return;
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) 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)

Example 10 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)

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