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!");
}
}
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);
}
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;
}
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);
}
}
}
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());
}
}
Aggregations