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;
}
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);
}
}
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);
}
}
Aggregations