use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class TestResourceCompatibilityChecker method bindSchemaResolvers.
/**
* Constructs "previous", "current-compatible", and "current-incompatible" schemas and binds them to their respective
* schema resolvers.
* TODO: This should be refactored to use schemas defined in the resources directory so that it's easier to extend.
*/
private void bindSchemaResolvers() {
StringBuilder errors = new StringBuilder();
Name toneName = new Name("com.linkedin.greetings.api.Tone");
EnumDataSchema tone = new EnumDataSchema(toneName);
List<String> symbols = new ArrayList<>();
symbols.add("FRIENDLY");
symbols.add("SINCERE");
symbols.add("INSULTING");
tone.setSymbols(symbols, errors);
Name greetingName = new Name("com.linkedin.greetings.api.Greeting");
RecordDataSchema prevGreeting = new RecordDataSchema(greetingName, RecordDataSchema.RecordType.RECORD);
List<RecordDataSchema.Field> oldFields = new ArrayList<>();
RecordDataSchema.Field id = new RecordDataSchema.Field(new LongDataSchema());
id.setName("id", errors);
oldFields.add(id);
RecordDataSchema.Field message = new RecordDataSchema.Field(new StringDataSchema());
message.setName("message", errors);
oldFields.add(message);
RecordDataSchema.Field toneField = new RecordDataSchema.Field(tone);
toneField.setName("tone", errors);
toneField.setOptional(true);
oldFields.add(toneField);
prevGreeting.setFields(oldFields, errors);
// Previous error details schema (overwrites the existing one in the resolver path)
Name dummyErrorDetailsName = new Name("com.linkedin.restli.tools.DummyErrorDetails");
RecordDataSchema prevDummyErrorDetails = new RecordDataSchema(dummyErrorDetailsName, RecordDataSchema.RecordType.RECORD);
RecordDataSchema.Field idField = new RecordDataSchema.Field(new LongDataSchema());
idField.setName("id", errors);
RecordDataSchema.Field validatedField = new RecordDataSchema.Field(new StringDataSchema());
validatedField.setName("validated", errors);
validatedField.setProperties(Collections.singletonMap("validate", new DataMap(Collections.singletonMap("v1", new DataMap()))));
prevDummyErrorDetails.setFields(Arrays.asList(idField, validatedField), errors);
prevSchemaResolver.bindNameToSchema(toneName, tone, null);
prevSchemaResolver.bindNameToSchema(greetingName, prevGreeting, null);
prevSchemaResolver.bindNameToSchema(dummyErrorDetailsName, prevDummyErrorDetails, null);
// compat greeting added a new optional field "newField"
RecordDataSchema compatGreeting = new RecordDataSchema(greetingName, RecordDataSchema.RecordType.RECORD);
List<RecordDataSchema.Field> compatFields = new ArrayList<>();
compatFields.add(id);
compatFields.add(message);
compatFields.add(toneField);
RecordDataSchema.Field newCompatField = new RecordDataSchema.Field(new StringDataSchema());
newCompatField.setName("newField", errors);
newCompatField.setOptional(true);
compatFields.add(newCompatField);
compatGreeting.setFields(compatFields, errors);
// Compatible error details schema
RecordDataSchema compatDummyErrorDetails = new RecordDataSchema(dummyErrorDetailsName, RecordDataSchema.RecordType.RECORD);
compatDummyErrorDetails.setFields(Arrays.asList(idField, validatedField, newCompatField), errors);
compatSchemaResolver.bindNameToSchema(toneName, tone, null);
compatSchemaResolver.bindNameToSchema(greetingName, compatGreeting, null);
compatSchemaResolver.bindNameToSchema(dummyErrorDetailsName, compatDummyErrorDetails, null);
// Incompatible greeting has removed non-optional field "message",
// has changed the type of "id" to string,
// and added a new non-optional field "newField"
RecordDataSchema incompatGreeting = new RecordDataSchema(greetingName, RecordDataSchema.RecordType.RECORD);
List<RecordDataSchema.Field> incompatFields = new ArrayList<>();
RecordDataSchema.Field incompatId = new RecordDataSchema.Field(new StringDataSchema());
incompatId.setName("id", errors);
oldFields.add(incompatId);
incompatFields.add(incompatId);
incompatFields.add(toneField);
RecordDataSchema.Field newIncompatField = new RecordDataSchema.Field(new StringDataSchema());
newIncompatField.setName("newField", errors);
incompatFields.add(newIncompatField);
incompatGreeting.setFields(incompatFields, errors);
// Incompatible error details schema
RecordDataSchema incompatDummyErrorDetails = new RecordDataSchema(dummyErrorDetailsName, RecordDataSchema.RecordType.RECORD);
RecordDataSchema.Field incompatValidatedField = new RecordDataSchema.Field(new StringDataSchema());
incompatValidatedField.setName("validated", errors);
incompatValidatedField.setProperties(Collections.singletonMap("validate", new DataMap(Collections.singletonMap("v2", new DataMap()))));
incompatDummyErrorDetails.setFields(Collections.singletonList(incompatValidatedField), errors);
incompatSchemaResolver.bindNameToSchema(toneName, tone, null);
incompatSchemaResolver.bindNameToSchema(greetingName, incompatGreeting, null);
incompatSchemaResolver.bindNameToSchema(dummyErrorDetailsName, incompatDummyErrorDetails, null);
}
use of com.linkedin.data.schema.EnumDataSchema 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<UnionDataSchema.Member> members = removeAlreadyTraversedSchemasFromUnionMemberList(parentSchemas, unionSchema.getMembers());
final int unionIndex = _random.nextInt(members.size());
final UnionDataSchema.Member unionMember = members.get(unionIndex);
data = buildData(parentSchemas, unionMember.getType(), fieldName, spec);
if (data != null) {
final DataMap unionMap = new DataMap();
unionMap.put(unionMember.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.EnumDataSchema in project rest.li by linkedin.
the class CompatibilityChecker method check.
private void check(DataSchema older, DataSchema newer) {
Checked toCheck = new Checked(older, newer);
if (_checked.contains(toCheck)) {
return;
}
_checked.add(toCheck);
if (older == newer) {
return;
}
int pathCount = 1;
if (_options.getMode() == CompatibilityOptions.Mode.DATA || _options.getMode() == CompatibilityOptions.Mode.EXTENSION) {
older = older.getDereferencedDataSchema();
while (newer.getType() == DataSchema.Type.TYPEREF) {
TyperefDataSchema typerefDataSchema = ((TyperefDataSchema) newer);
_path.addLast(typerefDataSchema.getFullName());
_path.addLast(DataSchemaConstants.REF_KEY);
pathCount++;
newer = typerefDataSchema.getRef();
}
}
if (newer.getType() == DataSchema.Type.TYPEREF) {
_path.addLast(((TyperefDataSchema) newer).getFullName());
} else {
_path.addLast(newer.getUnionMemberKey());
}
switch(newer.getType()) {
case TYPEREF:
if (isSameType(older, newer))
checkTyperef((TyperefDataSchema) older, (TyperefDataSchema) newer);
break;
case RECORD:
if (isSameType(older, newer))
checkRecord((RecordDataSchema) older, (RecordDataSchema) newer);
break;
case ARRAY:
if (isSameType(older, newer))
checkArray((ArrayDataSchema) older, (ArrayDataSchema) newer);
break;
case MAP:
if (isSameType(older, newer))
checkMap((MapDataSchema) older, (MapDataSchema) newer);
break;
case ENUM:
if (isSameType(older, newer))
checkEnum((EnumDataSchema) older, (EnumDataSchema) newer);
break;
case FIXED:
if (isSameType(older, newer))
checkFixed((FixedDataSchema) older, (FixedDataSchema) newer);
break;
case UNION:
if (isSameType(older, newer))
checkUnion((UnionDataSchema) older, (UnionDataSchema) newer);
break;
default:
if (newer instanceof PrimitiveDataSchema)
checkPrimitive(older, newer);
else
throw new IllegalStateException("Unknown schema type " + newer.getType() + ", checking old schema " + older + ", new schema " + newer);
break;
}
for (; pathCount > 0; pathCount--) {
_path.removeLast();
}
return;
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class ResourceSchemaToResourceSpecTranslator method toType.
public Class<?> toType(DataSchema schema) {
if (schema.getType() == DataSchema.Type.TYPEREF) {
Class<?> javaClass = CustomTypeUtil.getJavaCustomTypeClassFromSchema((TyperefDataSchema) schema);
if (javaClass != null)
return javaClass;
}
DataSchema.Type dereferencedType = schema.getDereferencedType();
DataSchema dereferencedDataSchema = schema.getDereferencedDataSchema();
if (dereferencedDataSchema.isPrimitive()) {
return DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(dereferencedType);
} else if (dereferencedDataSchema instanceof EnumDataSchema) {
return _bindingResolver.resolveEnumClass((EnumDataSchema) dereferencedDataSchema);
} else {
return _bindingResolver.resolveTemplateClass(dereferencedDataSchema);
}
}
use of com.linkedin.data.schema.EnumDataSchema in project rest.li by linkedin.
the class TestRestLiSymbolTableProvider method setup.
@SuppressWarnings("unchecked")
@BeforeMethod
public void setup() {
_client = mock(Client.class);
_provider = new RestLiSymbolTableProvider(_client, "d2://", 10, "Test", "https://Host:100/service");
_nullServerNodeUriProvider = new RestLiSymbolTableProvider(_client, "d2://", 10, "Test", (String) null);
_resourceDefinition = mock(ResourceDefinition.class);
doAnswer(invocation -> {
Set<DataSchema> schemas = (Set<DataSchema>) invocation.getArguments()[0];
EnumDataSchema schema = new EnumDataSchema(new Name("TestEnum"));
schema.setSymbols(Collections.unmodifiableList(Arrays.asList("Symbol1", "Symbol2")), new StringBuilder());
schemas.add(schema);
return null;
}).when(_resourceDefinition).collectReferencedDataSchemas(any(Set.class));
}
Aggregations