private Object defaultValueFromAvroWithoutLogical()

in schema-converter/avro-schema-converter/src/main/java/org/apache/rocketmq/schema/avro/AvroData.java [2078:2201]


    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.getFieldType() == FieldType.INT8) {
                    return (byte) jsonValue.intValue();
                } else if (schema.getFieldType() == FieldType.INT16) {
                    return jsonValue.shortValue();
                } else if (schema.getFieldType() == FieldType.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 ConnectException("Invalid binary data in default value", e);
                }

            case ARRAY: {
                if (!jsonValue.isArray()) {
                    throw new ConnectException("Invalid JSON for array default value: " + jsonValue);
                }
                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 ConnectException("Invalid JSON for map default value: " + jsonValue);
                }
                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.getValueSchema(), avroSchema.getValueType(), field.getValue(), toConnectContext);
                    result.put(field.getKey(), converted);
                }
                return result;
            }

            case RECORD: {
                if (!jsonValue.isObject()) {
                    throw new ConnectException("Invalid JSON for record default value: " + jsonValue);
                }

                Struct result = new Struct(schema);
                for (org.apache.avro.Schema.Field avroField : avroSchema.getFields()) {
                    Field field = schema.getField(avroField.name());
                    JsonNode fieldJson = ((JsonNode) value).get(field.getName());
                    Object converted = defaultValueFromAvro(
                            field.getSchema(), 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.getField(unionMemberFieldName(memberAvroSchema, enhancedSchemaSupport)).getSchema(),
                            memberAvroSchema,
                            value,
                            toConnectContext);
                }
            }
            default: {
                return null;
            }
        }
        return null;
    }