You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Fernando Moraes (JIRA)" <ji...@apache.org> on 2017/10/26 11:17:00 UTC
[jira] [Commented] (AVRO-2100) Nullable fields based on own
annotations
[ https://issues.apache.org/jira/browse/AVRO-2100?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16220331#comment-16220331 ]
Fernando Moraes commented on AVRO-2100:
---------------------------------------
Hello, one way that I was able to solve the situation was by doing the following:
{code:java}
public class Entity {
@NotNull
private String name;
@NotNull
@Digits(integer = 6, fraction = 2)
private BigDecimal salary;
}
public final class ReflectDataFactory {
private final static Schema.Parser PARSER = new Schema.Parser();
private final static String BIG_DECIMAL_SCHEMA_TEMPLATE
= "{\n"
+ " \"type\": \"bytes\",\n"
+ " \"logicalType\": \"decimal\",\n"
+ " \"precision\": %d,\n"
+ " \"scale\": %d\n"
+ "}";
private static final ReflectData INSTANCE = new ReflectData() {
@Override
protected Schema createFieldSchema(Field field, Map<String, Schema> names) {
final Class<?> c = field.getType();
final Schema schema
= c.isAssignableFrom(BigDecimal.class)
? getSchemaBigDecimal(field)
: super.createFieldSchema(field, names);
return (field.isAnnotationPresent(NotNull.class) && !field.isAnnotationPresent(Nullable.class))
? makeNullable(schema) : schema;
}
@Override
protected Schema createSchema(Type type, Map<String, Schema> names) {
return super.createSchema(type, names);
}
};
private static Schema getSchemaBigDecimal(final Field field) {
int precision = 2;
int scale = 2;
if (field.isAnnotationPresent(Digits.class)) {
final Digits digits = field.getAnnotation(Digits.class);
precision = digits.integer();
scale = digits.fraction();
}
return PARSER.parse(String.format(BIG_DECIMAL_SCHEMA_TEMPLATE, precision, scale));
}
public static ReflectData getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Schema schema = ReflectDataFactory.getInstance().getSchema(Entity.class);
System.out.println(schema.toString(true));
}
}
{code}
The last line, will result in:
{noformat}
{
"type" : "record",
"name" : "Entity",
"namespace" : "reflect",
"fields" : [ {
"name" : "name",
"type" : [ "null", "string" ],
"default" : null
}, {
"name" : "salary",
"type" : [ "null", {
"type" : "bytes",
"logicalType" : "decimal",
"precision" : 6,
"scale" : 2
} ],
"default" : null
} ]
}
{noformat}
> Nullable fields based on own annotations
> ----------------------------------------
>
> Key: AVRO-2100
> URL: https://issues.apache.org/jira/browse/AVRO-2100
> Project: Avro
> Issue Type: Wish
> Components: java
> Affects Versions: 1.8.2
> Reporter: Fernando Moraes
> Priority: Trivial
>
> Hello! first of all, congratulations on the project.
> I am generating avro schemas from java class, and my class already have annotations referring to not null. I would like to know if it is possible to somehow override the default behavior to define the nullable in the schema.
> My code looks like this:
> {code:java}
> public class MyJavaClass {
>
> @javax.validation.constraints.NotNull // This is necessary for my project.
> @org.apache.avro.reflect.Nullable // I wanted to avoid having to use this annotation
> private String field;
> }
> Schema schema = ReflectData.get().getSchema(MyJavaClass.class);
> {code}
> thank you all
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)