Skip to content

Commit

Permalink
feat(avro schema): new Builder hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed Apr 18, 2024
1 parent 2319ae9 commit 6caac9c
Show file tree
Hide file tree
Showing 9 changed files with 604 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

/**
* Apache Avro Schema.
*
Expand All @@ -15,9 +18,6 @@
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html">Avro Specification</a>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
Expand All @@ -39,10 +39,22 @@
})
public class Avro extends AvroMetadata {

public Avro() {
this.type = AvroType.NULL;
}

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

public Avro(@NotNull Builder<?, ?> builder) {
this.type = builder.type;
this.scale = builder.scale;
this.precision = builder.precision;
this.logicalType = builder.logicalType;
super.metadata = builder.metadata;
}

/**
* Avro Schema type.
*/
Expand All @@ -62,6 +74,81 @@ public Avro(@NotNull String avroType) {
@Nullable
private String logicalType;

public static Builder<?, ?> builder() {

return new Builder() {

@NotNull
@Override
protected Builder getThis() {
return this;
}

@NotNull
@Override
public Avro build() {
return new Avro(this);
}

};

}

public static abstract class Builder<AvroSchemaVariant extends Avro, BuilderVariant extends Builder<AvroSchemaVariant, BuilderVariant>> {

@NotNull
protected String type = AvroType.NULL;

@Nullable
private Integer scale;

@Nullable
private Integer precision;

@Nullable
private String logicalType;

@Nullable
protected Map<String, Object> metadata;

@NotNull
public BuilderVariant type(@NotNull String type) {
this.type = type;
return getThis();
}

@NotNull
public BuilderVariant scale(@Nullable Integer scale) {
this.scale = scale;
return getThis();
}

@NotNull
public BuilderVariant precision(@Nullable Integer precision) {
this.precision = precision;
return getThis();
}

@NotNull
public BuilderVariant logicalType(@Nullable String logicalType) {
this.logicalType = logicalType;
return getThis();
}

@NotNull
public BuilderVariant metadata(@Nullable Map<String, Object> metadata) {
this.metadata = metadata;
return getThis();
}

@NotNull
protected abstract BuilderVariant getThis();

@NotNull
public abstract AvroSchemaVariant build();

}

public static class LogicalType {

@JsonProperty("decimal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.asyncapi.v3.schema.avro.v1._9_0.jackson.AvroTypeDeserializer;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#Arrays">Arrays</a>
Expand All @@ -27,14 +28,23 @@ public AvroArray(@NotNull Object items) {
this.items = items;
}

@Builder(builderMethodName = "arrayBuilder")
public AvroArray(
@NotNull Object items,
@Nullable List<Object> defaultValue
@Nullable List<Object> defaultValue,
@Nullable Map<String, Object> metadata
) {
super(AvroType.ARRAY);
this.items = items;
this.defaultValue = defaultValue;
this.metadata = metadata;
}

public AvroArray(@NotNull Builder builder) {
super(AvroType.ARRAY);

this.items = builder.items;
this.defaultValue = builder.defaultValue;
this.metadata = builder.metadata;
}

@NotNull
Expand All @@ -56,4 +66,42 @@ public void setType(@NotNull String type) {
super.setType(AvroType.ARRAY);
}

public static Builder builder() {
return new Builder();
}

public static class Builder extends Avro.Builder<AvroArray, Builder> {

@NotNull
private Object items = Collections.emptyList();

@Nullable
private List<Object> defaultValue;

@NotNull
public Builder items(@NotNull Object items) {
this.items = items;
return this;
}

@NotNull
public Builder defaultValue(@NotNull List<Object> defaultValue) {
this.defaultValue = defaultValue;
return this;
}

@NotNull
@Override
protected Builder getThis() {
return this;
}

@NotNull
@Override
public AvroArray build() {
return new AvroArray(this);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import com.asyncapi.v3.schema.avro.v1._9_0.jackson.AvroTypeDeserializer;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Avro Enum Schema
Expand All @@ -25,14 +25,14 @@ public AvroEnum() {
super(AvroType.ENUM);
}

@Builder(builderMethodName = "enumBuilder")
public AvroEnum(
@NotNull String name,
@Nullable String namespace,
@Nullable String doc,
@NotNull List<@NotNull String> symbols,
@Nullable List<@NotNull String> aliases,
@Nullable Object defaultValue
@Nullable Object defaultValue,
@Nullable Map<String, Object> metadata
) {
super(AvroType.ENUM);

Expand All @@ -42,6 +42,19 @@ public AvroEnum(
this.symbols = symbols;
this.aliases = aliases;
this.defaultValue = defaultValue;
this.metadata = metadata;
}

public AvroEnum(@NotNull Builder builder) {
super(AvroType.ENUM);

this.name = builder.name;
this.namespace = builder.namespace;
this.doc = builder.doc;
this.symbols = builder.symbols;
this.aliases = builder.aliases;
this.defaultValue = builder.defaultValue;
this.metadata = builder.metadata;
}

@NotNull
Expand Down Expand Up @@ -88,4 +101,88 @@ public AvroEnum(
@JsonDeserialize(using = AvroTypeDeserializer.class)
private Object defaultValue;

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

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

public static Builder builder() {
return new Builder();
}

public static class Builder extends Avro.Builder<AvroEnum, Builder> {

@NotNull
private String name = "";

@Nullable
private String namespace;

@Nullable
private String doc;

@NotNull
private List<@NotNull String> symbols = Collections.emptyList();

@Nullable
private List<@NotNull String> aliases;

@Nullable
private Object defaultValue;

@NotNull
public Builder name(@NotNull String name) {
this.name = name;
return this;
}

@NotNull
public Builder namespace(@Nullable String namespace) {
this.namespace = namespace;
return this;
}

@NotNull
public Builder doc(@Nullable String doc) {
this.doc = doc;
return this;
}

@NotNull
public Builder symbols(@NotNull List<@NotNull String> symbols) {
this.symbols = symbols;
return this;
}

@NotNull
public Builder aliases(@NotNull List<@NotNull String> aliases) {
this.aliases = aliases;
return this;
}

@NotNull
public Builder defaultValue(@Nullable Object defaultValue) {
this.defaultValue = defaultValue;
return this;
}

@NotNull
@Override
protected Builder getThis() {
return this;
}

@NotNull
@Override
public AvroEnum build() {
return new AvroEnum(this);
}

}

}
Loading

0 comments on commit 6caac9c

Please sign in to comment.