use of com.linkedin.pegasus.generator.spec.CustomInfoSpec 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());
// Add just the "Array" suffix first. This is to ensure backwards compatibility with the old codegen logic.
String className = classInfo.name + ARRAY_SUFFIX;
// class name doesn't conflict with ancestor class names.
if (enclosingClass != null && classInfo.namespace.equals(enclosingClass.getFullName())) {
className = resolveInnerClassName(enclosingClass, className, ARRAY_SUFFIX);
}
classInfo.name = className;
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());
// Add just the "Map" suffix first. This is to ensure backwards compatibility with the old codegen logic.
String className = classInfo.name + MAP_SUFFIX;
// with ancestor class names.
if (enclosingClass != null && classInfo.namespace.equals(enclosingClass.getFullName())) {
className = resolveInnerClassName(enclosingClass, className, MAP_SUFFIX);
}
classInfo.name = className;
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 {
String className = resolveInnerClassName(enclosingClass, CodeUtil.capitalize(memberName), UNION_SUFFIX);
return new ClassInfo(enclosingClass.getFullName(), className);
}
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.pegasus.generator.spec.CustomInfoSpec 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<>(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.pegasus.generator.spec.CustomInfoSpec 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;
}
Aggregations