Search in sources :

Example 6 with PrimitiveDataSchema

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

the class TemplateSpecGenerator method classInfoForUnnamed.

/*
   * Determine name and class for unnamed types.
   */
private ClassInfo classInfoForUnnamed(ClassTemplateSpec enclosingClass, String name, DataSchema schema) {
    assert !(schema instanceof NamedDataSchema);
    assert !(schema instanceof PrimitiveDataSchema);
    final ClassInfo classInfo = classNameForUnnamedTraverse(enclosingClass, name, schema);
    final String className = classInfo.bindingName();
    final DataSchema schemaFromClassName = _classNameToSchemaMap.get(className);
    if (schemaFromClassName == null) {
        final ClassTemplateSpec classTemplateSpec = ClassTemplateSpec.createFromDataSchema(schema);
        if (enclosingClass != null && classInfo.namespace.equals(enclosingClass.getFullName())) {
            // enclosingClass flag indicates whether a class is nested or not.
            classTemplateSpec.setEnclosingClass(enclosingClass);
            classTemplateSpec.setClassName(classInfo.name);
            classTemplateSpec.setModifiers(ModifierSpec.PUBLIC, ModifierSpec.STATIC);
        } else {
            classTemplateSpec.setNamespace(classInfo.namespace);
            classTemplateSpec.setClassName(classInfo.name);
            classTemplateSpec.setPackage(classInfo.packageName);
            classTemplateSpec.setModifiers(ModifierSpec.PUBLIC);
        }
        classInfo.definedClass = classTemplateSpec;
    } else {
        checkForClassNameConflict(className, schema);
        classInfo.existingClass = _schemaToClassMap.get(schemaFromClassName);
    }
    return classInfo;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec)

Example 7 with PrimitiveDataSchema

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

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

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

the class TestSchemaSampleDataGenerator method testMapSchema.

@Test
public void testMapSchema() {
    for (Map.Entry<DataSchema.Type, Class<? extends DirectMapTemplate<?>>> entry : _dataSchemaTypeToprimitiveMapMap.entrySet()) {
        final PrimitiveDataSchema valueSchema = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchema(entry.getKey());
        final MapDataSchema mapSchema = new MapDataSchema(valueSchema);
        final DataMap value = (DataMap) SchemaSampleDataGenerator.buildData(mapSchema, _spec);
        final ParameterizedType mapType = (ParameterizedType) entry.getValue().getGenericSuperclass();
        assert (mapType.getRawType() == DirectMapTemplate.class);
        Assert.assertSame(value.values().iterator().next().getClass(), mapType.getActualTypeArguments()[0]);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) DirectMapTemplate(com.linkedin.data.template.DirectMapTemplate) BooleanMap(com.linkedin.data.template.BooleanMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) DoubleMap(com.linkedin.data.template.DoubleMap) BytesMap(com.linkedin.data.template.BytesMap) FloatMap(com.linkedin.data.template.FloatMap) IntegerMap(com.linkedin.data.template.IntegerMap) DataMap(com.linkedin.data.DataMap) StringMap(com.linkedin.data.template.StringMap) LongMap(com.linkedin.data.template.LongMap) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 10 with PrimitiveDataSchema

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

the class ResourceModelEncoder method buildDataSchemaType.

/*package*/
static String buildDataSchemaType(DataSchema schema) {
    if (schema instanceof PrimitiveDataSchema || schema instanceof NamedDataSchema) {
        return schema.getUnionMemberKey();
    }
    JsonBuilder builder = null;
    try {
        builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
        final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
        encoder.encode(schema);
        return builder.result();
    } catch (IOException e) {
        throw new RestLiInternalException("could not encode schema for '" + schema.toString() + "'", e);
    } finally {
        if (builder != null) {
            builder.closeQuietly();
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) IOException(java.io.IOException)

Aggregations

PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)10 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)6 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)5 MapDataSchema (com.linkedin.data.schema.MapDataSchema)5 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)5 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)5 DataSchema (com.linkedin.data.schema.DataSchema)4 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)4 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)4 Test (org.testng.annotations.Test)4 DataMap (com.linkedin.data.DataMap)3 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)3 BooleanMap (com.linkedin.data.template.BooleanMap)3 BytesMap (com.linkedin.data.template.BytesMap)3 DoubleMap (com.linkedin.data.template.DoubleMap)3 FloatMap (com.linkedin.data.template.FloatMap)3 IntegerMap (com.linkedin.data.template.IntegerMap)3 LongMap (com.linkedin.data.template.LongMap)3 StringMap (com.linkedin.data.template.StringMap)3