private static String toRecordLiteral(StringBuilder sb, DataMap dataMap, RecordDataSchema recordSchema) { Iterator<RecordDataSchema.Field> iter = recordSchema.getFields().iterator(); sb.append(escapeKeyword(recordSchema.getName())); sb.append("("); while (iter.hasNext()) { RecordDataSchema.Field field = iter.next(); sb.append(escapeKeyword(field.getName())); sb.append(": "); sb.append(toLiteral(field.getType(), dataMap.get(field.getName()))); if (iter.hasNext()) { sb.append(", "); } } sb.append(")"); return sb.toString(); }
/** * Returns Java source code that computes the hashCodes of each of the fields, as a list of * parameters. * * This is the same as {@link #fieldList} except when the fields are Java arrays, in which * case they are wrapped with a utilty method to hash them correctly. E.g. * * <code> * intField, stringField, mapField, Arrays.deepHashCode(javaArrayField), recordField * </code> * * @param fields provides the fields to include in the hashcode expression. * @return a java expression that calculates a hash code. */ public String hashCodeList(List<RecordTemplateSpec.Field> fields) { StringBuilder sb = new StringBuilder(); Iterator<RecordTemplateSpec.Field> iter = fields.iterator(); while(iter.hasNext()) { RecordTemplateSpec.Field field = iter.next(); Type schemaType = field.getSchemaField().getType().getType(); sb.append(escapeKeyword(field.getSchemaField().getName())); if (iter.hasNext()) sb.append(", "); } return sb.toString(); } }
if (value != null) DataSchema valueSchema = field.getType(); ValidationResult result = ValidateDataAgainstSchema.validate(value, valueSchema, _validationOptions); if (result.isValid() == false)
recordType(field.getType(), foundTypes, typeOrder);
for (RecordDataSchema.Field field : recordSchema.getFields()) DataSchema fieldSchema = field.getType();
value = buildData(parentSchemas, field.getType(), field.getName(), spec);
DataSchema fieldSchema = field.getType().getDereferencedDataSchema(); boolean unionWithMembersAliased = fieldSchema.getType() == DataSchema.Type.UNION && ((UnionDataSchema) fieldSchema).areMembersAliased();
wrapped = DataTemplateUtil.wrap(found, field.getType(), valueClass); getCache().put(found, wrapped);
if (i == path.length - 1) return field; if (field == null) return null; dataSchema = field.getType(); break; case UNION:
@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); } } }
final ClassTemplateSpec fieldClass = processSchema(field.getType(), recordClass, field.getName()); final RecordTemplateSpec.Field newField = new RecordTemplateSpec.Field(); newField.setSchemaField(field); newField.setType(fieldClass); newField.setDataClass(determineDataClass(field.getType(), recordClass, field.getName())); final CustomInfoSpec customInfo = getImmediateCustomInfo(field.getType()); if (customInfo != null && !customInfoMap.containsKey(customInfo))
@Override public void key(String key) throws IOException { DataSchema newSchema = null; if (_currentSchema != null) { switch (_currentSchema.getType()) { case RECORD: RecordDataSchema recordSchema = (RecordDataSchema) _currentSchema; RecordDataSchema.Field field = recordSchema.getField(key); if (field != null) { newSchema = field.getType(); } break; case UNION: UnionDataSchema unionSchema = (UnionDataSchema) _currentSchema; newSchema = unionSchema.getTypeByMemberKey(key); break; case MAP: MapDataSchema mapSchema = (MapDataSchema) _currentSchema; newSchema = mapSchema.getValues(); break; } } _pendingSchema = newSchema; super.key(key); }
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; }
@Override protected Object translateField(List<Object> path, Object fieldValue, RecordDataSchema.Field field) { DataSchema fieldDataSchema = field.getType(); boolean isOptional = field.getOptional(); Object result; if (isOptional && fieldValue == Data.NULL) { // for optional fields, // null union members have been removed from translated union schema // default value of null should also be removed, make it so that there is no default result = null; } else { result = translate(path, fieldValue, fieldDataSchema); } return result; } }
protected void recurseRecord(DataElement element, RecordDataSchema schema, DataMap map) { MutableDataElement childElement = new MutableDataElement(element); for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); RecordDataSchema.Field field = schema.getField(key); Object value = entry.getValue(); if (field != null) { DataSchema childSchema = field.getType(); childElement.setValueNameSchema(value, key, childSchema); validate(childElement, childSchema, value); } else { childElement.setValueNameSchema(value, key, null); handleUnrecognizedField(childElement); } } }
/** * Helper method to set an override for the field's schema and default value. * * @param field Reference to the field whose schema and default value is being overridden * @param modifiedSchema The override schema to use for the specified field * @param modifiedDefaultValue The override default value to use for the specified field */ private void overrideUnionFieldSchemaAndDefault(RecordDataSchema.Field field, DataSchema modifiedSchema, Object modifiedDefaultValue) { // Stash the field's original type and default value, so that we can use this for reverting them back after // the schema translation is complete. This is because we don't want the input schema to have any modifications // when the control goes back to the caller. FieldOverride fieldSchemaOverride = new FieldOverride(field.getType(), field.getDefault()); _schemaOverrides.put(field, fieldSchemaOverride); field.setType(modifiedSchema); field.setDefault(modifiedDefaultValue); }
public String typeName() { // To resolve type name we have to determine whether to use the DataSchema in _field.getType() or // the one in _field.getSchemaField().getType(). We reach first for the schemaField as it does not swallow // Typerefs. (e.g. if a type was defined as CustomInt, it will give us the string CustomInt, whereas // field.getType() would dereference all the way to the bottom). // // The only problem with schemaField is that it _does_ swallow the type names for enclosed unions. ARGH // can we catch a break?? Thankfully in the case of the enclosed union it ends up returning null, so // we back off to _field.getType() if schemaField returned null. TSTypeSyntax candidateSyntax = createTypeSyntax(_schemaField().getType()); if (candidateSyntax.typeName() == null || "".equals(candidateSyntax)) { candidateSyntax = createTypeSyntax(_field.getType()); } return typeNameQualifiedByEnclosingClass(candidateSyntax); }
private TSTypeSyntax _fieldTypeSyntax() { return createTypeSyntax(_schemaField().getType()); } }
/** * Encode a field's type (i.e. {@link DataSchema}. * * @param field providing the type to encode. * @throws IOException if there is an error while encoding. */ protected void encodeFieldType(RecordDataSchema.Field field) throws IOException { _builder.writeFieldName(TYPE_KEY); DataSchema fieldSchema = field.getType(); encode(fieldSchema, field.isDeclaredInline()); }
private boolean isFieldOptional(RecordDataSchema.Field field, DataElement element) { if (field.getOptional()) { return true; } return _options.getTreatOptional().evaluate(new SimpleDataElement(null, field.getName(), field.getType(), element)); }