[ 
https://issues.apache.org/jira/browse/AVRO-2115?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16287726#comment-16287726
 ] 

Miguel edited comment on AVRO-2115 at 12/12/17 3:29 PM:
--------------------------------------------------------

I think that changes are to be implemented into the ReflectData class. 

In a previous implementation I did a versioned ReflectData that defined those 
unions from a configuration input map. I think it could help:

{code:java}

public class PatchedReflectData extends ReflectData {
                
        private final Map<Type, List<Type>> map;
        
        public PatchedReflectData(Map<Type, List<Type>> map) {
                this.map = map;
        }
        
        @Override
        protected Schema createSchema(final Type type, final Map<String, 
Schema> names) {
                
                List<Type> list = map.get(type);
                if (list != null) {
                        List<Schema> branches = new ArrayList<>();

                    for (Type branch : list) {
                        final Schema schema;
                        if (branch == null) {
                                schema = Schema.create(Schema.Type.NULL);
                        } else {
                                schema = createSchema(branch, names);
                        }
                        branches.add(schema);
                        
                    }
                                    
                        return Schema.createUnion(branches);
                }
                
                return super.createSchema(type, names);
        }
}

{code}

I am a volunteer to implement it if you need help.


was (Author: elmendavies):
I think that changes are to be implemented into the ReflectData class. 

In a previous implementation I did a versioned ReflectData that defined those 
unions from a configuration input map. I think it could help:

{code:java}

public class PatchedReflectData extends ReflectData {
                
        private final Map<Type, List<Type>> map;
        
        public PatchedReflectData(Map<Type, List<Type>> map) {
                this.map = map;
        }
        
        @Override
        protected Schema createSchema(final Type type, final Map<String, 
Schema> names) {
                
                List<Type> list = map.get(type);
                if (list != null) {
                        List<Schema> branches = new ArrayList<>();

                    for (Type branch : list) {
                        final Schema schema;
                        if (branch == null) {
                                schema = Schema.create(Schema.Type.NULL);
                        } else {
                                schema = createSchema(branch, names);
                        }
                        branches.add(schema);
                        
                    }
                                    
                        return Schema.createUnion(branches);
                }
                
                return super.createSchema(type, names);
        }
}

{code}

I am a volunteer :-) 

> Support @Union in members too
> -----------------------------
>
>                 Key: AVRO-2115
>                 URL: https://issues.apache.org/jira/browse/AVRO-2115
>             Project: Avro
>          Issue Type: Improvement
>            Reporter: Miguel
>            Priority: Minor
>
> Allow defining unions in members:
> {code:java}
> class MyDatum {
>    @Nullable
>    @Union({String.class, Long.class, Other.class,...})
>    Object o;
> }
> {code}
> It can currently be implemented using @AvroSchema like this, but it is seems 
> in my oppinion more verbose and more difficult to maintain:
> {code:java}
> @AvroSchema("[\"null\",\"string\",...")
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to