Skip to content

Commit

Permalink
feat(avro schema): schemas required methods and basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed Apr 15, 2024
1 parent dda9b18 commit 1018622
Show file tree
Hide file tree
Showing 11 changed files with 1,152 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
package com.asyncapi.v3.schema.avro;

import com.asyncapi.v3.schema.avro.jackson.AvroRecordFieldSchemaTypeDeserializer;
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;

/**
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#Arrays">Arrays</a>
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class AvroArraySchema extends AvroSchema {

public AvroArraySchema() {
super(AvroSchemaType.ARRAY);
}

@Builder(builderMethodName = "arrayBuilder")
public AvroArraySchema(
@NotNull Object items
) {
super(AvroSchemaType.ARRAY);
this.items = items;
}

@NotNull
@JsonProperty("items")
private String items;
@JsonDeserialize(using = AvroRecordFieldSchemaTypeDeserializer.class)
private Object items;

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

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

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.asyncapi.v3.schema.avro;

import com.asyncapi.v3.schema.avro.jackson.AvroRecordFieldSchemaTypeDeserializer;
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;

Expand All @@ -12,12 +17,33 @@
*
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#Enums">Enums</a>
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class AvroEnumSchema extends AvroSchema {

public AvroEnumSchema() {
super(AvroSchemaType.ENUM);
}

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

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

@NotNull
@JsonProperty("name")
private String name = "";
Expand Down Expand Up @@ -59,6 +85,7 @@ public AvroEnumSchema() {
*/
@Nullable
@JsonProperty("default")
private String defaultValue;
@JsonDeserialize(using = AvroRecordFieldSchemaTypeDeserializer.class)
private Object defaultValue;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.asyncapi.v3.schema.avro;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -9,12 +12,29 @@
/**
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#Arrays">Arrays</a>
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class AvroFixedSchema extends AvroSchema {

public AvroFixedSchema() {
super(AvroSchemaType.FIXED);
}

@Builder(builderMethodName = "fixedBuilder")
public AvroFixedSchema(
@NotNull String name,
@Nullable String namespace,
@Nullable List<@NotNull String> aliases,
@NotNull Integer size
) {
super(AvroSchemaType.FIXED);

this.name = name;
this.namespace = namespace;
this.aliases = aliases;
this.size = size;
}

@NotNull
@JsonProperty("name")
private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
package com.asyncapi.v3.schema.avro;

import com.asyncapi.v3.schema.avro.jackson.AvroRecordFieldSchemaTypeDeserializer;
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.Map;

/**
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#Maps">Maps</a>
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class AvroMapSchema extends AvroSchema {

public AvroMapSchema() {
super(AvroSchemaType.MAP);
}

public AvroMapSchema(@NotNull Object values) {
this.values = values;
}

@Builder(builderMethodName = "mapBuilder")
public AvroMapSchema(@NotNull Object values, @Nullable Map<String, Object> metadata) {
this.values = values;
this.metadata = metadata;
}

@NotNull
@JsonProperty("values")
private String values;
@JsonDeserialize(using = AvroRecordFieldSchemaTypeDeserializer.class)
private Object values;

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

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.asyncapi.v3.schema.avro;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({"metadata"})
public class AvroMetadata {

@Nullable
@JsonAnyGetter
protected Map<String, Object> metadata;

@JsonAnySetter
protected final void readMetadata(String name, Object value) {
if (metadata == null) {
metadata = new HashMap<>();
}

metadata.put(name, value);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.asyncapi.v3.schema.avro;

import com.asyncapi.v3.schema.avro.jackson.AvroRecordFieldSchemaTypeDeserializer;
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 Record Field.
Expand All @@ -14,17 +19,39 @@
* @version 3.0.0
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#schema_record">Avro Record</a>
*/
public class AvroRecordFieldSchema extends AvroSchema {
@Data
@EqualsAndHashCode(callSuper = true)
public class AvroRecordFieldSchema extends AvroMetadata {

public AvroRecordFieldSchema() {
super(AvroSchemaType.RECORD);
this.type = AvroSchemaType.RECORD;
}

@Builder
public AvroRecordFieldSchema(
@NotNull Object type,
@NotNull String name,
@Nullable Order order,
@Nullable String doc,
@Nullable List<@NotNull String> aliases,
@Nullable Object defaultValue,
@Nullable Map<String, Object> metadata
) {
this.type = type;
this.name = name;
this.order = order == null ? Order.ASCENDING : order ;
this.doc = doc;
this.aliases = aliases;
this.defaultValue = defaultValue;
this.metadata = metadata;
}

/**
* Field type.
*/
@NotNull
@JsonProperty("type")
@JsonDeserialize(using = AvroRecordFieldSchemaTypeDeserializer.class)
private Object type;

/**
Expand All @@ -37,9 +64,23 @@ public AvroRecordFieldSchema() {
/**
* Specifies how this field impacts sort ordering of this record (optional).
*/
@Nullable("order")
@Nullable
@JsonProperty("order")
private Order order = Order.ASCENDING;

@NotNull
public Order getOrder() {
if (order == null) {
setOrder(Order.ASCENDING);
}

return order;
}

public void setOrder(@NotNull Order order) {
this.order = order;
}

/**
* A JSON string providing documentation to the user of this schema (optional).
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.asyncapi.v3.schema.avro;

import lombok.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -13,12 +14,36 @@
* @version 3.0.0
* @see <a href="https://avro.apache.org/docs/1.9.0/spec.html#schema_record">Avro Record</a>
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class AvroRecordSchema extends AvroSchema {

public AvroRecordSchema() {
super(AvroSchemaType.RECORD);
}

@Builder(builderMethodName = "recordBuilder")
public AvroRecordSchema(
@Nullable AvroSchemaType type,
@NotNull String name,
@Nullable String namespace,
@Nullable String doc,
@Nullable List<@NotNull String> aliases,
@NotNull List<@NotNull AvroRecordFieldSchema> fields
) {
if (AvroSchemaType.RECORD.equals(type) || AvroSchemaType.ERROR.equals(type)) {
super.setType(type);
} else {
super.setType(AvroSchemaType.RECORD);
}

this.name = name;
this.namespace = namespace;
this.doc = doc;
this.aliases = aliases;
this.fields = fields;
}

/**
* A JSON string providing the name of the record (required).
*/
Expand Down Expand Up @@ -49,4 +74,14 @@ public AvroRecordSchema() {
@NotNull
private List<@NotNull AvroRecordFieldSchema> fields = Collections.emptyList();

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

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

}
Loading

0 comments on commit 1018622

Please sign in to comment.