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);
}
}
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);
}
}
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;
}
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;
}
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);
}
}
Aggregations