use of com.linkedin.data.schema.MapDataSchema in project rest.li by linkedin.
the class TestSchemaSampleDataGenerator method testMapSchema.
@Test
public void testMapSchema() {
for (Map.Entry<DataSchema.Type, Class<? extends DirectMapTemplate<?>>> entry : _dataSchemaTypeToprimitiveMapMap.entrySet()) {
final PrimitiveDataSchema valueSchema = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchema(entry.getKey());
final MapDataSchema mapSchema = new MapDataSchema(valueSchema);
final DataMap value = (DataMap) SchemaSampleDataGenerator.buildData(mapSchema, _spec);
final ParameterizedType mapType = (ParameterizedType) entry.getValue().getGenericSuperclass();
assert (mapType.getRawType() == DirectMapTemplate.class);
Assert.assertSame(value.values().iterator().next().getClass(), mapType.getActualTypeArguments()[0]);
}
}
use of com.linkedin.data.schema.MapDataSchema in project rest.li by linkedin.
the class TestMap method testRecordMap.
@Test
public void testRecordMap() {
TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(new MapTest(), "recordMap");
@SuppressWarnings("unchecked") Class<RecordBarMap> templateClass = (Class<RecordBarMap>) fieldInfo.getFieldClass();
MapDataSchema schema = (MapDataSchema) fieldInfo.getField().getType();
Map<String, RecordBar> input = new HashMap<String, RecordBar>();
for (int i = 0; i < 5; ++i) {
String key = "input" + i;
input.put(key, new RecordBar());
input.get(key).setLocation("subinputvalue" + i);
}
Map<String, RecordBar> adds = new HashMap<String, RecordBar>();
for (int i = 0; i < 5; ++i) {
String key = "add" + i;
adds.put(key, new RecordBar());
adds.get(key).setLocation("subaddvalue" + i);
}
TestMapTemplate.testMap(templateClass, schema, input, adds);
}
use of com.linkedin.data.schema.MapDataSchema in project rest.li by linkedin.
the class BatchKVResponse method createSchema.
protected void createSchema(Class<V> valueClass) {
_valueClass = valueClass;
final StringBuilder errorMessageBuilder = new StringBuilder(10);
final Name elementSchemaName = new Name(valueClass.getSimpleName(), errorMessageBuilder);
final MapDataSchema resultsSchema = new MapDataSchema(new RecordDataSchema(elementSchemaName, RecordDataSchema.RecordType.RECORD));
final RecordDataSchema.Field resultsField = new RecordDataSchema.Field(resultsSchema);
resultsField.setName(RESULTS, errorMessageBuilder);
final Name errorSchemaName = new Name(ErrorResponse.class.getSimpleName(), errorMessageBuilder);
final MapDataSchema errorsSchema = new MapDataSchema(new RecordDataSchema(errorSchemaName, RecordDataSchema.RecordType.RECORD));
final RecordDataSchema.Field errorsField = new RecordDataSchema.Field(errorsSchema);
errorsField.setName(ERRORS, errorMessageBuilder);
final Name name = new Name(BatchKVResponse.class.getSimpleName(), errorMessageBuilder);
_schema = new RecordDataSchema(name, RecordDataSchema.RecordType.RECORD);
_schema.setFields(Arrays.asList(resultsField, errorsField), errorMessageBuilder);
}
use of com.linkedin.data.schema.MapDataSchema in project rest.li by linkedin.
the class RestLiDataValidator method buildMapDataSchemaByProjection.
/**
* Build a new {@link MapDataSchema} schema that contains only the masked fields.
*/
private static MapDataSchema buildMapDataSchemaByProjection(MapDataSchema originalSchema, DataMap maskMap) {
if (maskMap.containsKey(FilterConstants.WILDCARD)) {
DataSchema newValuesSchema = reuseOrBuildDataSchema(originalSchema.getValues(), maskMap.get(FilterConstants.WILDCARD));
MapDataSchema newSchema = new MapDataSchema(newValuesSchema);
if (originalSchema.getProperties() != null) {
newSchema.setProperties(originalSchema.getProperties());
}
return newSchema;
}
throw new IllegalArgumentException("Missing wildcard key in projection mask: " + maskMap.keySet());
}
use of com.linkedin.data.schema.MapDataSchema in project rest.li by linkedin.
the class RestLiResourceRelationship method findDataModels.
private void findDataModels() {
final ResourceSchemaVisitior visitor = new BaseResourceSchemaVisitor() {
@Override
public void visitResourceSchema(VisitContext visitContext, ResourceSchema resourceSchema) {
final String schema = resourceSchema.getSchema();
// ActionSet resources do not have a schema
if (schema != null) {
final NamedDataSchema schemaSchema = extractSchema(schema);
if (schemaSchema != null) {
connectSchemaToResource(visitContext, schemaSchema);
}
}
}
@Override
public void visitCollectionResource(VisitContext visitContext, CollectionSchema collectionSchema) {
final IdentifierSchema id = collectionSchema.getIdentifier();
final NamedDataSchema typeSchema = extractSchema(id.getType());
if (typeSchema != null) {
connectSchemaToResource(visitContext, typeSchema);
}
final String params = id.getParams();
if (params != null) {
final NamedDataSchema paramsSchema = extractSchema(params);
if (paramsSchema != null) {
connectSchemaToResource(visitContext, paramsSchema);
}
}
}
@Override
public void visitAssociationResource(VisitContext visitContext, AssociationSchema associationSchema) {
for (AssocKeySchema key : associationSchema.getAssocKeys()) {
final NamedDataSchema keyTypeSchema = extractSchema(key.getType());
if (keyTypeSchema != null) {
connectSchemaToResource(visitContext, keyTypeSchema);
}
}
}
@Override
public void visitParameter(VisitContext visitContext, RecordTemplate parentResource, Object parentMethodSchema, ParameterSchema parameterSchema) {
String parameterTypeString = parameterSchema.getType();
if (// the parameter type field contains a inline schema, so we traverse into it
isInlineSchema(parameterTypeString)) {
visitInlineSchema(visitContext, parameterTypeString);
} else {
final NamedDataSchema schema;
// grab the schema name from it
if (parameterSchema.hasItems()) {
schema = extractSchema(parameterSchema.getItems());
} else // the only remaining possibility is that the type field contains the name of a data schema
{
schema = extractSchema(parameterTypeString);
}
if (schema != null) {
connectSchemaToResource(visitContext, schema);
}
}
}
@Override
public void visitFinder(VisitContext visitContext, RecordTemplate parentResource, FinderSchema finderSchema) {
final MetadataSchema metadata = finderSchema.getMetadata();
if (metadata != null) {
final NamedDataSchema metadataTypeSchema = extractSchema(metadata.getType());
if (metadataTypeSchema != null) {
connectSchemaToResource(visitContext, metadataTypeSchema);
}
}
}
@Override
public void visitAction(VisitContext visitContext, RecordTemplate parentResource, ResourceLevel resourceLevel, ActionSchema actionSchema) {
final String returns = actionSchema.getReturns();
if (returns != null) {
if (// the parameter type field contains a inline schema, so we traverse into it
isInlineSchema(returns)) {
visitInlineSchema(visitContext, returns);
} else // otherwise the type field contains the name of a data schema
{
final NamedDataSchema returnsSchema = extractSchema(returns);
if (returnsSchema != null) {
connectSchemaToResource(visitContext, returnsSchema);
}
}
}
final StringArray throwsArray = actionSchema.getThrows();
if (throwsArray != null) {
for (String errorName : throwsArray) {
final NamedDataSchema errorSchema = extractSchema(errorName);
if (errorSchema != null) {
connectSchemaToResource(visitContext, errorSchema);
}
}
}
}
private boolean isInlineSchema(String schemaString) {
return schemaString.startsWith("{");
}
private void visitInlineSchema(VisitContext visitContext, String schemaString) {
DataSchema schema = DataTemplateUtil.parseSchema(schemaString, _schemaResolver);
if (schema instanceof ArrayDataSchema) {
DataSchema itemSchema = ((ArrayDataSchema) schema).getItems();
if (itemSchema instanceof NamedDataSchema) {
connectSchemaToResource(visitContext, (NamedDataSchema) itemSchema);
}
}
if (schema instanceof MapDataSchema) {
DataSchema valueSchema = ((MapDataSchema) schema).getValues();
if (valueSchema instanceof NamedDataSchema) {
connectSchemaToResource(visitContext, (NamedDataSchema) valueSchema);
}
}
}
private void connectSchemaToResource(VisitContext visitContext, final NamedDataSchema schema) {
final Node<NamedDataSchema> schemaNode = _relationships.get(schema);
_dataModels.put(schema.getFullName(), schema);
final DataSchemaTraverse traveler = new DataSchemaTraverse();
traveler.traverse(schema, new DataSchemaTraverse.Callback() {
@Override
public void callback(List<String> path, DataSchema nestedSchema) {
if (nestedSchema instanceof RecordDataSchema && nestedSchema != schema) {
final RecordDataSchema nestedRecordSchema = (RecordDataSchema) nestedSchema;
_dataModels.put(nestedRecordSchema.getFullName(), nestedRecordSchema);
final Node<RecordDataSchema> node = _relationships.get(nestedRecordSchema);
schemaNode.addAdjacentNode(node);
}
}
});
final Node<ResourceSchema> resourceNode = _relationships.get(visitContext.getParentSchema());
resourceNode.addAdjacentNode(schemaNode);
schemaNode.addAdjacentNode(resourceNode);
}
};
ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
}
Aggregations