Skip to content

Commit

Permalink
initial support for field properties in FieldBuilder (#162)
Browse files Browse the repository at this point in the history
addresses #160
  • Loading branch information
radai-rosenblatt authored Jun 18, 2021
1 parent a1fada4 commit d99099d
Show file tree
Hide file tree
Showing 20 changed files with 470 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ BinaryDecoder newBinaryDecoder(byte[] bytes, int offset,

Object getGenericDefaultValue(Schema.Field field);

//schema query and manipulation utils

boolean fieldHasDefault(Schema.Field field);

FieldBuilder cloneSchemaField(Schema.Field field);

FieldBuilder newFieldBuilder(String name);

String getFieldPropAsJsonString(Schema.Field field, String propName);

//code generation

Collection<AvroGeneratedSourceCode> compile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public interface FieldBuilder {

FieldBuilder setOrder(Order order);

@Deprecated
FieldBuilder copyFromField();

Schema.Field build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,16 @@ public static Schema.Field createSchemaField(String name, Schema schema, String
public static Schema.Field createSchemaField(String name, Schema schema, String doc, Object defaultValue) {
return createSchemaField(name, schema, doc, defaultValue, Schema.Field.Order.ASCENDING);
}

/**
* returns the value of the specified field prop as a json literal.
* returns null if the field has no such property.
* note that string values are returned quoted (as a proper json string literal)
* @param field the field who's property value we wish to get
* @param propName the name of the property
* @return field property value as json literal, or null if no such property
*/
public static String getFieldPropAsJsonString(Schema.Field field, String propName) {
return ADAPTER.getFieldPropAsJsonString(field, propName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package com.linkedin.avroutil1.compatibility.avro110;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkedin.avroutil1.compatibility.AvroAdapter;
import com.linkedin.avroutil1.compatibility.AvroGeneratedSourceCode;
import com.linkedin.avroutil1.compatibility.AvroVersion;
Expand Down Expand Up @@ -34,6 +36,7 @@
import org.apache.avro.io.JsonDecoder;
import org.apache.avro.io.JsonEncoder;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.internal.JacksonUtils;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -54,6 +57,9 @@

public class Avro110Adapter implements AvroAdapter {

//doc says thread safe outside config methods
private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private boolean compilerSupported;
private Constructor<?> specificCompilerCtr;
private Method compilerEnqueueMethod;
Expand Down Expand Up @@ -260,6 +266,21 @@ public FieldBuilder newFieldBuilder(String name) {
return new FieldBuilder110(name);
}

@Override
public String getFieldPropAsJsonString(Schema.Field field, String propName) {
Object val = field.getObjectProp(propName);
if (val == null) {
return null;
}
//the following is VERY rube-goldberg-ish, but will do until someone complains
JsonNode asJsonNode = JacksonUtils.toJsonNode(val);
try {
return OBJECT_MAPPER.writeValueAsString(asJsonNode);
} catch (Exception issue) {
throw new IllegalStateException("while trying to serialize " + val + " (a " + val.getClass().getName() + ")", issue);
}
}

@Override
public Collection<AvroGeneratedSourceCode> compile(
Collection<Schema> toCompile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field.Order;

import java.util.Map;


public class FieldBuilder110 implements FieldBuilder {
private final String _name;
private Schema.Field _field;
private Schema _schema;
private String _doc;
private Object _defaultVal;
private Order _order;
private Map<String, Object> _props;

public FieldBuilder110(Schema.Field field) {
this(field.name());
_field = field;
_schema = field.schema();
_doc = field.doc();
_defaultVal = field.defaultVal();
_order = field.order();
_props = field.getObjectProps();
}

public FieldBuilder110(String name) {
Expand Down Expand Up @@ -53,18 +59,19 @@ public FieldBuilder setOrder(Order order) {
}

@Override
@Deprecated
public FieldBuilder copyFromField() {
if (_field == null) {
throw new NullPointerException("Field in FieldBuilder can not be empty!");
}
_doc = _field.doc();
_defaultVal = _field.defaultVal();
_order = _field.order();
return this;
}

@Override
public Schema.Field build() {
return new Schema.Field(_name, _schema, _doc, _defaultVal, _order);
Schema.Field result = new Schema.Field(_name, _schema, _doc, _defaultVal, _order);
if (_props != null) {
for (Map.Entry<String, Object> entry : _props.entrySet()) {
result.addProp(entry.getKey(), entry.getValue());
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ public FieldBuilder newFieldBuilder(String name) {
return new FieldBuilder14(name);
}

@Override
public String getFieldPropAsJsonString(Schema.Field field, String propName) {
String val = field.getProp(propName);
return val == null ? null : "\"" + val + "\"";
}

@Override
public Collection<AvroGeneratedSourceCode> compile(
Collection<Schema> toCompile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,37 @@
import org.apache.avro.Schema.Field.Order;
import org.codehaus.jackson.JsonNode;

import java.lang.reflect.Field;
import java.util.Map;


public class FieldBuilder14 implements FieldBuilder {
private final static Field SCHEMA_FIELD_PROPS_FIELD;

static {
try {
Class<Schema.Field> fieldClass = Schema.Field.class;
SCHEMA_FIELD_PROPS_FIELD = fieldClass.getDeclaredField("props");
SCHEMA_FIELD_PROPS_FIELD.setAccessible(true); //muwahahaha
} catch (Throwable issue) {
throw new IllegalStateException("unable to find/access Schema$Field.props", issue);
}
}

private final String _name;
private Schema.Field _field;
private Schema _schema;
private String _doc;
private JsonNode _defaultVal;
private Order _order;
private Map<String,String> _props;

public FieldBuilder14(Schema.Field field) {
this(field.name());
_field = field;
_schema = field.schema();
_doc = field.doc();
_defaultVal = field.defaultValue();
_order = field.order();
_props = getProps(field);
}

public FieldBuilder14(String name) {
Expand Down Expand Up @@ -59,18 +78,28 @@ public FieldBuilder setOrder(Order order) {
}

@Override
@Deprecated
public FieldBuilder copyFromField() {
if (_field == null) {
throw new NullPointerException("Field in FieldBuilder can not be empty!");
}
_doc = _field.doc();
_defaultVal = _field.defaultValue();
_order = _field.order();
return this;
}

@Override
public Schema.Field build() {
return new Schema.Field(_name, _schema, _doc, _defaultVal, _order);
Schema.Field result = new Schema.Field(_name, _schema, _doc, _defaultVal, _order);
if (_props != null && !_props.isEmpty()) {
Map<String, String> clonedProps = getProps(result);
clonedProps.putAll(_props);
}
return result;
}

private Map<String,String> getProps(Schema.Field field) {
try {
@SuppressWarnings("unchecked")
Map<String, String> props = (Map<String, String>) SCHEMA_FIELD_PROPS_FIELD.get(field);
return props;
} catch (Exception e) {
throw new IllegalStateException("unable to access props on Schema$Field " + field.name(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ public FieldBuilder newFieldBuilder(String name) {
return new FieldBuilder15(name);
}

@Override
public String getFieldPropAsJsonString(Schema.Field field, String propName) {
String val = field.getProp(propName);
return val == null ? null : "\"" + val + "\"";
}

@Override
public Collection<AvroGeneratedSourceCode> compile(
Collection<Schema> toCompile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,37 @@
import org.apache.avro.Schema.Field.Order;
import org.codehaus.jackson.JsonNode;

import java.lang.reflect.Field;
import java.util.Map;


public class FieldBuilder15 implements FieldBuilder {
private final static Field SCHEMA_FIELD_PROPS_FIELD;

static {
try {
Class<Schema.Field> fieldClass = Schema.Field.class;
SCHEMA_FIELD_PROPS_FIELD = fieldClass.getDeclaredField("props");
SCHEMA_FIELD_PROPS_FIELD.setAccessible(true); //muwahahaha
} catch (Throwable issue) {
throw new IllegalStateException("unable to find/access Schema$Field.props", issue);
}
}

private final String _name;
private Schema.Field _field;
private Schema _schema;
private String _doc;
private JsonNode _defaultVal;
private Order _order;
private Map<String,String> _props;

public FieldBuilder15(Schema.Field field) {
this(field.name());
_field = field;
_schema = field.schema();
_doc = field.doc();
_defaultVal = field.defaultValue();
_order = field.order();
_props = getProps(field);
}

public FieldBuilder15(String name) {
Expand Down Expand Up @@ -59,18 +78,28 @@ public FieldBuilder setOrder(Order order) {
}

@Override
@Deprecated
public FieldBuilder copyFromField() {
if (_field == null) {
throw new NullPointerException("Field in FieldBuilder can not be empty!");
}
_doc = _field.doc();
_defaultVal = _field.defaultValue();
_order = _field.order();
return this;
}

@Override
public Schema.Field build() {
return new Schema.Field(_name, _schema, _doc, _defaultVal, _order);
Schema.Field result = new Schema.Field(_name, _schema, _doc, _defaultVal, _order);
if (_props != null && !_props.isEmpty()) {
Map<String, String> clonedProps = getProps(result);
clonedProps.putAll(_props);
}
return result;
}

private Map<String,String> getProps(Schema.Field field) {
try {
@SuppressWarnings("unchecked")
Map<String, String> props = (Map<String, String>) SCHEMA_FIELD_PROPS_FIELD.get(field);
return props;
} catch (Exception e) {
throw new IllegalStateException("unable to access props on Schema$Field " + field.name(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ public FieldBuilder newFieldBuilder(String name) {
return new FieldBuilder16(name);
}

@Override
public String getFieldPropAsJsonString(Schema.Field field, String propName) {
String val = field.getProp(propName);
return val == null ? null : "\"" + val + "\"";
}

@Override
public Collection<AvroGeneratedSourceCode> compile(
Collection<Schema> toCompile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@
import org.apache.avro.Schema.Field.Order;
import org.codehaus.jackson.JsonNode;

import java.util.Map;


public class FieldBuilder16 implements FieldBuilder {
private final String _name;
private Schema.Field _field;
private Schema _schema;
private String _doc;
private JsonNode _defaultVal;
private Order _order;
private Map<String,String> _props;

public FieldBuilder16(Schema.Field field) {
this(field.name());
_field = field;
_schema = field.schema();
_doc = field.doc();
_defaultVal = field.defaultValue();
_order = field.order();
_props = field.props();
}

public FieldBuilder16(String name) {
Expand Down Expand Up @@ -59,18 +65,19 @@ public FieldBuilder setOrder(Order order) {
}

@Override
@Deprecated
public FieldBuilder copyFromField() {
if (_field == null) {
throw new NullPointerException("Field in FieldBuilder can not be empty!");
}
_doc = _field.doc();
_defaultVal = _field.defaultValue();
_order = _field.order();
return this;
}

@Override
public Schema.Field build() {
return new Schema.Field(_name, _schema, _doc, _defaultVal, _order);
Schema.Field result = new Schema.Field(_name, _schema, _doc, _defaultVal, _order);
if (_props != null) {
for (Map.Entry<String, String> entry : _props.entrySet()) {
result.addProp(entry.getKey(), entry.getValue());
}
}
return result;
}
}
Loading

0 comments on commit d99099d

Please sign in to comment.