use of com.linkedin.data.schema.MapDataSchema 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.MapDataSchema 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;
}
use of com.linkedin.data.schema.MapDataSchema in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateMap.
protected void generateMap(JDefinedClass mapClass, MapTemplateSpec mapSpec) throws JClassAlreadyExistsException {
final JClass valueJClass = generate(mapSpec.getValueClass());
final JClass dataJClass = generate(mapSpec.getValueDataClass());
if (CodeUtil.isDirectType(mapSpec.getSchema().getValues())) {
mapClass._extends(_directMapBaseClass.narrow(valueJClass));
} else {
extendWrappingMapBaseClass(valueJClass, mapClass);
}
final DataSchema bareSchema = new MapDataSchema(schemaForArrayItemsOrMapValues(mapSpec.getCustomInfo(), mapSpec.getSchema().getValues()));
final JVar schemaField = generateSchemaField(mapClass, bareSchema);
generateConstructorWithNoArg(mapClass, _dataMapClass);
generateConstructorWithInitialCapacity(mapClass, _dataMapClass);
generateConstructorWithInitialCapacityAndLoadFactor(mapClass);
generateConstructorWithMap(mapClass, valueJClass);
generateConstructorWithArg(mapClass, schemaField, _dataMapClass, valueJClass, dataJClass);
if (_pathSpecMethods) {
generatePathSpecMethodsForCollection(mapClass, mapSpec.getSchema(), valueJClass, "values");
}
generateCustomClassInitialization(mapClass, mapSpec.getCustomInfo());
if (_copierMethods) {
generateCopierMethods(mapClass);
}
}
use of com.linkedin.data.schema.MapDataSchema 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;
}
use of com.linkedin.data.schema.MapDataSchema in project rest.li by linkedin.
the class SnapshotGenerator method recordType.
private void recordType(DataSchema schema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
if (schema instanceof NamedDataSchema) {
NamedDataSchema namedDataSchema = (NamedDataSchema) schema;
if (!foundTypes.containsKey(namedDataSchema.getFullName())) {
foundTypes.put(namedDataSchema.getFullName(), namedDataSchema);
if (// recurse into record, record any contained types.
schema instanceof RecordDataSchema) {
RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
for (NamedDataSchema includedSchema : recordDataSchema.getInclude()) {
recordType(includedSchema, foundTypes, typeOrder);
}
for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
recordType(field.getType(), foundTypes, typeOrder);
}
} else if (schema instanceof TyperefDataSchema) {
recordType(schema.getDereferencedDataSchema(), foundTypes, typeOrder);
}
typeOrder.add(namedDataSchema);
}
} else if (schema instanceof ArrayDataSchema) {
ArrayDataSchema arraySchema = (ArrayDataSchema) schema;
recordType(arraySchema.getItems(), foundTypes, typeOrder);
} else if (schema instanceof MapDataSchema) {
MapDataSchema mapSchema = (MapDataSchema) schema;
recordType(mapSchema.getValues(), foundTypes, typeOrder);
} else if (schema instanceof UnionDataSchema) {
UnionDataSchema unionSchema = (UnionDataSchema) schema;
for (DataSchema type : unionSchema.getTypes()) {
recordType(type, foundTypes, typeOrder);
}
}
}
Aggregations