Search in sources :

Example 6 with FixedDataSchema

use of com.linkedin.data.schema.FixedDataSchema in project rest.li by linkedin.

the class SchemaSampleDataGenerator method buildData.

private static Object buildData(ParentSchemas parentSchemas, DataSchema schema, String fieldName, DataGenerationOptions spec) {
    spec = preventRecursionIntoAlreadyTraversedSchemas(parentSchemas, spec, schema);
    parentSchemas.incrementReferences(schema);
    final DataSchema derefSchema = schema.getDereferencedDataSchema();
    final SampleDataCallback callback = spec.getCallback();
    Object data = null;
    switch(derefSchema.getType()) {
        case BOOLEAN:
            data = callback.getBoolean(fieldName);
            break;
        case INT:
            data = callback.getInteger(fieldName);
            break;
        case LONG:
            data = callback.getLong(fieldName);
            break;
        case FLOAT:
            data = callback.getFloat(fieldName);
            break;
        case DOUBLE:
            data = callback.getDouble(fieldName);
            break;
        case BYTES:
            data = callback.getBytes(fieldName);
            break;
        case STRING:
            data = callback.getString(fieldName);
            break;
        case NULL:
            data = Data.NULL;
            break;
        case FIXED:
            data = callback.getFixed(fieldName, (FixedDataSchema) derefSchema);
            break;
        case ENUM:
            data = callback.getEnum(fieldName, (EnumDataSchema) derefSchema);
            break;
        case ARRAY:
            final DataList dataList = new DataList(spec.getArraySize());
            for (int i = 0; i < spec.getArraySize(); i++) {
                final Object item = buildData(parentSchemas, ((ArrayDataSchema) derefSchema).getItems(), fieldName, spec);
                dataList.add(item);
            }
            data = dataList;
            break;
        case RECORD:
            data = buildRecordData(parentSchemas, (RecordDataSchema) derefSchema, spec);
            break;
        case MAP:
            final DataMap dataMap = new DataMap();
            for (int i = 0; i < spec.getArraySize(); i++) {
                final Object item = buildData(parentSchemas, ((MapDataSchema) derefSchema).getValues(), fieldName, spec);
                dataMap.put("mapField_" + _random.nextInt(), item);
            }
            data = dataMap;
            break;
        case UNION:
            final UnionDataSchema unionSchema = (UnionDataSchema) derefSchema;
            final List<DataSchema> types = removeAlreadyTraversedSchemasFromUnionMemberList(parentSchemas, unionSchema.getTypes());
            final int unionIndex = _random.nextInt(types.size());
            final DataSchema unionItemSchema = types.get(unionIndex);
            data = buildData(parentSchemas, unionItemSchema, fieldName, spec);
            if (data != null) {
                final DataMap unionMap = new DataMap();
                unionMap.put(unionItemSchema.getUnionMemberKey(), data);
                data = unionMap;
            }
            break;
        case TYPEREF:
            data = buildData(parentSchemas, derefSchema, fieldName, spec);
            break;
    }
    parentSchemas.decrementReferences(schema);
    return data;
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Example 7 with FixedDataSchema

use of com.linkedin.data.schema.FixedDataSchema in project rest.li by linkedin.

the class TestFixed method testFixed.

private <T extends FixedTemplate> void testFixed(Class<T> fixedClass) {
    try {
        // check for ByteString constructor
        Constructor<T> byteStringConstructor = fixedClass.getConstructor(ByteString.class);
        // check for Object constructor
        Constructor<T> objectConstructor = fixedClass.getConstructor(Object.class);
        // has embedded FixedDataSchema
        FixedDataSchema schema = (FixedDataSchema) DataTemplateUtil.getSchema(fixedClass);
        // get size of fixed
        int size = schema.getSize();
        // create input value
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append((char) ('a' + i % 26));
        }
        String stringValue = sb.toString();
        ByteString byteStringValue = ByteString.copy(stringValue.getBytes(Data.UTF_8_CHARSET));
        // Object ctor, value is String
        T fixed = objectConstructor.newInstance(stringValue);
        assertEquals(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // Object ctor, value is ByteString
        fixed = objectConstructor.newInstance(byteStringValue);
        assertSame(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // ByteString ctor
        fixed = byteStringConstructor.newInstance(byteStringValue);
        assertSame(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // schema()
        assertSame(fixed.schema(), schema);
        // toString()
        assertEquals(fixed.toString(), byteStringValue.toString());
        // check for clone and copy override with correct return type
        TestDataTemplateUtil.assertCloneAndCopyReturnType(fixedClass);
        // test clone
        FixedTemplate fixedClone = fixed.clone();
        assertSame(fixedClone.getClass(), fixed.getClass());
        assertSame(fixedClone.bytes(), fixed.bytes());
        // test copy
        FixedTemplate fixedCopy = fixed.clone();
        assertSame(fixedCopy.getClass(), fixed.getClass());
        assertSame(fixedCopy.bytes(), fixed.bytes());
    } catch (Exception exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : FixedTemplate(com.linkedin.data.template.FixedTemplate) ByteString(com.linkedin.data.ByteString) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) ByteString(com.linkedin.data.ByteString)

Example 8 with FixedDataSchema

use of com.linkedin.data.schema.FixedDataSchema in project rest.li by linkedin.

the class TestFixed method testFixed.

private <T extends FixedTemplate> void testFixed(Class<T> fixedClass) {
    try {
        // check for ByteString constructor
        Constructor<T> byteStringConstructor = fixedClass.getConstructor(ByteString.class);
        // check for Object constructor
        Constructor<T> objectConstructor = fixedClass.getConstructor(Object.class);
        // has embedded FixedDataSchema
        FixedDataSchema schema = (FixedDataSchema) DataTemplateUtil.getSchema(fixedClass);
        // get size of fixed
        int size = schema.getSize();
        // create input value
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append((char) ('a' + i % 26));
        }
        String stringValue = sb.toString();
        ByteString byteStringValue = ByteString.copy(stringValue.getBytes(Data.UTF_8_CHARSET));
        // Object ctor, value is String
        T fixed = objectConstructor.newInstance(stringValue);
        assertEquals(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // Object ctor, value is ByteString
        fixed = objectConstructor.newInstance(byteStringValue);
        assertSame(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // ByteString ctor
        fixed = byteStringConstructor.newInstance(byteStringValue);
        assertSame(fixed.data(), byteStringValue);
        assertSame(fixed.data(), fixed.bytes());
        // schema()
        assertSame(fixed.schema(), schema);
        // toString()
        assertEquals(fixed.toString(), byteStringValue.toString());
        // check for clone and copy override with correct return type
        TestDataTemplateUtil.assertCloneAndCopyReturnType(fixedClass);
        // test clone
        FixedTemplate fixedClone = fixed.clone();
        assertSame(fixedClone.getClass(), fixed.getClass());
        assertSame(fixedClone.bytes(), fixed.bytes());
        // test copy
        FixedTemplate fixedCopy = fixed.clone();
        assertSame(fixedCopy.getClass(), fixed.getClass());
        assertSame(fixedCopy.bytes(), fixed.bytes());
    } catch (Exception exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : FixedTemplate(com.linkedin.data.template.FixedTemplate) ByteString(com.linkedin.data.ByteString) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) ByteString(com.linkedin.data.ByteString)

Aggregations

FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)8 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)4 ByteString (com.linkedin.data.ByteString)3 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)3 MapDataSchema (com.linkedin.data.schema.MapDataSchema)3 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)3 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 DataSchema (com.linkedin.data.schema.DataSchema)2 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 FixedTemplate (com.linkedin.data.template.FixedTemplate)2 Name (com.linkedin.data.schema.Name)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)1 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)1 FixedMD5 (com.linkedin.pegasus.generator.test.FixedMD5)1 TyperefTest (com.linkedin.pegasus.generator.test.TyperefTest)1 UnionTest (com.linkedin.pegasus.generator.test.UnionTest)1 Test (org.testng.annotations.Test)1