use of com.linkedin.data.schema.PrimitiveDataSchema 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 (schema.getType() == DataSchema.Type.UNION) {
result = (found != null) ? found : generateUnion((UnionDataSchema) schema, typerefSchema);
break;
} else if (found == null) {
generateTyperef(typerefSchema, originalTyperefSchema);
}
}
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.data.schema.PrimitiveDataSchema 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.data.schema.PrimitiveDataSchema in project rest.li by linkedin.
the class TestSchemaSampleDataGenerator method testArraySchema.
@Test
public void testArraySchema() {
for (Map.Entry<DataSchema.Type, Class<? extends DirectArrayTemplate<?>>> entry : _dataSchemaTypeToprimitiveArrayMap.entrySet()) {
final PrimitiveDataSchema itemsSchema = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchema(entry.getKey());
final ArrayDataSchema arraySchema = new ArrayDataSchema(itemsSchema);
final DataList value = (DataList) SchemaSampleDataGenerator.buildData(arraySchema, _spec);
final ParameterizedType arrayType = (ParameterizedType) entry.getValue().getGenericSuperclass();
assert (arrayType.getRawType() == DirectArrayTemplate.class);
Assert.assertSame(value.get(0).getClass(), arrayType.getActualTypeArguments()[0]);
}
}
use of com.linkedin.data.schema.PrimitiveDataSchema in project rest.li by linkedin.
the class TestSchemaSampleDataGenerator method testPrimitiveSchema.
@Test
public void testPrimitiveSchema() {
for (Map.Entry<DataSchema.Type, Class<?>> entry : _dataSchemaTypeToPrimitiveJavaTypeMap.entrySet()) {
final PrimitiveDataSchema schema = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchema(entry.getKey());
final Object value = SchemaSampleDataGenerator.buildData(schema, _spec);
Assert.assertSame(value.getClass(), entry.getValue());
}
final PrimitiveDataSchema nullSchema = DataSchemaConstants.NULL_DATA_SCHEMA;
final Object nullData = SchemaSampleDataGenerator.buildData(nullSchema, _spec);
Assert.assertEquals(nullData, Data.NULL);
}
use of com.linkedin.data.schema.PrimitiveDataSchema in project rest.li by linkedin.
the class ResourceModelEncoder method buildDataSchemaType.
private static String buildDataSchemaType(final Class<?> type, final DataSchema dataSchema) {
final DataSchema schemaToEncode;
if (dataSchema instanceof TyperefDataSchema) {
return ((TyperefDataSchema) dataSchema).getFullName();
} else if (dataSchema instanceof PrimitiveDataSchema || dataSchema instanceof NamedDataSchema) {
return dataSchema.getUnionMemberKey();
} else if (dataSchema instanceof UnionDataSchema && HasTyperefInfo.class.isAssignableFrom(type)) {
final TyperefInfo unionRef = DataTemplateUtil.getTyperefInfo(type.asSubclass(DataTemplate.class));
return unionRef.getSchema().getFullName();
} else {
schemaToEncode = dataSchema;
}
JsonBuilder builder = null;
try {
builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
encoder.encode(schemaToEncode);
return builder.result();
} catch (IOException e) {
throw new RestLiInternalException("could not encode schema for '" + type.getName() + "'", e);
} finally {
if (builder != null) {
builder.closeQuietly();
}
}
}
Aggregations