/** * 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; }
public static String getJavaCustomTypeClassNameFromSchema(final DataSchema schema) { final Object o = schema.getProperties().get(JAVA_PROPERTY); if (o == null || !(o instanceof Map)) { return null; } @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) o; final Object o2 = map.get(CLASS_PROPERTY); if (o2 == null || !(o2 instanceof String)) { return null; } return (String)o2; }
/** * Returns the union member class name for the given {@link ClassTemplateSpec} as a Typescript * source code string. * * @return a typescript source code string identifying the union member. */ public String unionMemberTypeName() { DataSchema memberSchema = _memberSchema(); Type memberType = _memberSchema().getType(); if (memberSchema.isPrimitive() || memberType == Type.MAP || memberType == Type.ARRAY) { String unionMemberKey = _memberSchema().getUnionMemberKey(); String camelCasedName = Character.toUpperCase(unionMemberKey.charAt(0)) + unionMemberKey.substring(1); return camelCasedName + "Member"; // IntMember, DoubleMember, FixedMember etc } else if (memberSchema instanceof NamedDataSchema) { String className = ((NamedDataSchema) memberSchema).getName(); return className + "Member"; // e.g: FortuneCookieMember } else { throw new IllegalArgumentException("Don't know how to handle schema of type " + memberSchema.getType()); } }
/** * 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 String typeAccessor(DataSchema schema) { DataSchema.Type schemaType = schema.getType(); String swiftyType = swiftyTypeAccessors.get(schemaType); if (swiftyType == null) { throw new IllegalArgumentException("unrecognized type: " + schemaType); } return swiftyType; }
DataSchema dereferencedDataSchema = dataSchema.getDereferencedDataSchema(); DataSchema.Type type = dereferencedDataSchema.getType(); Object result; switch (type) DataSchema fieldDataSchema = field.getType(); Schema fieldAvroSchema = avroSchema.getField(fieldName).schema(); if (isOptional && (fieldDataSchema.getDereferencedType() != DataSchema.Type.UNION)) String key = memberDataSchema.getUnionMemberKey(); dataMap = new DataMap(1); _path.addLast(key); appendMessage("schema type unknown %1$s", dereferencedDataSchema.getType()) ; result = BAD_RESULT; break;
@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); } } }
if (validatorList == null) Object validateObject = schema.getProperties().get(VALIDATE); if (validateObject == null) if (schema.getType() == DataSchema.Type.RECORD)
if (_options.getMode() == CompatibilityOptions.Mode.DATA) older = older.getDereferencedDataSchema(); while (newer.getType() == DataSchema.Type.TYPEREF) if (newer.getType() == DataSchema.Type.TYPEREF) _path.addLast(newer.getUnionMemberKey()); switch (newer.getType()) checkPrimitive(older, newer); else throw new IllegalStateException("Unknown schema type " + newer.getType() + ", checking old schema " + older + ", new schema " + newer);
@Override public DataSchema getDereferencedDataSchema() { return _referencedType.getDereferencedDataSchema(); }
DataSchema dereferencedDataSchema = dataSchema.getDereferencedDataSchema(); DataSchema.Type type = dereferencedDataSchema.getType(); Object result; switch (type) if (isOptional) if (fieldDataSchema.getDereferencedType() != DataSchema.Type.UNION) appendMessage("schema type unknown %1$s", dereferencedDataSchema.getType()); result = BAD_RESULT; break;
private Object generateFieldDefValue(FieldDef<?> fieldDef) DataSchema dereferencedDataSchema = fieldDef.getDataSchema().getDereferencedDataSchema(); if (!dereferencedDataSchema.isPrimitive()) switch(dereferencedDataSchema.getType()) throw new IllegalStateException("TYPEREF should not be returned for a dereferenced byte. schema: " + fieldDef.getDataSchema()); default: throw new IllegalStateException("Unrecognized enum value: " + dereferencedDataSchema.getType());
else if (includedSchema.getDereferencedType() != DataSchema.Type.RECORD) RecordDataSchema includedRecord = (RecordDataSchema) includedSchema.getDereferencedDataSchema(); fields.addAll(includedRecord.getFields());
@Override public String getUnionMemberKey() { return _referencedType.getUnionMemberKey(); }
private Expr writeTypeExpr(Expr expr, ClassTemplateSpec spec) { DataSchema.Type schemaType = spec.getSchema().getType(); switch (schemaType) { case ENUM: return expr.mapValues(writeTypeExpr(coercer.coercerOutput($0), valueSpec)); } else { if (valueSpec.getSchema().isPrimitive()) { return expr; } else { return expr.map(writeTypeExpr(coercer.coercerOutput($0), itemSpec)); } else { if (itemSpec.getSchema().isPrimitive()) { return expr; } else {
/** * * @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); }
@Override public Type getDereferencedType() { return _referencedType.getDereferencedType(); }
private String getParameterItems(ParameterSchema param, DataSchemaResolver resolver) { if (param.hasItems()) { _infoMap.addRestSpecInfo(CompatibilityInfo.Type.DEPRECATED, _infoPath, "The \"items\" field"); return param.getItems(GetMode.DEFAULT); } final DataSchema paramDataSchema = RestSpecCodec.textToSchema(param.getType(GetMode.DEFAULT), resolver); if (paramDataSchema instanceof ArrayDataSchema) { // Union member key works because previously only primitive types are allowed for items field return ((ArrayDataSchema) paramDataSchema).getItems().getUnionMemberKey(); } else { _infoMap.addRestSpecInfo("type", CompatibilityInfo.Type.TYPE_ERROR, _infoPath, "expect an array, got " + paramDataSchema.getType()); return null; } }
@Override public void callback(List<String> path, DataSchema schema) if (schema.getType() != DataSchema.Type.RECORD) boolean isUnion = fieldSchema.getDereferencedType() == DataSchema.Type.UNION; field.setOptional(false); if (isUnion) { .filter(member -> member.getType().getType() != NULL_DATA_SCHEMA.getType()) .collect(Collectors.toCollection(ArrayList::new));