use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class TestArray method testRecordArray.
@Test
public void testRecordArray() {
TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new ArrayTest(), "recordArray");
@SuppressWarnings("unchecked") Class<RecordBarArray> templateClass = (Class<RecordBarArray>) fieldInfo.getFieldClass();
ArrayDataSchema schema = (ArrayDataSchema) fieldInfo.getField().getType();
List<RecordBar> input = new ArrayList<RecordBar>();
for (int i = 0; i < 5; ++i) {
input.add(new RecordBar());
input.get(i).setLocation("input " + i);
}
List<RecordBar> adds = new ArrayList<RecordBar>();
for (int i = 0; i < 5; ++i) {
adds.add(new RecordBar());
input.get(i).setLocation("add " + i);
}
TestArrayTemplate.testArray(templateClass, schema, input, adds);
}
use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class TestArray method testRecordArray.
@Test
public void testRecordArray() {
TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new ArrayTest(), "recordArray");
@SuppressWarnings("unchecked") Class<RecordBarArray> templateClass = (Class<RecordBarArray>) fieldInfo.getFieldClass();
ArrayDataSchema schema = (ArrayDataSchema) fieldInfo.getField().getType();
List<RecordBar> input = new ArrayList<RecordBar>();
for (int i = 0; i < 5; ++i) {
input.add(new RecordBar());
input.get(i).setLocation("input " + i);
}
List<RecordBar> adds = new ArrayList<RecordBar>();
for (int i = 0; i < 5; ++i) {
adds.add(new RecordBar());
input.get(i).setLocation("add " + i);
}
TestArrayTemplate.testArray(templateClass, schema, input, adds);
}
use of com.linkedin.data.schema.ArrayDataSchema in project rest.li by linkedin.
the class TemplateSpecGenerator method generateArray.
private ArrayTemplateSpec generateArray(ArrayDataSchema schema, ClassTemplateSpec enclosingClass, String memberName) {
final DataSchema itemSchema = schema.getItems();
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(itemSchema, enclosingClass, memberName);
return (ArrayTemplateSpec) classInfo.existingClass;
}
final ArrayTemplateSpec arrayClass = (ArrayTemplateSpec) classInfo.definedClass;
registerClassTemplateSpec(schema, arrayClass);
arrayClass.setItemClass(processSchema(itemSchema, enclosingClass, memberName));
arrayClass.setItemDataClass(determineDataClass(itemSchema, enclosingClass, memberName));
final CustomInfoSpec customInfo = getImmediateCustomInfo(itemSchema);
arrayClass.setCustomInfo(customInfo);
return arrayClass;
}
use of com.linkedin.data.schema.ArrayDataSchema 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.ArrayDataSchema in project rest.li by linkedin.
the class ExampleRequestResponseGenerator method generateFieldDefValue.
private Object generateFieldDefValue(FieldDef<?> fieldDef) {
Object value = _dataGenerator.buildData(fieldDef.getName(), fieldDef.getDataSchema());
DataSchema dereferencedDataSchema = fieldDef.getDataSchema().getDereferencedDataSchema();
if (!dereferencedDataSchema.isPrimitive()) {
switch(dereferencedDataSchema.getType()) {
case FIXED:
value = new FixedTemplatePlaceholder(value, (FixedDataSchema) dereferencedDataSchema);
break;
case ENUM:
// just use the string value already generated. Will be coerced by DataTemplateUtil.DynamicEnumCoercer.
break;
case ARRAY:
value = new ArrayTemplatePlaceholder<Object>((DataList) value, (ArrayDataSchema) dereferencedDataSchema, Object.class);
break;
case RECORD:
value = new RecordTemplatePlaceholder((DataMap) value, (RecordDataSchema) dereferencedDataSchema);
break;
case MAP:
value = new MapTemplatePlaceholder<Object>((DataMap) value, (MapDataSchema) dereferencedDataSchema, Object.class);
break;
case UNION:
value = new UnionTemplatePlaceholder(value, (UnionDataSchema) dereferencedDataSchema);
break;
case TYPEREF:
throw new IllegalStateException("TYPEREF should not be returned for a dereferenced byte. schema: " + fieldDef.getDataSchema());
default:
throw new IllegalStateException("Unrecognized enum value: " + dereferencedDataSchema.getType());
}
}
return value;
}
Aggregations