@Override public DataSchema getDereferencedDataSchema() { return _referencedType.getDereferencedDataSchema(); }
private static String toArrayLiteral(DataList dataList, ArrayDataSchema arraySchema) { StringBuilder sb = new StringBuilder(); DataSchema itemsSchema = arraySchema.getItems().getDereferencedDataSchema(); Iterator<Object> iter = dataList.iterator(); sb.append("["); while (iter.hasNext()) { Object element = iter.next(); sb.append(toLiteral(itemsSchema, element)); if (iter.hasNext()) { sb.append(", "); } } sb.append("]"); return sb.toString(); }
private static String toMapLiteral(DataMap dataMap, MapDataSchema mapSchema) { StringBuilder sb = new StringBuilder(); DataSchema valuesSchema = mapSchema.getValues().getDereferencedDataSchema(); Iterator<Map.Entry<String, Object>> iter = dataMap.entrySet().iterator(); sb.append("["); while (iter.hasNext()) { Map.Entry<String, Object> entry = iter.next(); sb.append("\""); sb.append(escapeString(entry.getKey())); sb.append("\""); sb.append(": "); sb.append(toLiteral(valuesSchema, entry.getValue())); if (iter.hasNext()) { sb.append(", "); } } sb.append("]"); return sb.toString(); } }
/** * Encode a {@link DataSchema}. * * Special handling is required for typeref's. All typeref's are * de-referenced to the actual type. * * @param schema to encode. * @throws IOException */ @Override protected void encode(DataSchema schema, boolean originallyInlined) throws IOException { if (encodeCustomAvroSchema(schema) == false) { super.encode(schema.getDereferencedDataSchema(), originallyInlined); } }
/** * Encode a {@link DataSchema}. * * Special handling is required for typeref's. All typeref's are * de-referenced to the actual type. * * @param schema to encode. * @throws IOException */ @Override public void encode(DataSchema schema) throws IOException { if (encodeCustomAvroSchema(schema) == false) { super.encode(schema.getDereferencedDataSchema()); } }
/** * Return the Avro-compatible union member key (discriminator) for the provided {@link DataSchema}. * * The Avro-compatible key does not include the namespace in the key, e.g. the key for * "com.linkedin.foo.Bar" is "Bar". * * @param schema to return the Avro-compatible union member key for. * @return the Avro-compatible union member key for provided {@link DataSchema}. */ public static String avroUnionMemberKey(DataSchema schema) { DataSchema dereferencedSchema = schema.getDereferencedDataSchema(); String name; // Avro union member type discriminator names if (dereferencedSchema instanceof NamedDataSchema) { name = ((NamedDataSchema) dereferencedSchema).getName(); } else { name = dereferencedSchema.getUnionMemberKey(); } return name; }
private DataSchema currentSchema() { DataSchema schema = null; DataSchema listSchema = _element.getSchema(); if (listSchema != null) { DataSchema dereferencedListSchema = listSchema.getDereferencedDataSchema(); if (dereferencedListSchema.getType() == DataSchema.Type.ARRAY) { schema = ((ArrayDataSchema) dereferencedListSchema).getItems(); } } return schema; }
if (includedSchema != null) DataSchema dereferencedIncludedSchema = includedSchema.getDereferencedDataSchema(); if (includedSchema instanceof NamedDataSchema && dereferencedIncludedSchema instanceof RecordDataSchema)
RecordDataSchema includedRecord = (RecordDataSchema) includedSchema.getDereferencedDataSchema(); fields.addAll(includedRecord.getFields());
if (schema.getType() == DataSchema.Type.TYPEREF) schema = schema.getDereferencedDataSchema();
@Override protected void encodeProperties(DataSchema schema) throws IOException { if (_options.getEmbeddedSchema() == EmbedSchemaMode.ROOT_ONLY) { DataSchema dereferencedSchema = _rootSchema.getDereferencedDataSchema(); if (schema == dereferencedSchema && schema.getType() != DataSchema.Type.UNION) { encodePropertiesWithEmbeddedSchema(schema); return; } } super.encodeProperties(schema); }
private ClassTemplateSpec determineDataClass(DataSchema schema, ClassTemplateSpec enclosingClass, String memberName) { final ClassTemplateSpec result; final DataSchema dereferencedSchema = schema.getDereferencedDataSchema(); if (dereferencedSchema.getType() == DataSchema.Type.ENUM) { result = PrimitiveTemplateSpec.getInstance(DataSchema.Type.STRING); } else if (CodeUtil.isDirectType(dereferencedSchema)) { result = getPrimitiveClassForSchema((PrimitiveDataSchema) dereferencedSchema, enclosingClass, memberName); } else { result = null; } return result; }
private DataSchema currentSchema() { DataSchema schema; DataSchema mapSchema = _element.getSchema(); if (mapSchema == null) { schema = null; } else { DataSchema dereferencedSchema = mapSchema.getDereferencedDataSchema(); DataSchema.Type deferencedType = dereferencedSchema.getType(); switch (deferencedType) { case RECORD: RecordDataSchema.Field field = ((RecordDataSchema) dereferencedSchema).getField(_currentEntry.getKey()); schema = (field == null ? null : field.getType()); break; case UNION: schema = ((UnionDataSchema) dereferencedSchema).getTypeByMemberKey(_currentEntry.getKey()); break; case MAP: schema = ((MapDataSchema) dereferencedSchema).getValues(); break; default: throw new IllegalStateException("Unknown dereferenced type " + deferencedType + " for DataMap's schema " + mapSchema); } } return schema; }
/** * Allow custom class to to bound to record or typeref of primitive types that are not enums. */ private static boolean allowCustomClass(DataSchema schema) { boolean result = false; final DataSchema.Type type = schema.getType(); if (type == DataSchema.Type.TYPEREF || type == DataSchema.Type.RECORD) { // allow custom class only if the dereferenced type is a record or a primitive types that are not enums final DataSchema dereferencedSchema = schema.getDereferencedDataSchema(); if (dereferencedSchema.getType() == DataSchema.Type.RECORD || (CodeUtil.isDirectType(dereferencedSchema) && dereferencedSchema.getType() != DataSchema.Type.ENUM)) { result = true; } } return result; }
for (int i = 0; i < path.length; i++) dataSchema = dataSchema.getDereferencedDataSchema(); switch (dataSchema.getType())
/** * * @param value the stringified value * @param schema the schema of the type * @param type a non-complex type to convert to * @return the converted value */ public static Object convertSimpleValue(final String value, final DataSchema schema, final Class<?> type) { DataSchema.Type dereferencedType = schema.getDereferencedType(); Object underlyingValue; if (schema.getDereferencedDataSchema().isComplex()) { underlyingValue = value; } else { underlyingValue = ValueConverter.coerceString(value, DataSchemaUtil.dataSchemaTypeToPrimitiveDataSchemaClass(dereferencedType)); } return DataTemplateUtil.coerceOutput(underlyingValue, type); }
DataSchema fieldSchema = field.getType().getDereferencedDataSchema(); boolean unionWithMembersAliased = fieldSchema.getType() == DataSchema.Type.UNION && ((UnionDataSchema) fieldSchema).areMembersAliased();
@Override public void callback(List<String> path, DataSchema schema) { if (schema.getType() != DataSchema.Type.RECORD) { return; } // If schema has avro override, do not translate the record's aliased union fields. // These are handled in AvroOverrideFactory#createFromDataSchema() while encoding the Avro schema. if (schema.getProperties().get("avro") != null) { return; } RecordDataSchema recordSchema = (RecordDataSchema) schema; for (RecordDataSchema.Field field : recordSchema.getFields()) { DataSchema fieldSchema = field.getType().getDereferencedDataSchema(); // The conversion from Pegasus union type to an Avro record is performed when the union appears as either the // field's direct type or when the field's type is an array or a map whose (nested) elements is of union type. // This conversion ignores the default value when specified on an array or map typed field. Since the elements in // the default value collection can have conflicting union members, it will be hard to figure out the optionality // property of the fields in the generated Avro record. DataMap modifiedDefaultValue = modifyFieldDefaultValue(field, path); DataSchema modifiedSchema = modifyFieldSchema(recordSchema, field, fieldSchema, modifiedDefaultValue); if (modifiedSchema != null) { overrideUnionFieldSchemaAndDefault(field, modifiedSchema, modifiedDefaultValue); } } }
private Object generateFieldDefValue(FieldDef<?> fieldDef) DataSchema dereferencedDataSchema = fieldDef.getDataSchema().getDereferencedDataSchema(); if (!dereferencedDataSchema.isPrimitive())
private void addParams(ActionRequestBuilder<?, ?> request, DynamicRecordMetadata requestMetadata, ParameterSchemaArray parameters) { if (parameters != null) { for (ParameterSchema parameter : parameters) { FieldDef<?> fieldDef = requestMetadata.getFieldDef(parameter.getName()); Object value = generateFieldDefValue(fieldDef); // For custom types(TypeRefs) we generate the example values using the dereferenced type. Changing the field-def // to the dereferenced type so the example values can be set on the request without coercing. if (fieldDef.getDataSchema().getType() == Type.TYPEREF) { FieldDef<?> deRefFieldDef = new FieldDef<>(fieldDef.getName(), fieldDef.getDataClass(), fieldDef.getDataSchema().getDereferencedDataSchema()); deRefFieldDef.getField().setRecord(fieldDef.getField().getRecord()); fieldDef = deRefFieldDef; } request.setParam(fieldDef, value); } } }