Skip to content

Commit

Permalink
refactor(avro schema): Avro Union new constructor + types now are string
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed Apr 17, 2024
1 parent 87e7d32 commit 2c630c6
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@
)
@JsonSubTypes({
@JsonSubTypes.Type(value = Avro.class, names = {
"null", "boolean", "int", "long", "float", "double", "bytes", "string"
AvroType.NULL, AvroType.BOOLEAN, AvroType.INT, AvroType.LONG,
AvroType.FLOAT, AvroType.DOUBLE, AvroType.BYTES, AvroType.STRING
}),
@JsonSubTypes.Type(value = AvroRecord.class, names = {"record", "error"}),
@JsonSubTypes.Type(value = AvroArray.class, name = "array"),
@JsonSubTypes.Type(value = AvroMap.class, name = "map"),
@JsonSubTypes.Type(value = AvroEnum.class, name = "enum"),
@JsonSubTypes.Type(value = AvroFixed.class, name = "fixed"),
@JsonSubTypes.Type(value = AvroRecord.class, names = {AvroType.RECORD, AvroType.ERROR}),
@JsonSubTypes.Type(value = AvroArray.class, name = AvroType.ARRAY),
@JsonSubTypes.Type(value = AvroMap.class, name = AvroType.MAP),
@JsonSubTypes.Type(value = AvroEnum.class, name = AvroType.ENUM),
@JsonSubTypes.Type(value = AvroFixed.class, name = AvroType.FIXED),
})
public class Avro extends AvroMetadata {

public Avro(@NotNull AvroType avroType) {
public Avro(@NotNull String avroType) {
this.type = avroType;
}

/**
* Avro Schema type.
*/
@NotNull
private AvroType type;
private String type;

@Nullable
private Integer scale;
Expand All @@ -59,36 +60,36 @@ public Avro(@NotNull AvroType avroType) {
private Integer precision;

@Nullable
private LogicalType logicalType;
private String logicalType;

public enum LogicalType {
public static class LogicalType {

@JsonProperty("decimal")
DECIMAL,
public static final String DECIMAL = "decimal";

@JsonProperty("big-decimal")
BIG_DECIMAL,
public static final String BIG_DECIMAL = "big-decimal";

@JsonProperty("uuid")
UUID,
public static final String UUID = "uuid";

@JsonProperty("date")
DATE,
public static final String DATE = "date";

@JsonProperty("time-millis")
TIME_MILLIS,
public static final String TIME_MILLIS = "time-millis";

@JsonProperty("time-micros")
TIME_MICROS,
public static final String TIME_MICROS = "time-micros";

@JsonProperty("timestamp-millis")
TIMESTAMP_MILLIS,
public static final String TIMESTAMP_MILLIS = "timestamp-millis";

@JsonProperty("timestamp-micros")
TIMESTAMP_MICROS,
public static final String TIMESTAMP_MICROS = "timestamp-micros";

@JsonProperty("duration")
DURATION
public static final String DURATION = "duration";

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public AvroArray(

@NotNull
@Override
public AvroType getType() {
public String getType() {
return AvroType.ARRAY;
}

public void setType(@NotNull AvroType type) {
public void setType(@NotNull String type) {
super.setType(AvroType.ARRAY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public AvroMap(

@NotNull
@Override
public AvroType getType() {
public String getType() {
return AvroType.MAP;
}

public void setType(@NotNull AvroType type) {
public void setType(@NotNull String type) {
super.setType(AvroType.MAP);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public AvroRecord() {

@Builder(builderMethodName = "recordBuilder")
public AvroRecord(
@Nullable AvroType type,
@Nullable String type,
@NotNull String name,
@Nullable String namespace,
@Nullable String doc,
Expand Down Expand Up @@ -76,11 +76,11 @@ public AvroRecord(

@NotNull
@Override
public AvroType getType() {
public String getType() {
return AvroType.RECORD;
}

public void setType(@NotNull AvroType type) {
public void setType(@NotNull String type) {
super.setType(AvroType.RECORD);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,67 @@
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#schema_primitive">Avro Schema Primitive Types</a>
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#schema_complex">Avro Schema Complex Types</a>
*/
public enum AvroType {
public class AvroType {

/*
Primitive Types.
*/

@JsonProperty("null")
@JsonPropertyDescription("no value")
NULL,
public static final String NULL = "null";

@JsonProperty("boolean")
@JsonPropertyDescription("a binary value")
BOOLEAN,
public static final String BOOLEAN = "boolean";

@JsonProperty("int")
@JsonPropertyDescription("32-bit signed integer")
INT,
public static final String INT = "int";

@JsonProperty("long")
@JsonPropertyDescription("64-bit signed integer")
LONG,
public static final String LONG = "long";

@JsonProperty("float")
@JsonPropertyDescription("single precision (32-bit) IEEE 754 floating-point number")
FLOAT,
public static final String FLOAT = "float";

@JsonProperty("double")
@JsonPropertyDescription("double precision (64-bit) IEEE 754 floating-point number")
DOUBLE,
public static final String DOUBLE = "double";

@JsonProperty("bytes")
@JsonPropertyDescription("sequence of 8-bit unsigned bytes")
BYTES,
public static final String BYTES = "bytes";

@JsonProperty("string")
@JsonPropertyDescription("unicode character sequence")
STRING,
public static final String STRING = "string";

/*
Complex Types.
*/

@JsonProperty("record")
RECORD,
public static final String RECORD = "record";

@JsonProperty("error")
ERROR,
public static final String ERROR = "error";

@JsonProperty("enum")
ENUM,
public static final String ENUM = "enum";

@JsonProperty("array")
ARRAY,
public static final String ARRAY = "array";

@JsonProperty("map")
MAP,
public static final String MAP = "map";

@JsonProperty("unions")
UNIONS,
public static final String UNIONS = "unions";

@JsonProperty("fixed")
FIXED;
public static final String FIXED = "fixed";

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.LinkedList;

/**
Expand Down Expand Up @@ -39,6 +40,11 @@ public AvroUnion() {
super();
}

public AvroUnion(@NotNull Object ...variant) {
super();
addAll(Arrays.asList(variant));
}

public AvroUnion(@NotNull Object variantA, @NotNull Object variantB) {
super();
add(0, variantA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private Object chooseKnownPojo(JsonNode jsonNode, final ObjectCodec objectCodec)
case OBJECT:
return jsonParser.readValueAs(Avro.class);
case STRING:
return jsonParser.readValueAs(AvroType.class);
return jsonParser.readValueAs(String.class);
case BINARY:
case POJO:
case MISSING:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.asyncapi.v3.schema.avro

import com.asyncapi.v3.schema.avro.v1._9_0.*
import com.asyncapi.v3.schema.avro.v1._9_0.Avro.LogicalType

class AvroSchemasProvider {

Expand Down Expand Up @@ -870,4 +871,37 @@ class AvroSchemasProvider {
)
}

fun unionAndFixedFields(): AvroRecord {
return AvroRecord.recordBuilder()
.name("UnionAndFixedFields")
.doc("Schema for UnionAndFixedFields designed to trigger fixed compiler warnings in generated code")
.namespace("org.apache.avro.specific")
.fields(listOf(
AvroRecordField.builder()
.name("u")
.type(AvroUnion(
AvroType.BOOLEAN, AvroType.INT, AvroType.LONG, AvroType.FLOAT, AvroType.STRING
))
.build(),
AvroRecordField.builder()
.name("l")
.type(AvroUnion(
AvroType.STRING, Avro.builder()
.type(AvroType.LONG)
.logicalType(LogicalType.TIMESTAMP_MILLIS)
.build()
))
.build(),
AvroRecordField.builder()
.name("f")
.type(AvroFixed.fixedBuilder()
.name("md5")
.size(16)
.build()
)
.build()
))
.build()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AvroSchemaTest {

override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> {
return Stream.of(
// Arguments.of("/json/v3/schema/avro/ApplicationEvent.avsc", AvroSchemasProvider().applicationEventTest()), // TODO: fix - Cannot deserialize value of type `com.asyncapi.v3.schema.avro.v1._9_0.AvroSchemaType` from String "model.DocumentInfo"
Arguments.of("/json/v3/schema/avro/ApplicationEvent.avsc", Avro::class.java, AvroSchemasProvider().applicationEventTest()),
Arguments.of("/json/v3/schema/avro/DocumentInfo.avsc", Avro::class.java, AvroSchemasProvider().documentInfo()),
Arguments.of("/json/v3/schema/avro/foo.Bar.avsc", Avro::class.java, AvroSchemasProvider().fooBar()),
Arguments.of("/json/v3/schema/avro/full_record_v1.avsc", Avro::class.java, AvroSchemasProvider().fullRecordV1()),
Expand All @@ -58,6 +58,7 @@ class AvroSchemaTest {
Arguments.of("/json/v3/schema/avro/TestRecordWithLogicalTypes.avsc", Avro::class.java, AvroSchemasProvider().testRecordWithLogicalTypes()),
Arguments.of("/json/v3/schema/avro/TestRecordWithMapsAndArrays.avsc", Avro::class.java, AvroSchemasProvider().testRecordWithMapsAndArrays()),
Arguments.of("/json/v3/schema/avro/TestUnionRecord.avsc", AvroUnion::class.java, AvroSchemasProvider().testUnionRecord()),
Arguments.of("/json/v3/schema/avro/union_and_fixed_fields.avsc", Avro::class.java, AvroSchemasProvider().unionAndFixedFields()),
)
}

Expand Down

0 comments on commit 2c630c6

Please sign in to comment.