in avro-kafkaconnect-converter/src/main/java/com/amazonaws/services/schemaregistry/kafkaconnect/avrodata/AvroData.java [1886:2009]
private Object defaultValueFromAvroWithoutLogical(Schema schema,
org.apache.avro.Schema avroSchema,
Object value,
ToConnectContext toConnectContext) {
if (value == null || value == JsonProperties.NULL_VALUE) {
return null;
}
// The type will be JsonNode if this default was pulled from a Connect default field, or an
// Object if it's the actual Avro-specified default. If it's a regular Java object, we can
// use our existing conversion tools.
if (!(value instanceof JsonNode)) {
return toConnectData(schema, value, toConnectContext, false);
}
JsonNode jsonValue = (JsonNode) value;
switch (avroSchema.getType()) {
case INT:
if (schema.type() == Schema.Type.INT8) {
return (byte) jsonValue.intValue();
} else if (schema.type() == Schema.Type.INT16) {
return jsonValue.shortValue();
} else if (schema.type() == Schema.Type.INT32) {
return jsonValue.intValue();
} else {
break;
}
case LONG:
return jsonValue.longValue();
case FLOAT:
return (float) jsonValue.doubleValue();
case DOUBLE:
return jsonValue.doubleValue();
case BOOLEAN:
return jsonValue.asBoolean();
case NULL:
return null;
case STRING:
case ENUM:
return jsonValue.asText();
case BYTES:
case FIXED:
try {
byte[] bytes;
if (jsonValue.isTextual()) {
// Avro's JSON form may be a quoted string, so decode the binary value
String encoded = jsonValue.textValue();
bytes = encoded.getBytes(StandardCharsets.ISO_8859_1);
} else {
bytes = jsonValue.binaryValue();
}
return bytes == null ? null : ByteBuffer.wrap(bytes);
} catch (IOException e) {
throw new DataException("Invalid binary data in default value", e);
}
case ARRAY: {
if (!jsonValue.isArray()) {
throw new DataException("Invalid JSON for array default value: " + jsonValue.toString());
}
List<Object> result = new ArrayList<>(jsonValue.size());
for (JsonNode elem : jsonValue) {
result.add(
defaultValueFromAvro(schema, avroSchema.getElementType(), elem, toConnectContext));
}
return result;
}
case MAP: {
if (!jsonValue.isObject()) {
throw new DataException("Invalid JSON for map default value: " + jsonValue.toString());
}
Map<String, Object> result = new HashMap<>(jsonValue.size());
Iterator<Map.Entry<String, JsonNode>> fieldIt = jsonValue.fields();
while (fieldIt.hasNext()) {
Map.Entry<String, JsonNode> field = fieldIt.next();
Object converted = defaultValueFromAvro(
schema.valueSchema(), avroSchema.getValueType(), field.getValue(), toConnectContext);
result.put(field.getKey(), converted);
}
return result;
}
case RECORD: {
if (!jsonValue.isObject()) {
throw new DataException("Invalid JSON for record default value: " + jsonValue.toString());
}
Struct result = new Struct(schema);
for (org.apache.avro.Schema.Field avroField : avroSchema.getFields()) {
Field field = schema.field(avroField.name());
JsonNode fieldJson = ((JsonNode) value).get(field.name());
Object converted = defaultValueFromAvro(
field.schema(), avroField.schema(), fieldJson, toConnectContext);
result.put(avroField.name(), converted);
}
return result;
}
case UNION: {
// Defaults must match first type
org.apache.avro.Schema memberAvroSchema = avroSchema.getTypes().get(0);
if (memberAvroSchema.getType() == org.apache.avro.Schema.Type.NULL) {
return null;
} else {
return defaultValueFromAvro(
schema.field(unionMemberFieldName(memberAvroSchema)).schema(),
memberAvroSchema,
value,
toConnectContext);
}
}
default: {
return null;
}
}
return null;
}