use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class RuntimeSymbolTableGenerator method expandAndCollectSymbols.
public static void expandAndCollectSymbols(DataSchema resourceSchema, Set<DataSchema> processedSchemas, Set<String> symbols) {
if (resourceSchema instanceof TyperefDataSchema) {
TyperefDataSchema typerefDataSchema = (TyperefDataSchema) resourceSchema;
expandAndCollectSymbols(typerefDataSchema.getDereferencedDataSchema(), processedSchemas, symbols);
return;
} else if (resourceSchema instanceof ArrayDataSchema) {
ArrayDataSchema arrayDataSchema = (ArrayDataSchema) resourceSchema;
expandAndCollectSymbols(arrayDataSchema.getItems(), processedSchemas, symbols);
return;
} else if (resourceSchema instanceof MapDataSchema) {
MapDataSchema mapDataSchema = (MapDataSchema) resourceSchema;
expandAndCollectSymbols(mapDataSchema.getValues(), processedSchemas, symbols);
return;
}
if (processedSchemas.contains(resourceSchema)) {
return;
}
processedSchemas.add(resourceSchema);
if (resourceSchema instanceof RecordDataSchema) {
RecordDataSchema recordDataSchema = (RecordDataSchema) resourceSchema;
for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
symbols.add(field.getName());
expandAndCollectSymbols(field.getType(), processedSchemas, symbols);
}
} else if (resourceSchema instanceof UnionDataSchema) {
UnionDataSchema unionDataSchema = (UnionDataSchema) resourceSchema;
for (UnionDataSchema.Member member : unionDataSchema.getMembers()) {
symbols.add(member.getUnionMemberKey());
expandAndCollectSymbols(member.getType(), processedSchemas, symbols);
}
} else if (resourceSchema instanceof EnumDataSchema) {
EnumDataSchema enumDataSchema = (EnumDataSchema) resourceSchema;
symbols.addAll(enumDataSchema.getSymbols());
}
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class TestSchemaSampleDataGenerator method testEnumSchema.
@Test
public void testEnumSchema() {
final EnumDataSchema schema = (EnumDataSchema) DataTemplateUtil.getSchema(EnumFruits.class);
final String value = (String) SchemaSampleDataGenerator.buildData(schema, _spec);
Assert.assertSame(schema.getSymbolDocs().size(), EnumFruits.class.getEnumConstants().length - 1);
EnumFruits.valueOf(value);
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class TestSchemaSampleDataGenerator method testEmptyEnumSchema.
@Test
public void testEmptyEnumSchema() {
final EnumDataSchema schema = (EnumDataSchema) DataTemplateUtil.getSchema(EnumEmpty.class);
final String value = (String) SchemaSampleDataGenerator.buildData(schema, _spec);
Assert.assertSame(schema.getSymbols().size(), EnumEmpty.class.getEnumConstants().length - 1);
Assert.assertEquals("EmptyEnum", value);
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class TestEnum method testEnum.
private static <T extends Enum<T>> void testEnum(Class<T> enumClass) {
try {
assertTrue(Enum.class.isAssignableFrom(enumClass));
// has embedded EnumDataSchema
DataSchema schema = DataTemplateUtil.getSchema(enumClass);
assertNotNull(schema);
assertTrue(schema instanceof EnumDataSchema);
// get symbols
EnumDataSchema enumSchema = (EnumDataSchema) schema;
Set<String> schemaSymbols = new HashSet<>(enumSchema.getSymbols());
assertNotNull(schemaSymbols);
for (String symbol : schemaSymbols) {
// IllegalArgumentException thrown if not valid symbol
Enum.valueOf(enumClass, symbol);
}
// IllegalArgumentException thrown if not valid symbol
Enum.valueOf(enumClass, "$UNKNOWN");
} catch (Exception exc) {
fail("Unexpected exception", exc);
}
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class TestEnum method testEnumProperties.
@Test
public void testEnumProperties() {
EnumDataSchema enumPropertiesSchema = EnumProperties.dataSchema();
assertNotNull(enumPropertiesSchema);
Map<String, Object> appleProps = enumPropertiesSchema.getSymbolProperties(EnumProperties.APPLE.name());
assertNotNull(appleProps);
assertEquals(appleProps.get("color"), "red");
assertNull(enumPropertiesSchema.getSymbolProperties("unknown"));
// Fruits.pdl enum doesn't have symbol properties.
EnumDataSchema fruitsSchema = Fruits.dataSchema();
assertNotNull(fruitsSchema);
appleProps = fruitsSchema.getSymbolProperties(Fruits.APPLE.name());
assertNotNull(appleProps);
assertTrue(appleProps.isEmpty());
}
Aggregations