Search in sources :

Example 11 with NamedDataSchema

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

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

the class FileFormatDataSchemaParser method parseFile.

/**
   * Parse a source that specifies a file (not a fully qualified schema name).
   *
   * @param schemaSourceFile provides the source file.
   * @throws IOException if there is a file access error.
   */
private void parseFile(File schemaSourceFile, DataSchemaParser.ParseResult result) throws IOException {
    final DataSchemaLocation location = getSchemaLocation(schemaSourceFile);
    // if a the data schema has been resolved before, must skip parsing again, because one name can't be bound to two data schemas
    if (_schemaResolver.locationResolved(location)) {
        return;
    }
    final InputStream inputStream = new SchemaFileInputStream(schemaSourceFile);
    final List<DataSchema> schemas = parseSchemaStream(inputStream, location, result);
    for (DataSchema schema : schemas) {
        if (schema instanceof NamedDataSchema) {
            validateSchemaWithPath(schemaSourceFile.getAbsolutePath(), (NamedDataSchema) schema);
        }
        result.getSchemaAndLocations().put(schema, location);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InJarFileDataSchemaLocation(com.linkedin.data.schema.resolver.InJarFileDataSchemaLocation) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) FileDataSchemaLocation(com.linkedin.data.schema.resolver.FileDataSchemaLocation)

Example 13 with NamedDataSchema

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

the class TemplateSpecGenerator method processSchema.

private ClassTemplateSpec processSchema(DataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
    final CustomInfoSpec customInfo = getImmediateCustomInfo(schema);
    ClassTemplateSpec result = null;
    TyperefDataSchema originalTyperefSchema = null;
    while (schema.getType() == DataSchema.Type.TYPEREF) {
        final TyperefDataSchema typerefSchema = (TyperefDataSchema) schema;
        if (originalTyperefSchema == null) {
            originalTyperefSchema = typerefSchema;
        }
        final ClassTemplateSpec found = _schemaToClassMap.get(schema);
        schema = typerefSchema.getRef();
        if (found == null) {
            if (schema.getType() == DataSchema.Type.UNION) {
                result = generateUnion((UnionDataSchema) schema, typerefSchema);
                break;
            } else {
                generateTyperef(typerefSchema, originalTyperefSchema);
            }
        } else if (schema.getType() == DataSchema.Type.UNION) {
            result = found;
            break;
        }
    }
    if (result == null) {
        assert schema == schema.getDereferencedDataSchema();
        if (schema instanceof ComplexDataSchema) {
            final ClassTemplateSpec found = _schemaToClassMap.get(schema);
            if (found == null) {
                if (schema instanceof NamedDataSchema) {
                    result = generateNamedSchema((NamedDataSchema) schema);
                } else {
                    result = generateUnnamedComplexSchema(schema, enclosingClass, memberName);
                }
            } else {
                result = found;
            }
            if (customInfo != null) {
                result = customInfo.getCustomClass();
            }
        } else if (schema instanceof PrimitiveDataSchema) {
            result = (customInfo != null) ? customInfo.getCustomClass() : getPrimitiveClassForSchema((PrimitiveDataSchema) schema, enclosingClass, memberName);
        }
    }
    if (result == null) {
        throw unrecognizedSchemaType(enclosingClass, memberName, schema);
    }
    result.setOriginalTyperefSchema(originalTyperefSchema);
    return result;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) ComplexDataSchema(com.linkedin.data.schema.ComplexDataSchema)

Example 14 with NamedDataSchema

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

the class TemplateSpecGenerator method generateRecord.

private RecordTemplateSpec generateRecord(RecordDataSchema schema) {
    final RecordTemplateSpec recordClass = new RecordTemplateSpec(schema);
    recordClass.setNamespace(schema.getNamespace());
    recordClass.setPackage(schema.getPackage());
    recordClass.setClassName(schema.getName());
    recordClass.setModifiers(ModifierSpec.PUBLIC);
    registerClassTemplateSpec(schema, recordClass);
    // processSchema included schemas first, so that unnamed classes will belong to the defining class
    // instead of the current class
    final List<NamedDataSchema> includes = schema.getInclude();
    for (NamedDataSchema includedSchema : includes) {
        processSchema(includedSchema, null, null);
    }
    final Map<CustomInfoSpec, Object> customInfoMap = new IdentityHashMap<CustomInfoSpec, Object>(schema.getFields().size() * 2);
    for (RecordDataSchema.Field field : schema.getFields()) {
        final ClassTemplateSpec fieldClass = processSchema(field.getType(), recordClass, field.getName());
        final RecordTemplateSpec.Field newField = new RecordTemplateSpec.Field();
        newField.setSchemaField(field);
        newField.setType(fieldClass);
        newField.setDataClass(determineDataClass(field.getType(), recordClass, field.getName()));
        final CustomInfoSpec customInfo = getImmediateCustomInfo(field.getType());
        if (customInfo != null) {
            if (!customInfoMap.containsKey(customInfo)) {
                customInfoMap.put(customInfo, null);
            }
            newField.setCustomInfo(customInfo);
        }
        recordClass.addField(newField);
    }
    return recordClass;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) CustomInfoSpec(com.linkedin.pegasus.generator.spec.CustomInfoSpec) IdentityHashMap(java.util.IdentityHashMap) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec)

Example 15 with NamedDataSchema

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

the class RestLiJSONDocumentationRenderer method renderDataModel.

@Override
public void renderDataModel(String dataModelName, OutputStream out) {
    final NamedDataSchema schema = _relationships.getDataModels().get(dataModelName);
    if (schema == null) {
        throw new RoutingException(String.format("Data model named '%s' does not exist", dataModelName), 404);
    }
    final DataMap outputMap = createEmptyOutput();
    try {
        renderDataModel(schema, outputMap);
        _codec.writeMap(outputMap, out);
    } catch (IOException e) {
        throw new RestLiInternalException(e);
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) RoutingException(com.linkedin.restli.server.RoutingException) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Aggregations

NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)46 DataSchema (com.linkedin.data.schema.DataSchema)25 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)16 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)12 MapDataSchema (com.linkedin.data.schema.MapDataSchema)11 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)11 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)11 DataMap (com.linkedin.data.DataMap)8 IOException (java.io.IOException)8 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)7 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)7 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)6 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 SchemaParser (com.linkedin.data.schema.SchemaParser)6 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)5 File (java.io.File)5 Test (org.testng.annotations.Test)5 CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)4 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)4 FileInputStream (java.io.FileInputStream)4