Search in sources :

Example 26 with MapDataSchema

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]);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) DirectMapTemplate(com.linkedin.data.template.DirectMapTemplate) BooleanMap(com.linkedin.data.template.BooleanMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) DoubleMap(com.linkedin.data.template.DoubleMap) BytesMap(com.linkedin.data.template.BytesMap) FloatMap(com.linkedin.data.template.FloatMap) IntegerMap(com.linkedin.data.template.IntegerMap) DataMap(com.linkedin.data.DataMap) StringMap(com.linkedin.data.template.StringMap) LongMap(com.linkedin.data.template.LongMap) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 27 with MapDataSchema

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);
}
Also used : HashMap(java.util.HashMap) MapDataSchema(com.linkedin.data.schema.MapDataSchema) Test(org.testng.annotations.Test)

Example 28 with MapDataSchema

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);
}
Also used : MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Name(com.linkedin.data.schema.Name) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 29 with MapDataSchema

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

Example 30 with MapDataSchema

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);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceLevel(com.linkedin.restli.server.ResourceLevel) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) StringArray(com.linkedin.data.template.StringArray) IdentifierSchema(com.linkedin.restli.restspec.IdentifierSchema) RecordTemplate(com.linkedin.data.template.RecordTemplate) AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) MetadataSchema(com.linkedin.restli.restspec.MetadataSchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchemaTraverse(com.linkedin.data.schema.DataSchemaTraverse)

Aggregations

MapDataSchema (com.linkedin.data.schema.MapDataSchema)30 Test (org.testng.annotations.Test)17 DataMap (com.linkedin.data.DataMap)13 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)12 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)11 DataSchema (com.linkedin.data.schema.DataSchema)10 ByteString (com.linkedin.data.ByteString)9 DataList (com.linkedin.data.DataList)9 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)9 HashMap (java.util.HashMap)9 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)7 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)7 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)6 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)4 TestDataTemplateUtil (com.linkedin.data.template.TestDataTemplateUtil)4 Map (java.util.Map)3 TestUtil.asMap (com.linkedin.data.TestUtil.asMap)2 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)2 IntegerMap (com.linkedin.data.template.IntegerMap)2