use of com.linkedin.pegasus.generator.spec.RecordTemplateSpec 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.pegasus.generator.spec.RecordTemplateSpec in project rest.li by linkedin.
the class JavaDataTemplateGenerator method defineClass.
private JDefinedClass defineClass(ClassTemplateSpec classTemplateSpec) throws JClassAlreadyExistsException {
JDefinedClass result = _definedClasses.get(classTemplateSpec);
if (result == null) {
final int jmodValue = getJModValue(classTemplateSpec.getModifiers());
final JClassContainer container;
if (classTemplateSpec.getEnclosingClass() == null) {
container = getPackage(classTemplateSpec.getPackage());
} else {
container = defineClass(classTemplateSpec.getEnclosingClass());
}
if (classTemplateSpec instanceof ArrayTemplateSpec || classTemplateSpec instanceof FixedTemplateSpec || classTemplateSpec instanceof MapTemplateSpec || classTemplateSpec instanceof RecordTemplateSpec || classTemplateSpec instanceof TyperefTemplateSpec || classTemplateSpec instanceof UnionTemplateSpec) {
result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()));
} else if (classTemplateSpec instanceof EnumTemplateSpec) {
result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()), ClassType.ENUM);
} else {
throw new RuntimeException();
}
_definedClasses.put(classTemplateSpec, result);
}
return result;
}
use of com.linkedin.pegasus.generator.spec.RecordTemplateSpec in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateRecord.
protected void generateRecord(JDefinedClass templateClass, RecordTemplateSpec recordSpec) throws JClassAlreadyExistsException {
templateClass.javadoc().append(recordSpec.getSchema().getDoc());
setDeprecatedAnnotationAndJavadoc(recordSpec.getSchema(), templateClass);
extendRecordBaseClass(templateClass);
if (_pathSpecMethods) {
generatePathSpecMethodsForRecord(recordSpec.getFields(), templateClass);
}
final JFieldVar schemaFieldVar = generateSchemaField(templateClass, recordSpec.getSchema());
generateConstructorWithNoArg(templateClass, schemaFieldVar, _dataMapClass);
generateConstructorWithArg(templateClass, schemaFieldVar, _dataMapClass);
for (RecordTemplateSpec.Field field : recordSpec.getFields()) {
generateRecordFieldAccessors(templateClass, field, generate(field.getType()), schemaFieldVar);
}
recordSpec.getFields().stream().map(RecordTemplateSpec.Field::getCustomInfo).distinct().forEach(customInfo -> generateCustomClassInitialization(templateClass, customInfo));
if (_copierMethods) {
generateCopierMethods(templateClass);
}
}
use of com.linkedin.pegasus.generator.spec.RecordTemplateSpec in project rest.li by linkedin.
the class TestTemplateSpecGenerator method testCustomInfoForRecordFields.
@Test(dataProvider = "customTypeDataForRecord")
public void testCustomInfoForRecordFields(final List<DataSchema> customTypedSchemas) {
final List<RecordDataSchema.Field> fields = customTypedSchemas.stream().map(RecordDataSchema.Field::new).peek(field -> field.setName("field_" + _uniqueNumberGenerator.getAndIncrement(), null)).collect(Collectors.toList());
final RecordDataSchema record = new RecordDataSchema(new Name(INPUT_SCHEMA_NAME), RecordDataSchema.RecordType.RECORD);
record.setFields(fields, null);
final TemplateSpecGenerator generator = new TemplateSpecGenerator(_resolver);
final RecordTemplateSpec spec = (RecordTemplateSpec) generator.generate(record, _location);
for (int i = 0; i < customTypedSchemas.size(); ++i) {
Assert.assertNotNull(spec.getFields().get(i).getCustomInfo());
Assert.assertEquals(spec.getFields().get(i).getCustomInfo().getCustomClass().getClassName(), CustomTypeUtil.getJavaCustomTypeClassNameFromSchema((TyperefDataSchema) customTypedSchemas.get(i)));
}
}
Aggregations