-
Notifications
You must be signed in to change notification settings - Fork 0
3445: AVRO-3520: [java] expose read schema in custom encoding #19
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,20 +364,12 @@ protected ClassAccessorData computeValue(Class<?> c) { | |
|
|
||
| static class ClassAccessorData { | ||
| private final Class<?> clazz; | ||
| private final Map<String, FieldAccessor> byName = new HashMap<>(); | ||
| // getAccessorsFor replaces this map with each modification | ||
| private final Map<String, FieldAccessor> byName; | ||
| volatile Map<Schema, FieldAccessor[]> bySchema = new WeakHashMap<>(); | ||
|
|
||
| private ClassAccessorData(Class<?> c) { | ||
| clazz = c; | ||
| for (Field f : getFields(c, false)) { | ||
| if (f.isAnnotationPresent(AvroIgnore.class)) { | ||
| continue; | ||
| } | ||
| FieldAccessor accessor = ReflectionUtil.getFieldAccess().getAccessor(f); | ||
| AvroName avroname = f.getAnnotation(AvroName.class); | ||
| byName.put((avroname != null ? avroname.value() : f.getName()), accessor); | ||
| } | ||
| byName = buildByName(c, null); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -397,10 +389,12 @@ private FieldAccessor[] getAccessorsFor(Schema schema) { | |
| } | ||
|
|
||
| private FieldAccessor[] createAccessorsFor(Schema schema) { | ||
|
|
||
| var byNameSchema = buildByName(clazz, schema); | ||
| List<Schema.Field> avroFields = schema.getFields(); | ||
| FieldAccessor[] result = new FieldAccessor[avroFields.size()]; | ||
| for (Schema.Field avroField : schema.getFields()) { | ||
| result[avroField.pos()] = byName.get(avroField.name()); | ||
| result[avroField.pos()] = byNameSchema.get(avroField.name()); | ||
| } | ||
| return result; | ||
| } | ||
|
|
@@ -412,6 +406,25 @@ private FieldAccessor getAccessorFor(String fieldName) { | |
| } | ||
| return result; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Schema-less accessors break custom encoding evolutionThe |
||
| private static Map<String, FieldAccessor> buildByName(Class<?> c, Schema schema) { | ||
| Map<String, FieldAccessor> byName = new HashMap<>(); | ||
| for (Field f : getFields(c, false)) { | ||
| if (f.isAnnotationPresent(AvroIgnore.class)) { | ||
| continue; | ||
| } | ||
| AvroName avroname = f.getAnnotation(AvroName.class); | ||
| var name = (avroname != null ? avroname.value() : f.getName()); | ||
| Schema fieldSchema = null; | ||
| if (schema != null) { | ||
| var field = schema.getField(name); | ||
| fieldSchema = field != null ? field.schema() : null; | ||
| } | ||
| FieldAccessor accessor = ReflectionUtil.getFieldAccess().getAccessor(f, fieldSchema); | ||
| byName.put(name, accessor); | ||
| } | ||
| return byName; | ||
| } | ||
| } | ||
|
|
||
| private ClassAccessorData getClassAccessorData(Class<?> c) { | ||
|
|
@@ -1055,7 +1068,8 @@ private CustomEncodingWrapper populateEncoderCache(Schema schema) { | |
| var enc = ReflectionUtil.getAvroEncode(getClass(schema)); | ||
| if (enc != null) { | ||
| try { | ||
| return new CustomEncodingWrapper(enc.using().getDeclaredConstructor().newInstance()); | ||
| var customEncodingClass = enc.using().getDeclaredConstructor().newInstance(); | ||
| return new CustomEncodingWrapper(customEncodingClass.withSchema(schema)); | ||
| } catch (Exception e) { | ||
| throw new AvroRuntimeException("Could not instantiate custom Encoding"); | ||
| } | ||
|
|
||
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.
The Javadoc says "schema detected during read", but
withSchemais also called on write paths (e.g.,ReflectData.populateEncoderCacheand in field access for writing), so this phrasing can be misleading. Consider generalizing to "active schema (read or write)" to reflect actual usage.🤖 Was this useful? React with 👍 or 👎
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.
value:useful; category:documentation; feedback: The Augment AI reviewer is correct that the javadoc should be updated to not mention
readorwriteschema. The method is used with both.