in generator/src/main/java/org/apache/kafka/message/FieldSpec.java [64:143]
public FieldSpec(@JsonProperty("name") String name,
@JsonProperty("versions") String versions,
@JsonProperty("fields") List<FieldSpec> fields,
@JsonProperty("type") String type,
@JsonProperty("mapKey") boolean mapKey,
@JsonProperty("nullableVersions") String nullableVersions,
@JsonProperty("default") String fieldDefault,
@JsonProperty("ignorable") boolean ignorable,
@JsonProperty("entityType") EntityType entityType,
@JsonProperty("about") String about,
@JsonProperty("taggedVersions") String taggedVersions,
@JsonProperty("flexibleVersions") String flexibleVersions,
@JsonProperty("tag") Integer tag,
@JsonProperty("zeroCopy") boolean zeroCopy) {
this.name = Objects.requireNonNull(name);
if (!VALID_FIELD_NAMES.matcher(this.name).matches()) {
throw new RuntimeException("Invalid field name " + this.name);
}
this.taggedVersions = Versions.parse(taggedVersions, Versions.NONE);
// If versions is not set, but taggedVersions is, default to taggedVersions.
this.versions = Versions.parse(versions, this.taggedVersions.empty() ?
null : this.taggedVersions);
if (this.versions == null) {
throw new RuntimeException("You must specify the version of the " +
name + " structure.");
}
this.fields = Collections.unmodifiableList(fields == null ?
Collections.emptyList() : new ArrayList<>(fields));
this.type = FieldType.parse(Objects.requireNonNull(type));
this.mapKey = mapKey;
this.nullableVersions = Versions.parse(nullableVersions, Versions.NONE);
if (!this.nullableVersions.empty()) {
if (!this.type.canBeNullable()) {
throw new RuntimeException("Type " + this.type + " cannot be nullable.");
}
}
this.fieldDefault = fieldDefault == null ? "" : fieldDefault;
this.ignorable = ignorable;
this.entityType = (entityType == null) ? EntityType.UNKNOWN : entityType;
this.entityType.verifyTypeMatches(name, this.type);
this.about = about == null ? "" : about;
if (!this.fields().isEmpty()) {
if (!this.type.isArray() && !this.type.isStruct()) {
throw new RuntimeException("Non-array or Struct field " + name + " cannot have fields");
}
// Check struct invariants
if (this.type.isStruct() || this.type.isStructArray()) {
new StructSpec(name,
versions,
Versions.NONE_STRING, // version deprecations not supported at field level
fields);
}
}
if (flexibleVersions == null || flexibleVersions.isEmpty()) {
this.flexibleVersions = Optional.empty();
} else {
this.flexibleVersions = Optional.of(Versions.parse(flexibleVersions, null));
if (!(this.type.isString() || this.type.isBytes())) {
// For now, only allow flexibleVersions overrides for the string and bytes
// types. Overrides are only needed to keep compatibility with some old formats,
// so there isn't any need to support them for all types.
throw new RuntimeException("Invalid flexibleVersions override for " + name +
". Only fields of type string or bytes can specify a flexibleVersions " +
"override.");
}
}
this.tag = Optional.ofNullable(tag);
if (this.tag.isPresent() && mapKey) {
throw new RuntimeException("Tagged fields cannot be used as keys.");
}
checkTagInvariants();
this.zeroCopy = zeroCopy;
if (this.zeroCopy && !this.type.isBytes()) {
throw new RuntimeException("Invalid zeroCopy value for " + name +
". Only fields of type bytes can use zeroCopy flag.");
}
}