-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-4165: [java] ability to specify AvroEncode on a class #3425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AVRO-4165: [java] ability to specify AvroEncode on a class #3425
Conversation
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectionUtil.java
Outdated
Show resolved
Hide resolved
| private CustomEncodingWrapper populateEncoderCache(Schema schema) { | ||
| var enc = ReflectionUtil.getAvroEncode(getClass(schema)); | ||
| if (enc != null) { | ||
| try { | ||
| return new CustomEncodingWrapper(enc.using().getDeclaredConstructor().newInstance()); | ||
| } catch (Exception e) { | ||
| throw new AvroRuntimeException("Could not instantiate custom Encoding"); | ||
| } | ||
| } | ||
| return new CustomEncodingWrapper(null); | ||
| } | ||
|
|
||
| private class CustomEncodingWrapper { | ||
|
|
||
| private final CustomEncoding customEncoding; | ||
|
|
||
| private CustomEncodingWrapper(CustomEncoding customEncoding) { | ||
| this.customEncoding = customEncoding; | ||
| } | ||
|
|
||
| public CustomEncoding get() { | ||
| return customEncoding; | ||
| } | ||
|
|
||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this implementation! As you already mentioned in the PR, it's also suited for one of the superclasses. However, as the current use case for CustomEncoding is only from @AvroEncode, I think it's good as it is now.
|
@opwvhk thanks for the review. Have addressed the feedback. Question on process: Do you like conversations to be left for you to resolve? |
This PR adds the ability to use the
CustomEncodeannotation directly on a class.It required some changes to when this annotation would be scanned. Currently, it is only used on fields.
Getting this to work for the first Record written required additional changes to support the feature.
I added a new map on
ReflectDataas I feel this is cleaner from an encapsulation point of view. Alternatively, it could be added toSpecifiedDataby changing theclassCachemap to an object that stores the class and thisCustomEncoding. This may be slightly faster, as there would be one less map lookup. However, it would probably be negligible as this new lookup is only performed once per row, whereas other maps can be queried per field.This change added tests and can be verified as follows:
Documentation