Search in sources :

Example 21 with ArrayDataSchema

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

the class SnapshotGenerator method recordType.

private void recordType(DataSchema schema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    if (schema instanceof NamedDataSchema) {
        NamedDataSchema namedDataSchema = (NamedDataSchema) schema;
        if (!foundTypes.containsKey(namedDataSchema.getFullName())) {
            foundTypes.put(namedDataSchema.getFullName(), namedDataSchema);
            if (// recurse into record, record any contained types.
            schema instanceof RecordDataSchema) {
                RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
                for (NamedDataSchema includedSchema : recordDataSchema.getInclude()) {
                    recordType(includedSchema, foundTypes, typeOrder);
                }
                for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
                    recordType(field.getType(), foundTypes, typeOrder);
                }
            } else if (schema instanceof TyperefDataSchema) {
                recordType(schema.getDereferencedDataSchema(), foundTypes, typeOrder);
            }
            typeOrder.add(namedDataSchema);
        }
    } else if (schema instanceof ArrayDataSchema) {
        ArrayDataSchema arraySchema = (ArrayDataSchema) schema;
        recordType(arraySchema.getItems(), foundTypes, typeOrder);
    } else if (schema instanceof MapDataSchema) {
        MapDataSchema mapSchema = (MapDataSchema) schema;
        recordType(mapSchema.getValues(), foundTypes, typeOrder);
    } else if (schema instanceof UnionDataSchema) {
        UnionDataSchema unionSchema = (UnionDataSchema) schema;
        for (DataSchema type : unionSchema.getTypes()) {
            recordType(type, foundTypes, typeOrder);
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 22 with ArrayDataSchema

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

the class RequestBuilderSpecGenerator method generateQueryParamBindingMethods.

private void generateQueryParamBindingMethods(ParameterSchemaArray parameters, RequestBuilderSpec requestBuilderClass) {
    for (ParameterSchema param : parameters) {
        final String paramName = param.getName();
        final DataSchema typeSchema = RestSpecCodec.textToSchema(param.getType(), _schemaResolver);
        final ClassTemplateSpec typeClassSpec = schemaToTemplateSpec(typeSchema);
        final boolean isOptional = RestLiToolsUtils.isParameterOptional(param);
        final String setMethodName = RestLiToolsUtils.nameCamelCase(paramName + "Param");
        final QueryParamBindingMethodSpec spec = new QueryParamBindingMethodSpec(setMethodName);
        spec.setParamName(paramName);
        spec.setNeedAddParamMethod(false);
        spec.setOptional(isOptional);
        spec.setArgType(typeClassSpec);
        spec.setDoc(param.getDoc());
        requestBuilderClass.addQueryParamMethod(spec);
        if (typeSchema instanceof ArrayDataSchema) {
            final DataSchema itemsSchema = ((ArrayDataSchema) typeSchema).getItems();
            final ClassTemplateSpec itemSpecClass = schemaToTemplateSpec(itemsSchema);
            final String addMethodName = RestLiToolsUtils.nameCamelCase("add" + RestLiToolsUtils.normalizeCaps(paramName) + "Param");
            final QueryParamBindingMethodSpec addSpec = new QueryParamBindingMethodSpec(addMethodName);
            addSpec.setParamName(paramName);
            addSpec.setNeedAddParamMethod(true);
            addSpec.setOptional(isOptional);
            addSpec.setArgType(itemSpecClass);
            addSpec.setDoc(param.getDoc());
            requestBuilderClass.addQueryParamMethod(addSpec);
        }
    }
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) QueryParamBindingMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.QueryParamBindingMethodSpec)

Example 23 with ArrayDataSchema

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

the class RestLiDataValidator method buildArrayDataSchemaByProjection.

/**
   * Build a new {@link ArrayDataSchema} schema that contains only the masked fields.
   */
private static ArrayDataSchema buildArrayDataSchemaByProjection(ArrayDataSchema originalSchema, DataMap maskMap) {
    if (maskMap.containsKey(FilterConstants.WILDCARD)) {
        DataSchema newItemsSchema = reuseOrBuildDataSchema(originalSchema.getItems(), maskMap.get(FilterConstants.WILDCARD));
        ArrayDataSchema newSchema = new ArrayDataSchema(newItemsSchema);
        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) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema)

Example 24 with ArrayDataSchema

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

the class RestLiAnnotationReader method checkParameterHasTyperefSchema.

private static boolean checkParameterHasTyperefSchema(Parameter<?> parameter) {
    boolean result = false;
    DataSchema dataSchema = parameter.getDataSchema();
    Class<?> dataType = parameter.getType();
    if (dataType.isArray()) {
        if (dataSchema instanceof ArrayDataSchema) {
            dataSchema = ((ArrayDataSchema) dataSchema).getItems();
        } else {
            throw new ResourceConfigException("Array typed parameter " + parameter.getName() + " must have an array schema.");
        }
    }
    if (dataSchema instanceof TyperefDataSchema) {
        result = true;
    }
    return result;
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 25 with ArrayDataSchema

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

the class RestLiAnnotationReader method checkTyperefSchema.

private static String checkTyperefSchema(final Class<?> type, final DataSchema dataSchema) {
    if (type.isArray() && dataSchema instanceof ArrayDataSchema) {
        registerCoercerForPrimitiveTypeRefArray((ArrayDataSchema) dataSchema);
    }
    if (!(dataSchema instanceof TyperefDataSchema)) {
        return null;
    }
    TyperefDataSchema typerefSchema = (TyperefDataSchema) dataSchema;
    boolean ok;
    DataSchema.Type schemaType = typerefSchema.getDereferencedType();
    Class<?>[] validTypes = RestModelConstants.PRIMITIVE_DATA_SCHEMA_TYPE_ALLOWED_TYPES.get(schemaType);
    if (validTypes != null) {
        String javaClassNameFromSchema = CustomTypeUtil.getJavaCustomTypeClassNameFromSchema(typerefSchema);
        if (javaClassNameFromSchema != null) {
            registerCoercer(typerefSchema);
            ok = type.getName().equals(javaClassNameFromSchema) || (type.isArray() && (type.getComponentType().getName()).equals(javaClassNameFromSchema));
        } else {
            ok = checkParameterType(type, validTypes);
        }
    } else {
        try {
            DataSchema inferredSchema = DataTemplateUtil.getSchema(type);
            DataSchema derefSchema = typerefSchema.getDereferencedDataSchema();
            if (inferredSchema.equals(derefSchema)) {
                return null;
            }
            return "typeref " + typerefSchema + " is not compatible with (" + type + ") with schema " + derefSchema;
        } catch (TemplateRuntimeException e) {
        }
        ok = false;
    }
    if (!ok) {
        return "typeref " + typerefSchema + " is not compatible with (" + type + ")";
    }
    return null;
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException)

Aggregations

ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)42 Test (org.testng.annotations.Test)20 DataList (com.linkedin.data.DataList)16 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)16 DataMap (com.linkedin.data.DataMap)15 DataSchema (com.linkedin.data.schema.DataSchema)15 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)11 MapDataSchema (com.linkedin.data.schema.MapDataSchema)10 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)8 ArrayList (java.util.ArrayList)7 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)6 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)6 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)5 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)5 TestDataTemplateUtil (com.linkedin.data.template.TestDataTemplateUtil)5 ByteString (com.linkedin.data.ByteString)3 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)3 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)2 Name (com.linkedin.data.schema.Name)2 StringArray (com.linkedin.data.template.StringArray)2