Search in sources :

Example 16 with EnumDataSchema

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

the class TestResourceCompatibilityChecker method bindSchemaResolvers.

/**
 * Constructs "previous", "current-compatible", and "current-incompatible" schemas and binds them to their respective
 * schema resolvers.
 * TODO: This should be refactored to use schemas defined in the resources directory so that it's easier to extend.
 */
private void bindSchemaResolvers() {
    StringBuilder errors = new StringBuilder();
    Name toneName = new Name("com.linkedin.greetings.api.Tone");
    EnumDataSchema tone = new EnumDataSchema(toneName);
    List<String> symbols = new ArrayList<>();
    symbols.add("FRIENDLY");
    symbols.add("SINCERE");
    symbols.add("INSULTING");
    tone.setSymbols(symbols, errors);
    Name greetingName = new Name("com.linkedin.greetings.api.Greeting");
    RecordDataSchema prevGreeting = new RecordDataSchema(greetingName, RecordDataSchema.RecordType.RECORD);
    List<RecordDataSchema.Field> oldFields = new ArrayList<>();
    RecordDataSchema.Field id = new RecordDataSchema.Field(new LongDataSchema());
    id.setName("id", errors);
    oldFields.add(id);
    RecordDataSchema.Field message = new RecordDataSchema.Field(new StringDataSchema());
    message.setName("message", errors);
    oldFields.add(message);
    RecordDataSchema.Field toneField = new RecordDataSchema.Field(tone);
    toneField.setName("tone", errors);
    toneField.setOptional(true);
    oldFields.add(toneField);
    prevGreeting.setFields(oldFields, errors);
    // Previous error details schema (overwrites the existing one in the resolver path)
    Name dummyErrorDetailsName = new Name("com.linkedin.restli.tools.DummyErrorDetails");
    RecordDataSchema prevDummyErrorDetails = new RecordDataSchema(dummyErrorDetailsName, RecordDataSchema.RecordType.RECORD);
    RecordDataSchema.Field idField = new RecordDataSchema.Field(new LongDataSchema());
    idField.setName("id", errors);
    RecordDataSchema.Field validatedField = new RecordDataSchema.Field(new StringDataSchema());
    validatedField.setName("validated", errors);
    validatedField.setProperties(Collections.singletonMap("validate", new DataMap(Collections.singletonMap("v1", new DataMap()))));
    prevDummyErrorDetails.setFields(Arrays.asList(idField, validatedField), errors);
    prevSchemaResolver.bindNameToSchema(toneName, tone, null);
    prevSchemaResolver.bindNameToSchema(greetingName, prevGreeting, null);
    prevSchemaResolver.bindNameToSchema(dummyErrorDetailsName, prevDummyErrorDetails, null);
    // compat greeting added a new optional field "newField"
    RecordDataSchema compatGreeting = new RecordDataSchema(greetingName, RecordDataSchema.RecordType.RECORD);
    List<RecordDataSchema.Field> compatFields = new ArrayList<>();
    compatFields.add(id);
    compatFields.add(message);
    compatFields.add(toneField);
    RecordDataSchema.Field newCompatField = new RecordDataSchema.Field(new StringDataSchema());
    newCompatField.setName("newField", errors);
    newCompatField.setOptional(true);
    compatFields.add(newCompatField);
    compatGreeting.setFields(compatFields, errors);
    // Compatible error details schema
    RecordDataSchema compatDummyErrorDetails = new RecordDataSchema(dummyErrorDetailsName, RecordDataSchema.RecordType.RECORD);
    compatDummyErrorDetails.setFields(Arrays.asList(idField, validatedField, newCompatField), errors);
    compatSchemaResolver.bindNameToSchema(toneName, tone, null);
    compatSchemaResolver.bindNameToSchema(greetingName, compatGreeting, null);
    compatSchemaResolver.bindNameToSchema(dummyErrorDetailsName, compatDummyErrorDetails, null);
    // Incompatible greeting has removed non-optional field "message",
    // has changed the type of "id" to string,
    // and added a new non-optional field "newField"
    RecordDataSchema incompatGreeting = new RecordDataSchema(greetingName, RecordDataSchema.RecordType.RECORD);
    List<RecordDataSchema.Field> incompatFields = new ArrayList<>();
    RecordDataSchema.Field incompatId = new RecordDataSchema.Field(new StringDataSchema());
    incompatId.setName("id", errors);
    oldFields.add(incompatId);
    incompatFields.add(incompatId);
    incompatFields.add(toneField);
    RecordDataSchema.Field newIncompatField = new RecordDataSchema.Field(new StringDataSchema());
    newIncompatField.setName("newField", errors);
    incompatFields.add(newIncompatField);
    incompatGreeting.setFields(incompatFields, errors);
    // Incompatible error details schema
    RecordDataSchema incompatDummyErrorDetails = new RecordDataSchema(dummyErrorDetailsName, RecordDataSchema.RecordType.RECORD);
    RecordDataSchema.Field incompatValidatedField = new RecordDataSchema.Field(new StringDataSchema());
    incompatValidatedField.setName("validated", errors);
    incompatValidatedField.setProperties(Collections.singletonMap("validate", new DataMap(Collections.singletonMap("v2", new DataMap()))));
    incompatDummyErrorDetails.setFields(Collections.singletonList(incompatValidatedField), errors);
    incompatSchemaResolver.bindNameToSchema(toneName, tone, null);
    incompatSchemaResolver.bindNameToSchema(greetingName, incompatGreeting, null);
    incompatSchemaResolver.bindNameToSchema(dummyErrorDetailsName, incompatDummyErrorDetails, null);
}
Also used : ArrayList(java.util.ArrayList) Name(com.linkedin.data.schema.Name) DataMap(com.linkedin.data.DataMap) StringDataSchema(com.linkedin.data.schema.StringDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) LongDataSchema(com.linkedin.data.schema.LongDataSchema)

