use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec in project rest.li by linkedin.
the class TemplateSpecGenerator method generateNamedSchema.
private ClassTemplateSpec generateNamedSchema(NamedDataSchema schema) {
pushCurrentLocation(_schemaResolver.nameToDataSchemaLocations().get(schema.getFullName()));
// make sure no duplicate template spec classname which should be binding name of the schema
final String className = schema.getBindingName();
checkForClassNameConflict(className, schema);
final ClassTemplateSpec templateClass;
switch(schema.getType()) {
case RECORD:
templateClass = generateRecord((RecordDataSchema) schema);
break;
case ENUM:
templateClass = generateEnum((EnumDataSchema) schema);
break;
case FIXED:
templateClass = generateFixed((FixedDataSchema) schema);
break;
default:
throw unrecognizedSchemaType(null, null, schema);
}
popCurrentLocation();
return templateClass;
}
use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec in project rest.li by linkedin.
the class TemplateSpecGenerator method determineDataClass.
private ClassTemplateSpec determineDataClass(DataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
final ClassTemplateSpec result;
final DataSchema dereferencedSchema = schema.getDereferencedDataSchema();
if (dereferencedSchema.getType() == DataSchema.Type.ENUM) {
result = PrimitiveTemplateSpec.getInstance(DataSchema.Type.STRING);
} else if (CodeUtil.isDirectType(dereferencedSchema)) {
result = getPrimitiveClassForSchema((PrimitiveDataSchema) dereferencedSchema, enclosingClass, memberName);
} else {
result = null;
}
return result;
}
use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec 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.pegasus.generator.spec.ClassTemplateSpec in project rest.li by linkedin.
the class TemplateSpecGenerator method getCustomClasses.
public static CustomClasses getCustomClasses(DataSchema schema, String customTypeLanguage) {
CustomClasses customClasses = null;
final Map<String, Object> properties = schema.getProperties();
if (customTypeLanguage != null) {
final Object java = properties.get(customTypeLanguage);
if (java != null) {
if (java.getClass() != DataMap.class) {
throw new IllegalArgumentException(schema + " has \"" + customTypeLanguage + "\" property that is not a DataMap");
}
final DataMap map = (DataMap) java;
final Object custom = map.get(CustomTypeUtil.CLASS_PROPERTY);
if (custom != null) {
if (custom.getClass() != String.class) {
throw new IllegalArgumentException(schema + " has \"" + customTypeLanguage + "\" property with \"class\" that is not a string");
}
// a custom class specification has been found
customClasses = new CustomClasses();
customClasses.customClass = new ClassTemplateSpec();
customClasses.customClass.setFullName((String) custom);
if (!allowCustomClass(schema)) {
throw new IllegalArgumentException(schema + " cannot have custom class binding");
}
}
// check for coercer class
final Object coercerClass = map.get(CustomTypeUtil.COERCER_CLASS_PROPERTY);
if (coercerClass != null) {
if (coercerClass.getClass() != String.class) {
throw new IllegalArgumentException(schema + " has \"" + customTypeLanguage + "\" property with \"coercerClass\" that is not a string");
}
if (customClasses == null) {
throw new IllegalArgumentException(schema + " has \"" + customTypeLanguage + "\" property with \"coercerClass\" but does not have \"class\" property");
}
// a custom class specification has been found
customClasses.customCoercerClass = new ClassTemplateSpec();
customClasses.customCoercerClass.setFullName((String) coercerClass);
}
}
}
return customClasses;
}
use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec 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;
}
Aggregations