Search in sources :

Example 11 with TyperefDataSchema

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

the class KeyValueRecord method setPrimitiveKey.

/**
 * Sets a primitive key. If the key is a typeref the typeref is followed and the primitive value is stored.
 * @param keyField key field
 * @param key the primitive key to set
 * @param keyType the type of the key
 */
@SuppressWarnings("unchecked")
void setPrimitiveKey(RecordDataSchema.Field keyField, K key, TypeSpec<K> keyType) {
    DataSchema keySchema = keyType.getSchema();
    if (keySchema.isPrimitive()) {
        putDirect(keyField, keyType.getType(), keyType.getType(), key, SetMode.IGNORE_NULL);
        return;
    }
    switch(keySchema.getType()) {
        case TYPEREF:
            TyperefDataSchema typerefDataSchema = (TyperefDataSchema) keySchema;
            DataSchema.Type dereferencedType = keySchema.getDereferencedType();
            Class<?> javaClassForSchema = CustomTypeUtil.getJavaCustomTypeClassFromSchema(typerefDataSchema);
            if (javaClassForSchema == null) {
                // typeref to a primitive. In this case the keyClass is a primitive, and so is the key.
                putDirect(keyField, keyType.getType(), keyType.getType(), key, SetMode.IGNORE_NULL);
            } else {
                // typeref to a custom type. In this case the keyClass is the typeref class, but the class of the key
                // is the custom class.
                Class<?> keyDereferencedClass = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(dereferencedType);
                putDirect(keyField, (Class<Object>) javaClassForSchema, keyDereferencedClass, key, SetMode.IGNORE_NULL);
            }
            break;
        case ENUM:
            putDirect(keyField, keyType.getType(), String.class, key, SetMode.IGNORE_NULL);
            break;
        default:
            throw new IllegalArgumentException("key is not a primitive, typeref, or an enum!");
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema)

Example 12 with TyperefDataSchema

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

the class ResponseUtils method convertKey.

public static Object convertKey(String rawKey, TypeSpec<?> keyType, Map<String, CompoundKey.TypeInfo> keyParts, ComplexKeySpec<?, ?> complexKeyType, ProtocolVersion version) {
    Class<?> keyBindingClass = keyType.getType();
    Object result;
    if (TyperefInfo.class.isAssignableFrom(keyType.getType())) {
        TyperefDataSchema schema = (TyperefDataSchema) keyType.getSchema();
        if (!schema.getDereferencedDataSchema().isPrimitive()) {
            throw new IllegalArgumentException("Typeref must reference a primitive type when used as a key type.");
        }
        // Coerce the raw key string to the referenced primitive type.
        DataSchema.Type dereferencedType = schema.getDereferencedType();
        Class<?> primitiveClass = DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(dereferencedType);
        result = ValueConverter.coerceString(rawKey, primitiveClass);
        // Identify the binding class for the typeref.
        keyBindingClass = CustomTypeUtil.getJavaCustomTypeClassFromSchema(schema);
        if (keyBindingClass == null) {
            keyBindingClass = primitiveClass;
        }
    } else if (CompoundKey.class.isAssignableFrom(keyType.getType())) {
        DataMap keyDataMap;
        if (version.compareTo(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()) >= 0) {
            try {
                keyDataMap = (DataMap) URIElementParser.parse(rawKey);
            } catch (PathSegment.PathSegmentSyntaxException e) {
                throw new IllegalStateException(rawKey + " is not a valid value for the resource key", e);
            }
        } else {
            keyDataMap = parseKey(rawKey);
        }
        result = CompoundKey.fromValues(keyDataMap, keyParts);
    } else if (ComplexResourceKey.class.isAssignableFrom(keyType.getType())) {
        try {
            ComplexResourceKey<RecordTemplate, RecordTemplate> complexResourceKey = ComplexResourceKey.parseString(rawKey, complexKeyType, version);
            result = QueryParamsDataMap.fixUpComplexKeySingletonArray(complexResourceKey);
        } catch (PathSegment.PathSegmentSyntaxException e) {
            throw new IllegalStateException(rawKey + " is not a valid value for the resource key", e);
        }
    } else {
        try {
            result = ValueConverter.coerceString(rawKey, keyType.getType());
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException(rawKey + " is not a valid value for resource key type " + keyType.getType().getName(), e);
        }
    }
    return DataTemplateUtil.coerceOutput(result, keyBindingClass);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordTemplate(com.linkedin.data.template.RecordTemplate)

Example 13 with TyperefDataSchema

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

the class ProjectionMaskApplier method buildTyperefDataSchemaByProjection.

/**
 * Build a new {@link TyperefDataSchema} schema that contains only the masked fields.
 */
private static TyperefDataSchema buildTyperefDataSchemaByProjection(TyperefDataSchema originalSchema, DataMap maskMap) {
    TyperefDataSchema newSchema = new TyperefDataSchema(new Name(originalSchema.getFullName()));
    if (originalSchema.getProperties() != null) {
        newSchema.setProperties(originalSchema.getProperties());
    }
    if (originalSchema.getDoc() != null) {
        newSchema.setDoc(originalSchema.getDoc());
    }
    if (originalSchema.getAliases() != null) {
        newSchema.setAliases(originalSchema.getAliases());
    }
    DataSchema newRefSchema = buildSchemaByProjection(originalSchema.getRef(), maskMap);
    newSchema.setReferencedType(newRefSchema);
    return newSchema;
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) Name(com.linkedin.data.schema.Name)

Example 14 with TyperefDataSchema

use of com.linkedin.data.schema.TyperefDataSchema 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 (UnionDataSchema.Member member : unionSchema.getMembers()) {
            recordType(member.getType(), foundTypes, typeOrder);
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) 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 15 with TyperefDataSchema

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

the class RuntimeSymbolTableGenerator method expandAndCollectSymbols.

public static void expandAndCollectSymbols(DataSchema resourceSchema, Set<DataSchema> processedSchemas, Set<String> symbols) {
    if (resourceSchema instanceof TyperefDataSchema) {
        TyperefDataSchema typerefDataSchema = (TyperefDataSchema) resourceSchema;
        expandAndCollectSymbols(typerefDataSchema.getDereferencedDataSchema(), processedSchemas, symbols);
        return;
    } else if (resourceSchema instanceof ArrayDataSchema) {
        ArrayDataSchema arrayDataSchema = (ArrayDataSchema) resourceSchema;
        expandAndCollectSymbols(arrayDataSchema.getItems(), processedSchemas, symbols);
        return;
    } else if (resourceSchema instanceof MapDataSchema) {
        MapDataSchema mapDataSchema = (MapDataSchema) resourceSchema;
        expandAndCollectSymbols(mapDataSchema.getValues(), processedSchemas, symbols);
        return;
    }
    if (processedSchemas.contains(resourceSchema)) {
        return;
    }
    processedSchemas.add(resourceSchema);
    if (resourceSchema instanceof RecordDataSchema) {
        RecordDataSchema recordDataSchema = (RecordDataSchema) resourceSchema;
        for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
            symbols.add(field.getName());
            expandAndCollectSymbols(field.getType(), processedSchemas, symbols);
        }
    } else if (resourceSchema instanceof UnionDataSchema) {
        UnionDataSchema unionDataSchema = (UnionDataSchema) resourceSchema;
        for (UnionDataSchema.Member member : unionDataSchema.getMembers()) {
            symbols.add(member.getUnionMemberKey());
            expandAndCollectSymbols(member.getType(), processedSchemas, symbols);
        }
    } else if (resourceSchema instanceof EnumDataSchema) {
        EnumDataSchema enumDataSchema = (EnumDataSchema) resourceSchema;
        symbols.addAll(enumDataSchema.getSymbols());
    }
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Aggregations

TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)35 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)26 DataSchema (com.linkedin.data.schema.DataSchema)23 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)21 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)18 MapDataSchema (com.linkedin.data.schema.MapDataSchema)14 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)9 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)9 DataMap (com.linkedin.data.DataMap)8 Test (org.testng.annotations.Test)8 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)7 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 Name (com.linkedin.data.schema.Name)6 ArrayList (java.util.ArrayList)4 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)3 StringDataSchema (com.linkedin.data.schema.StringDataSchema)3 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)3 DataList (com.linkedin.data.DataList)2 JsonBuilder (com.linkedin.data.schema.JsonBuilder)2 PathSpec (com.linkedin.data.schema.PathSpec)2