Skip to content

Commit

Permalink
start of implementation of Avsc writing for AvroSchema class (#300)
Browse files Browse the repository at this point in the history
Co-authored-by: Radai Rosenblatt <[email protected]>
  • Loading branch information
radai-rosenblatt and Radai Rosenblatt authored Mar 28, 2022
1 parent 162d032 commit 144d4a0
Show file tree
Hide file tree
Showing 31 changed files with 1,133 additions and 118 deletions.
2 changes: 2 additions & 0 deletions parser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ dependencies {
//json-p under its new home @ https://projects.eclipse.org/projects/ee4j.jsonp
api "jakarta.json:jakarta.json-api:2.0.1"
implementation "org.glassfish:jakarta.json:2.0.1"
// implementation project(path: ':helper:helper', configuration: 'shadow')

testImplementation project(":test-common")
testImplementation project(path: ':helper:helper', configuration: 'shadow')
testImplementation "org.apache.avro:avro:1.10.2"
testImplementation "org.skyscreamer:jsonassert:1.5.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public AvroArrayLiteral(AvroArraySchema schema, CodeLocation codeLocation, List<
this.value = value;
}

public List<AvroLiteral> getValue() {
return value;
}

@Override
public String toString() {
StringJoiner csv = new StringJoiner(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ public AvroBooleanLiteral(AvroPrimitiveSchema schema, CodeLocation codeLocation,
}

@Override
protected AvroType expectedType() {
protected AvroType primitiveType() {
return AvroType.BOOLEAN;
}

public boolean getValue() {
return value;
}

@Override
public String toString() {
return Boolean.toString(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public AvroBytesLiteral(AvroPrimitiveSchema schema, CodeLocation codeLocation, b
this.value = value;
}

public byte[] getValue() {
return value;
}

@Override
protected AvroType expectedType() {
protected AvroType primitiveType() {
return AvroType.BYTES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public AvroDoubleLiteral(AvroPrimitiveSchema schema, CodeLocation codeLocation,
this.value = value;
}

public double getValue() {
return value;
}

@Override
protected AvroType expectedType() {
protected AvroType primitiveType() {
return AvroType.DOUBLE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public AvroEnumLiteral(AvroEnumSchema schema, CodeLocation codeLocation, String
this.value = value;
}

public String getValue() {
return value;
}

@Override
public String toString() {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class AvroEnumSchema extends AvroNamedSchema {

public AvroEnumSchema(
CodeLocation codeLocation,
String simpleName,
String namespace,
AvroName name,
List<AvroName> aliases,
String doc,
List<String> symbols,
String defaultSymbol,
JsonPropertiesContainer props
) {
super(codeLocation, simpleName, namespace, doc, props);
super(codeLocation, name, aliases, doc, props);
//TODO - check for dup symbols, same-name-different-case, etc
//TODO - check default (if exists) is a symbol
this.symbols = Collections.unmodifiableList(new ArrayList<>(symbols));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public AvroFixedLiteral(AvroFixedSchema schema, CodeLocation codeLocation, byte[
this.value = value;
}

public byte[] getValue() {
return value;
}

@Override
public String toString() {
//TODO - hex encode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

package com.linkedin.avroutil1.model;

import java.util.List;


public class AvroFixedSchema extends AvroNamedSchema {
private final int size;
private final AvroLogicalType logicalType;

public AvroFixedSchema(
CodeLocation codeLocation,
String simpleName,
String namespace,
AvroName name,
List<AvroName> aliases,
String doc,
int size,
AvroLogicalType logicalType,
JsonPropertiesContainer props
) {
super(codeLocation, simpleName, namespace, doc, props);
super(codeLocation, name, aliases, doc, props);
if (logicalType != null && !logicalType.getParentTypes().contains(type())) {
throw new IllegalArgumentException(type() + " " + simpleName + " at " + codeLocation
throw new IllegalArgumentException(type() + " " + getSimpleName() + " at " + codeLocation
+ " cannot have a logical type of " + logicalType + " (which can only be a logical type of "
+ logicalType.getParentTypes() + ")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public AvroFloatLiteral(AvroPrimitiveSchema schema, CodeLocation codeLocation, f
this.value = value;
}

public float getValue() {
return value;
}

@Override
protected AvroType expectedType() {
protected AvroType primitiveType() {
return AvroType.FLOAT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public AvroIntegerLiteral(AvroPrimitiveSchema schema, CodeLocation codeLocation,
this.value = value;
}

public int getValue() {
return value;
}

@Override
protected AvroType expectedType() {
protected AvroType primitiveType() {
return AvroType.INT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,53 @@

package com.linkedin.avroutil1.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
* represents the possible ways avro code generation may represent String fields
* in generated java code.
* specified by setting the "avro.java.string" property on string types in schemas
*/
public enum AvroJavaStringRepresentation {
CHAR_SEQUENCE, STRING, UTF8;
CHAR_SEQUENCE("CharSequence"), STRING("String"), UTF8("Utf8");
private final String jsonValue;

public String getJsonValue() {
switch (this) {
case CHAR_SEQUENCE:
return "CharSequence";
case STRING:
return "String";
case UTF8:
return "Utf8";
AvroJavaStringRepresentation(String jsonValue) {
if (jsonValue == null || jsonValue.isEmpty()) {
throw new IllegalArgumentException("jsonValue required");
}
this.jsonValue = jsonValue;
}

private final static List<String> LEGAL_JSON_VALUES;
static {
List<String> temp = new ArrayList<>(values().length);
for (AvroJavaStringRepresentation rep : values()) {
temp.add(rep.getJsonValue());
}
throw new IllegalStateException("unhandled: " + this);
LEGAL_JSON_VALUES = Collections.unmodifiableList(temp);
}

public String getJsonValue() {
return jsonValue;
}

public static AvroJavaStringRepresentation fromJson(String jsonStringRepStr) {
if (jsonStringRepStr == null || jsonStringRepStr.isEmpty()) {
return null;
}
switch (jsonStringRepStr) {
//CharSequence, String, Utf8
case "CharSequence":
return CHAR_SEQUENCE;
case "String":
return STRING;
case "Utf8":
return UTF8;
default:
return null;
for (AvroJavaStringRepresentation candidate : values()) {
if (candidate.jsonValue.equals(jsonStringRepStr)) {
return candidate;
}
}
return null;
}

public static List<String> legalJsonValues() {
return LEGAL_JSON_VALUES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public AvroLiteral(AvroSchema schema, CodeLocation codeLocation) {
this.codeLocation = codeLocation;
}

public AvroSchema getSchema() {
return schema;
}

public AvroType type() {
return schema.type();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public AvroLongLiteral(AvroPrimitiveSchema schema, CodeLocation codeLocation, lo
this.value = value;
}

public long getValue() {
return value;
}

@Override
protected AvroType expectedType() {
protected AvroType primitiveType() {
return AvroType.LONG;
}

Expand Down
80 changes: 80 additions & 0 deletions parser/src/main/java/com/linkedin/avroutil1/model/AvroName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2022 LinkedIn Corp.
* Licensed under the BSD 2-Clause License (the "License").
* See License in the project root for license information.
*/

package com.linkedin.avroutil1.model;

import java.util.Objects;


public class AvroName {
private final static String NO_NAMESPACE = "";

private final String simpleName;
private final String namespace;
private final String fullname;

public AvroName(String simpleName, String namespace) {
if (simpleName == null || simpleName.isEmpty()) {
throw new IllegalArgumentException("simple name required");
}
if (simpleName.contains(".")) {
throw new IllegalArgumentException("simple name must be simple: " + simpleName);
}
this.simpleName = simpleName;
if (namespace != null && !namespace.isEmpty()) {
this.namespace = namespace;
this.fullname = namespace + "." + simpleName;
} else {
this.namespace = NO_NAMESPACE;
this.fullname = this.simpleName;
}
}

public String getSimpleName() {
return simpleName;
}

public String getNamespace() {
return namespace;
}

public String getFullname() {
return fullname;
}

public boolean hasNamespace() {
return !NO_NAMESPACE.equals(namespace);
}

public String qualified(String contextNamespace) {
if (!namespace.equals(contextNamespace)) { //also works if argument is null
return fullname;
}
return simpleName;
}

@Override
public String toString() {
return fullname;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AvroName avroName = (AvroName) o;
return Objects.equals(fullname, avroName.fullname);
}

@Override
public int hashCode() {
return Objects.hash(fullname);
}
}
Loading

0 comments on commit 144d4a0

Please sign in to comment.