Search in sources :

Example 6 with MapDataSchema

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

the class TemplateSpecGenerator method classNameForUnnamedTraverse.

private ClassInfo classNameForUnnamedTraverse(ClassTemplateSpec enclosingClass, String memberName, DataSchema schema) {
    final DataSchema dereferencedDataSchema = schema.getDereferencedDataSchema();
    switch(dereferencedDataSchema.getType()) {
        case ARRAY:
            final ArrayDataSchema arraySchema = (ArrayDataSchema) dereferencedDataSchema;
            CustomInfoSpec customInfo = getImmediateCustomInfo(arraySchema.getItems());
            if (customInfo != null) {
                return new ClassInfo(customInfo.getCustomSchema().getNamespace(), customInfo.getCustomSchema().getName() + ARRAY_SUFFIX, customInfo.getCustomSchema().getPackage());
            } else {
                final ClassInfo classInfo = classNameForUnnamedTraverse(enclosingClass, memberName, arraySchema.getItems());
                classInfo.name += ARRAY_SUFFIX;
                return classInfo;
            }
        case MAP:
            final MapDataSchema mapSchema = (MapDataSchema) dereferencedDataSchema;
            customInfo = getImmediateCustomInfo(mapSchema.getValues());
            if (customInfo != null) {
                return new ClassInfo(customInfo.getCustomSchema().getNamespace(), customInfo.getCustomSchema().getName() + MAP_SUFFIX, customInfo.getCustomSchema().getPackage());
            } else {
                final ClassInfo classInfo = classNameForUnnamedTraverse(enclosingClass, memberName, mapSchema.getValues());
                classInfo.name += MAP_SUFFIX;
                return classInfo;
            }
        case UNION:
            if (schema.getType() == DataSchema.Type.TYPEREF) {
                DataSchema referencedDataSchema;
                TyperefDataSchema typerefDataSchema = (TyperefDataSchema) schema;
                while ((referencedDataSchema = typerefDataSchema.getDereferencedDataSchema()) != dereferencedDataSchema) {
                    typerefDataSchema = (TyperefDataSchema) referencedDataSchema;
                }
                return new ClassInfo(typerefDataSchema.getNamespace(), CodeUtil.capitalize(typerefDataSchema.getName()), typerefDataSchema.getPackage());
            } else {
                return new ClassInfo(enclosingClass.getFullName(), CodeUtil.capitalize(memberName));
            }
        case FIXED:
        case RECORD:
        case ENUM:
            final NamedDataSchema namedSchema = (NamedDataSchema) dereferencedDataSchema;
            // carry package override information for named schema.
            return new ClassInfo(namedSchema.getNamespace(), CodeUtil.capitalize(namedSchema.getName()), namedSchema.getPackage());
        case BOOLEAN:
            return new ClassInfo(_templatePackageName, "Boolean");
        case INT:
            return new ClassInfo(_templatePackageName, "Integer");
        case LONG:
            return new ClassInfo(_templatePackageName, "Long");
        case FLOAT:
            return new ClassInfo(_templatePackageName, "Float");
        case DOUBLE:
            return new ClassInfo(_templatePackageName, "Double");
        case STRING:
            return new ClassInfo(_templatePackageName, "String");
        case BYTES:
            return new ClassInfo(_templatePackageName, "ByteString");
        case NULL:
            throw nullTypeNotAllowed(enclosingClass, memberName);
        default:
            throw unrecognizedSchemaType(enclosingClass, memberName, dereferencedDataSchema);
    }
}
Also used : 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) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) MapDataSchema(com.linkedin.data.schema.MapDataSchema)

Example 7 with MapDataSchema

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

the class TemplateSpecGenerator method generateMap.

private MapTemplateSpec generateMap(MapDataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
    final DataSchema valueSchema = schema.getValues();
    final ClassInfo classInfo = classInfoForUnnamed(enclosingClass, memberName, schema);
    if (classInfo.existingClass != null) {
        /* When type refs are used as item types inside some unnamed complex schemas like map and array,
       * the type refs are de-referenced and the underlying real type is used in the generated class.
       * In those cases the type refs are not processed by the class generation logic, an explicit
       * schema processing is necessary in order to processSchema the data template classes for those type
       * refs.
       */
        processSchema(valueSchema, enclosingClass, memberName);
        return (MapTemplateSpec) classInfo.existingClass;
    }
    final MapTemplateSpec mapClass = (MapTemplateSpec) classInfo.definedClass;
    registerClassTemplateSpec(schema, mapClass);
    mapClass.setValueClass(processSchema(valueSchema, enclosingClass, memberName));
    mapClass.setValueDataClass(determineDataClass(valueSchema, enclosingClass, memberName));
    final CustomInfoSpec customInfo = getImmediateCustomInfo(valueSchema);
    mapClass.setCustomInfo(customInfo);
    return mapClass;
}
Also used : 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) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) MapTemplateSpec(com.linkedin.pegasus.generator.spec.MapTemplateSpec)

Example 8 with MapDataSchema

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

the class JavaDataTemplateGenerator method generateMap.

protected void generateMap(JDefinedClass mapClass, MapTemplateSpec mapSpec) throws JClassAlreadyExistsException {
    final JClass valueJClass = generate(mapSpec.getValueClass());
    final JClass dataJClass = generate(mapSpec.getValueDataClass());
    if (CodeUtil.isDirectType(mapSpec.getSchema().getValues())) {
        mapClass._extends(_directMapBaseClass.narrow(valueJClass));
    } else {
        extendWrappingMapBaseClass(valueJClass, mapClass);
    }
    final DataSchema bareSchema = new MapDataSchema(schemaForArrayItemsOrMapValues(mapSpec.getCustomInfo(), mapSpec.getSchema().getValues()));
    final JVar schemaField = generateSchemaField(mapClass, bareSchema);
    generateConstructorWithNoArg(mapClass, _dataMapClass);
    generateConstructorWithInitialCapacity(mapClass, _dataMapClass);
    generateConstructorWithInitialCapacityAndLoadFactor(mapClass);
    generateConstructorWithMap(mapClass, valueJClass);
    generateConstructorWithArg(mapClass, schemaField, _dataMapClass, valueJClass, dataJClass);
    if (_pathSpecMethods) {
        generatePathSpecMethodsForCollection(mapClass, mapSpec.getSchema(), valueJClass, "values");
    }
    generateCustomClassInitialization(mapClass, mapSpec.getCustomInfo());
    if (_copierMethods) {
        generateCopierMethods(mapClass);
    }
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) JClass(com.sun.codemodel.JClass) JVar(com.sun.codemodel.JVar)

Example 9 with MapDataSchema

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

use of com.linkedin.data.schema.MapDataSchema 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)

Aggregations

MapDataSchema (com.linkedin.data.schema.MapDataSchema)30 Test (org.testng.annotations.Test)17 DataMap (com.linkedin.data.DataMap)13 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)12 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)11 DataSchema (com.linkedin.data.schema.DataSchema)10 ByteString (com.linkedin.data.ByteString)9 DataList (com.linkedin.data.DataList)9 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)9 HashMap (java.util.HashMap)9 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)7 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)7 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)6 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)4 TestDataTemplateUtil (com.linkedin.data.template.TestDataTemplateUtil)4 Map (java.util.Map)3 TestUtil.asMap (com.linkedin.data.TestUtil.asMap)2 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)2 IntegerMap (com.linkedin.data.template.IntegerMap)2