Search in sources :

Example 56 with DataSchema

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);
    }
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) HashSet(java.util.HashSet)

Example 57 with DataSchema

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;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) DataMap(com.linkedin.data.DataMap)

Example 58 with DataSchema

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;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) DataMap(com.linkedin.data.DataMap)

Example 59 with DataSchema

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;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) DataMap(com.linkedin.data.DataMap)

Example 60 with DataSchema

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;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) DataMap(com.linkedin.data.DataMap)

Aggregations

DataSchema (com.linkedin.data.schema.DataSchema)131 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)82 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)53 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)48 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)44 DataMap (com.linkedin.data.DataMap)43 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)41 MapDataSchema (com.linkedin.data.schema.MapDataSchema)40 Test (org.testng.annotations.Test)37 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)36 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)24 ByteString (com.linkedin.data.ByteString)15 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)15 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)14 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)14 ArrayList (java.util.ArrayList)12 DataList (com.linkedin.data.DataList)11 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)9 SchemaParser (com.linkedin.data.schema.SchemaParser)9 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)9