use of com.linkedin.data.schema.DataSchema 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<String>(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.DataSchema in project rest.li by linkedin.
the class CustomAnyRecord method getValue.
public <T extends RecordTemplate> T getValue(Class<T> clazz) throws TemplateOutputCastException {
T result = null;
DataMap map = data();
DataSchema schema = DataTemplateUtil.getSchema(clazz);
String key = schema.getUnionMemberKey();
Object valueData = (map.size() == 1 ? map.get(key) : null);
if (valueData != null) {
if (_cachedValue != null && _cachedValue.data() == valueData && clazz.isInstance(_cachedValue)) {
@SuppressWarnings("unchecked") T value = (T) _cachedValue;
result = value;
} else {
result = DataTemplateUtil.wrap(valueData, schema, clazz);
_cachedValue = result;
}
}
return result;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class CustomAnyRecord method setValue.
public <T extends RecordTemplate> AnyRecord setValue(T value) {
DataSchema schema = value.schema();
String key = schema.getUnionMemberKey();
DataMap map = data();
map.clear();
map.put(key, value.data());
_cachedValue = value;
return this;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class CustomAnyRecord method getValue.
public <T extends RecordTemplate> T getValue(Class<T> clazz) throws TemplateOutputCastException {
T result = null;
DataMap map = data();
DataSchema schema = DataTemplateUtil.getSchema(clazz);
String key = schema.getUnionMemberKey();
Object valueData = (map.size() == 1 ? map.get(key) : null);
if (valueData != null) {
if (_cachedValue != null && _cachedValue.data() == valueData && clazz.isInstance(_cachedValue)) {
@SuppressWarnings("unchecked") T value = (T) _cachedValue;
result = value;
} else {
result = DataTemplateUtil.wrap(valueData, schema, clazz);
_cachedValue = result;
}
}
return result;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class CustomAnyRecord method setValue.
public <T extends RecordTemplate> AnyRecord setValue(T value) {
DataSchema schema = value.schema();
String key = schema.getUnionMemberKey();
DataMap map = data();
map.clear();
map.put(key, value.data());
_cachedValue = value;
return this;
}
Aggregations