Search in sources :

Example 36 with RecordDataSchema

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

the class TestComplexResourceKey method testKeySchema.

@Test
public void testKeySchema() {
    RecordDataSchema schema = OmniRecord.schema;
    TypeSpec<OmniRecord> keyType = new TypeSpec<OmniRecord>(OmniRecord.class, schema);
    TypeSpec<OmniRecord> paramsType = new TypeSpec<OmniRecord>(OmniRecord.class, schema);
    ComplexKeySpec<OmniRecord, OmniRecord> keySpec = new ComplexKeySpec<OmniRecord, OmniRecord>(keyType, paramsType);
    DataMap data = new DataMap();
    data.put("int", 1);
    ComplexResourceKey<RecordTemplate, RecordTemplate> key = ComplexResourceKey.buildFromDataMap(data, keySpec);
    Assert.assertEquals(key.getKey().schema(), schema);
    Assert.assertEquals(key.getParams().schema(), schema);
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 37 with RecordDataSchema

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

the class ActionResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    final Object value;
    final HttpStatus status;
    if (result instanceof ActionResult) {
        final ActionResult<?> actionResult = (ActionResult<?>) result;
        value = actionResult.getValue();
        status = actionResult.getStatus();
        if (status == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null HttpStatus inside of an ActionResult returned by the resource method: " + routingResult.getResourceMethod());
        }
    } else {
        value = result;
        status = HttpStatus.S_200_OK;
    }
    RecordDataSchema actionReturnRecordDataSchema = routingResult.getResourceMethod().getActionReturnRecordDataSchema();
    @SuppressWarnings("unchecked") FieldDef<Object> actionReturnFieldDef = (FieldDef<Object>) routingResult.getResourceMethod().getActionReturnFieldDef();
    final ActionResponse<?> actionResponse = new ActionResponse<Object>(value, actionReturnFieldDef, actionReturnRecordDataSchema);
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(status, headers, cookies);
    responseData.setResponseEnvelope(new ActionResponseEnvelope(actionResponse, responseData));
    return responseData;
}
Also used : HttpStatus(com.linkedin.restli.common.HttpStatus) ActionResponse(com.linkedin.restli.common.ActionResponse) FieldDef(com.linkedin.data.template.FieldDef) ActionResult(com.linkedin.restli.server.ActionResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 38 with RecordDataSchema

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

the class DataSchemaAnnotationValidator method buildSchemaValidators.

/**
   * Build a cache of {@link Validator}s declared for the specified schema.
   *
   * @param schema to cache {@link Validator}s for.
   * @return the cache if successful.
   */
private IdentityHashMap<Object, List<Validator>> buildSchemaValidators(DataSchema schema) {
    final IdentityHashMap<Object, List<Validator>> map = new IdentityHashMap<Object, List<Validator>>();
    DataSchemaTraverse traverse = new DataSchemaTraverse();
    traverse.traverse(schema, new DataSchemaTraverse.Callback() {

        @Override
        public void callback(List<String> path, DataSchema schema) {
            List<Validator> validatorList = map.get(schema);
            if (validatorList == null) {
                Object validateObject = schema.getProperties().get(VALIDATE);
                if (validateObject == null) {
                    validatorList = NO_VALIDATORS;
                } else {
                    validatorList = buildValidatorList(validateObject, path, schema);
                }
                map.put(schema, validatorList);
                if (schema.getType() == DataSchema.Type.RECORD) {
                    RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
                    for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
                        validateObject = field.getProperties().get(VALIDATE);
                        if (validateObject == null) {
                            validatorList = NO_VALIDATORS;
                        } else {
                            path.add(field.getName());
                            validatorList = buildValidatorList(validateObject, path, field);
                            path.remove(path.size() - 1);
                        }
                        map.put(field, validatorList);
                    }
                }
            }
        }
    });
    return map;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) MessageList(com.linkedin.data.message.MessageList) ArrayList(java.util.ArrayList) List(java.util.List) DataSchemaTraverse(com.linkedin.data.schema.DataSchemaTraverse)

Example 39 with RecordDataSchema

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

the class DynamicRecordMetadata method buildSchema.

/**
   * Build the schema of a {@link DynamicRecordTemplate}.
   *
   * @param name the name of the record.
   * @param fieldDefs the fields of the record.
   * @throws IllegalArgumentException if the {@link com.linkedin.data.schema.RecordDataSchema.Field} of the fieldDefs
   *                                  are already set.
   */
public static RecordDataSchema buildSchema(String name, Collection<? extends FieldDef<?>> fieldDefs) {
    StringBuilder errorMessageBuilder = new StringBuilder();
    RecordDataSchema schema = new RecordDataSchema(new Name(name, errorMessageBuilder), RecordDataSchema.RecordType.RECORD);
    List<RecordDataSchema.Field> fields = new ArrayList<RecordDataSchema.Field>(fieldDefs.size());
    for (FieldDef<?> fieldDef : fieldDefs) {
        RecordDataSchema.Field paramField = fieldDef.getField();
        if (paramField.getRecord() != null) {
            throw new IllegalArgumentException("Attempt to assign field " + fieldDef.getName() + " to record " + schema.getName() + "failed: " + "Record of field is already set to " + paramField.getRecord().getName());
        }
        paramField.setRecord(schema);
        fields.add(paramField);
    }
    schema.setFields(fields, errorMessageBuilder);
    return schema;
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayList(java.util.ArrayList) Name(com.linkedin.data.schema.Name)

Example 40 with RecordDataSchema

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

the class TestLongStringLiteral method testSchema.

@Test
public void testSchema() {
    DataSchema schema = DataTemplateUtil.getSchema(LongStringLiteral.class);
    String schemaText = schema.toString();
    assertTrue(schemaText.length() > 65536);
    RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
    RecordDataSchema.Field field = recordDataSchema.getField("text");
    DataList defaultValue = (DataList) field.getDefault();
    assertEquals(defaultValue.size(), 400);
    for (Object s : defaultValue) {
        assertEquals(s, LOREM);
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) DataList(com.linkedin.data.DataList) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Test(org.testng.annotations.Test)

Aggregations

RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)63 DataMap (com.linkedin.data.DataMap)26 DataSchema (com.linkedin.data.schema.DataSchema)25 Test (org.testng.annotations.Test)24 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)15 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)14 MapDataSchema (com.linkedin.data.schema.MapDataSchema)12 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)12 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)10 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)8 Name (com.linkedin.data.schema.Name)8 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)7 ArrayList (java.util.ArrayList)7 FieldDef (com.linkedin.data.template.FieldDef)6 Schema (org.apache.avro.Schema)6 DataList (com.linkedin.data.DataList)5 ActionResponse (com.linkedin.restli.common.ActionResponse)5 GenericRecord (org.apache.avro.generic.GenericRecord)5 SchemaParser (com.linkedin.data.schema.SchemaParser)4 TyperefTest (com.linkedin.pegasus.generator.test.TyperefTest)4