Example 17 with EnumDataSchema

use of com.linkedin.data.schema.EnumDataSchema 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<UnionDataSchema.Member> members = removeAlreadyTraversedSchemasFromUnionMemberList(parentSchemas, unionSchema.getMembers());
            final int unionIndex = _random.nextInt(members.size());
            final UnionDataSchema.Member unionMember = members.get(unionIndex);
            data = buildData(parentSchemas, unionMember.getType(), fieldName, spec);
            if (data != null) {
                final DataMap unionMap = new DataMap();
                unionMap.put(unionMember.getUnionMemberKey(), data);
                data = unionMap;
            }
            break;
        case TYPEREF:
            data = buildData(parentSchemas, derefSchema, fieldName, spec);
            break;
    }
    parentSchemas.decrementReferences(schema);
    return data;
}
Also used : DataMap(com.linkedin.data.DataMap) 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)

Example 18 with EnumDataSchema

use of com.linkedin.data.schema.EnumDataSchema 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 || _options.getMode() == CompatibilityOptions.Mode.EXTENSION) {
        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 19 with EnumDataSchema

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

the class ResourceSchemaToResourceSpecTranslator method toType.

public Class<?> toType(DataSchema schema) {
    if (schema.getType() == DataSchema.Type.TYPEREF) {
        Class<?> javaClass = CustomTypeUtil.getJavaCustomTypeClassFromSchema((TyperefDataSchema) schema);
        if (javaClass != null)
            return javaClass;
    }
    DataSchema.Type dereferencedType = schema.getDereferencedType();
    DataSchema dereferencedDataSchema = schema.getDereferencedDataSchema();
    if (dereferencedDataSchema.isPrimitive()) {
        return DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(dereferencedType);
    } else if (dereferencedDataSchema instanceof EnumDataSchema) {
        return _bindingResolver.resolveEnumClass((EnumDataSchema) dereferencedDataSchema);
    } else {
        return _bindingResolver.resolveTemplateClass(dereferencedDataSchema);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema)

Example 20 with EnumDataSchema

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

the class TestRestLiSymbolTableProvider method setup.

@SuppressWarnings("unchecked")
@BeforeMethod
public void setup() {
    _client = mock(Client.class);
    _provider = new RestLiSymbolTableProvider(_client, "d2://", 10, "Test", "https://Host:100/service");
    _nullServerNodeUriProvider = new RestLiSymbolTableProvider(_client, "d2://", 10, "Test", (String) null);
    _resourceDefinition = mock(ResourceDefinition.class);
    doAnswer(invocation -> {
        Set<DataSchema> schemas = (Set<DataSchema>) invocation.getArguments()[0];
        EnumDataSchema schema = new EnumDataSchema(new Name("TestEnum"));
        schema.setSymbols(Collections.unmodifiableList(Arrays.asList("Symbol1", "Symbol2")), new StringBuilder());
        schemas.add(schema);
        return null;
    }).when(_resourceDefinition).collectReferencedDataSchemas(any(Set.class));
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) Set(java.util.Set) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) ResourceDefinition(com.linkedin.restli.server.ResourceDefinition) Client(com.linkedin.r2.transport.common.Client) Name(com.linkedin.data.schema.Name) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)21 DataSchema (com.linkedin.data.schema.DataSchema)11 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)10 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)7 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)7 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)7 DataMap (com.linkedin.data.DataMap)6 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)6 MapDataSchema (com.linkedin.data.schema.MapDataSchema)6 Test (org.testng.annotations.Test)6 Name (com.linkedin.data.schema.Name)5 ByteString (com.linkedin.data.ByteString)3 DataList (com.linkedin.data.DataList)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)2 StringDataSchema (com.linkedin.data.schema.StringDataSchema)2 TyperefTest (com.linkedin.pegasus.generator.test.TyperefTest)2