private static Schema getUnnestedSchema( Schema schema, List<String> nameComponents, SerializableFunction<List<String>, String> fn) { Schema.Builder builder = Schema.builder(); for (Field field : schema.getFields()) { nameComponents.add(field.getName()); if (field.getType().getTypeName().isCompositeType()) { Schema nestedSchema = getUnnestedSchema(field.getType().getRowSchema(), nameComponents, fn); for (Field nestedField : nestedSchema.getFields()) { builder.addField(nestedField); } } else { String name = fn.apply(nameComponents); Field newField = field.toBuilder().setName(name).build(); builder.addField(newField); } nameComponents.remove(nameComponents.size() - 1); } return builder.build(); } /** Unnest a row. */
/** Returns a copy of the Field with the {@link FieldType} set. */ public Field withType(FieldType fieldType) { return toBuilder().setType(fieldType).build(); }
/** Returns a copy of the Field with isNullable set. */ public Field withNullable(boolean isNullable) { return toBuilder().setNullable(isNullable).build(); }
/** Returns a copy of the Field with the description set. */ public Field withDescription(String description) { return toBuilder().setDescription(description).build(); }
/** Returns a copy of the Field with the name set. */ public Field withName(String name) { return toBuilder().setName(name).build(); }
/** Return's a nullable field with the give name and type. */ public static Field nullable(String name, FieldType fieldType) { return new AutoValue_Schema_Field.Builder() .setName(name) .setDescription("") .setType(fieldType) .setNullable(true) .build(); }
/** Return's a field with the give name and type. */ public static Field of(String name, FieldType fieldType) { return new AutoValue_Schema_Field.Builder() .setName(name) .setDescription("") .setType(fieldType) .setNullable(false) // By default fields are not nullable. .build(); }