in generator/src/main/java/org/apache/kafka/message/FieldSpec.java [294:479]
String fieldDefault(HeaderGenerator headerGenerator,
StructRegistry structRegistry) {
if (type instanceof FieldType.BoolFieldType) {
if (fieldDefault.isEmpty()) {
return "false";
} else if (fieldDefault.equalsIgnoreCase("true")) {
return "true";
} else if (fieldDefault.equalsIgnoreCase("false")) {
return "false";
} else {
throw new RuntimeException("Invalid default for boolean field " +
name + ": " + fieldDefault);
}
} else if ((type instanceof FieldType.Int8FieldType) ||
(type instanceof FieldType.Int16FieldType) ||
(type instanceof FieldType.Uint16FieldType) ||
(type instanceof FieldType.Uint32FieldType) ||
(type instanceof FieldType.Int32FieldType) ||
(type instanceof FieldType.Int64FieldType)) {
int base = 10;
String defaultString = fieldDefault;
if (defaultString.startsWith("0x")) {
base = 16;
defaultString = defaultString.substring(2);
}
if (type instanceof FieldType.Int8FieldType) {
if (defaultString.isEmpty()) {
return "(byte) 0";
} else {
try {
Byte.valueOf(defaultString, base);
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid default for int8 field " +
name + ": " + defaultString, e);
}
return "(byte) " + fieldDefault;
}
} else if (type instanceof FieldType.Int16FieldType) {
if (defaultString.isEmpty()) {
return "(short) 0";
} else {
try {
Short.valueOf(defaultString, base);
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid default for int16 field " +
name + ": " + defaultString, e);
}
return "(short) " + fieldDefault;
}
} else if (type instanceof FieldType.Uint16FieldType) {
if (defaultString.isEmpty()) {
return "0";
} else {
try {
int value = Integer.valueOf(defaultString, base);
if (value < 0 || value > MessageGenerator.UNSIGNED_SHORT_MAX) {
throw new RuntimeException("Invalid default for uint16 field " +
name + ": out of range.");
}
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid default for uint16 field " +
name + ": " + defaultString, e);
}
return fieldDefault;
}
} else if (type instanceof FieldType.Uint32FieldType) {
if (defaultString.isEmpty()) {
return "0";
} else {
try {
long value = Long.valueOf(defaultString, base);
if (value < 0 || value > MessageGenerator.UNSIGNED_INT_MAX) {
throw new RuntimeException("Invalid default for uint32 field " +
name + ": out of range.");
}
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid default for uint32 field " +
name + ": " + defaultString, e);
}
return fieldDefault;
}
} else if (type instanceof FieldType.Int32FieldType) {
if (defaultString.isEmpty()) {
return "0";
} else {
try {
Integer.valueOf(defaultString, base);
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid default for int32 field " +
name + ": " + defaultString, e);
}
return fieldDefault;
}
} else if (type instanceof FieldType.Int64FieldType) {
if (defaultString.isEmpty()) {
return "0L";
} else {
try {
Long.valueOf(defaultString, base);
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid default for int64 field " +
name + ": " + defaultString, e);
}
return fieldDefault + "L";
}
} else {
throw new RuntimeException("Unsupported field type " + type);
}
} else if (type instanceof FieldType.UUIDFieldType) {
headerGenerator.addImport(MessageGenerator.UUID_CLASS);
if (fieldDefault.isEmpty()) {
return "Uuid.ZERO_UUID";
} else {
try {
ByteBuffer uuidBytes = ByteBuffer.wrap(Base64.getUrlDecoder().decode(fieldDefault));
uuidBytes.getLong();
uuidBytes.getLong();
} catch (IllegalArgumentException e) {
throw new RuntimeException("Invalid default for uuid field " +
name + ": " + fieldDefault, e);
}
headerGenerator.addImport(MessageGenerator.UUID_CLASS);
return "Uuid.fromString(\"" + fieldDefault + "\")";
}
} else if (type instanceof FieldType.Float64FieldType) {
if (fieldDefault.isEmpty()) {
return "0.0";
} else {
try {
Double.parseDouble(fieldDefault);
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid default for float64 field " +
name + ": " + fieldDefault, e);
}
return "Double.parseDouble(\"" + fieldDefault + "\")";
}
} else if (type instanceof FieldType.StringFieldType) {
if (fieldDefault.equals("null")) {
validateNullDefault();
return "null";
} else {
return "\"" + fieldDefault + "\"";
}
} else if (type.isBytes()) {
if (fieldDefault.equals("null")) {
validateNullDefault();
return "null";
} else if (!fieldDefault.isEmpty()) {
throw new RuntimeException("Invalid default for bytes field " +
name + ". The only valid default for a bytes field " +
"is empty or null.");
}
if (zeroCopy) {
headerGenerator.addImport(MessageGenerator.BYTE_UTILS_CLASS);
return "ByteUtils.EMPTY_BUF";
} else {
headerGenerator.addImport(MessageGenerator.BYTES_CLASS);
return "Bytes.EMPTY";
}
} else if (type.isRecords()) {
return "null";
} else if (type.isStruct()) {
if (fieldDefault.equals("null")) {
validateNullDefault();
return "null";
} else if (!fieldDefault.isEmpty()) {
throw new RuntimeException("Invalid default for struct field " +
name + ". The only valid default for a struct field " +
"is the empty struct or null.");
}
return "new " + type + "()";
} else if (type.isArray()) {
if (fieldDefault.equals("null")) {
validateNullDefault();
return "null";
} else if (!fieldDefault.isEmpty()) {
throw new RuntimeException("Invalid default for array field " +
name + ". The only valid default for an array field " +
"is the empty array or null.");
}
return String.format("new %s(0)",
concreteJavaType(headerGenerator, structRegistry));
} else {
throw new RuntimeException("Unsupported field type " + type);
}
}