-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-1340: Added Enum Defaults and unit tests. #298
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
Conversation
cutting
left a comment
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.
This looks great. I like the simple, minimal approach taken. I've identified a few improvements, namely builder and IDL integration. These could be done as followup issues, but it might be better to get them in from the start. Thanks!
| && symbols.equals(that.symbols) | ||
| && props.equals(that.props); | ||
| } | ||
| public String getEnumDefault() { return enumDefault; } |
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.
needs @OverRide?
| if (aliases.containsKey(name)) | ||
| result = Schema.createEnum(aliases.get(name).full, s.getDoc(), null, | ||
| s.getEnumSymbols()); | ||
| s.getEnumSymbols(), s.getEnumDefault()); |
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 scanned for other places where Schema.createEnum is called to see which need updates.
ResolvingVisitor.java also needs this same change. This is used to support IDL. We should also add support to IDL for enum defaults.
SchemaBuilder.java should be changed to permit specifying an enum default.
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.
Okay yes, I can add that to this PR.
… at the idl.jj for enum default but tests not passing for simple.avdl
e2ff000 to
739cce8
Compare
|
Here's a fix for the IDL problem. The grammar change seems to cause the parser to lookahead farther, so the last documentation comment that's been seen is too far along. I changed it to grab the doc string once it's seen the enum keyword and that seems to fix it. Patch otherwise looks good. We should also test the IDL change. Maybe change simple.avdl to specify an enum default, then update simple.avpr to contain the new output? (You can use 'mvn package -dskipTests' to create a tools.jar so 'java -jar tools.jar idl ...' can be used to create the new compiler test output file.) |
…ing. Added test case to simple.avdl and simple.avpr
|
@cutting - Thanks Doug. I applied the patch, added a test and built the tooling to validate that it works. Is there anything else you forsee being required? Also, do we need additional eyes on this? I recall that someone mentioned in the JIRA thread about the "@default" annotation - I don't have any idea how it would interact with this. |
|
We're almost there. I just looked at the builder integration, and think it would be better if we instead added defaultSymbol(String) method to EnumBuilder. We also need javadoc for this new method, and should update the EnumBuilder example in the SchemaBuilder class javadoc to specify a default symbol. Also, we need to update doc/src/content/xdocs/idl.xml to describe enum defaults. This can be in the "Defining and Enumeration" section. We can update the example to include a default. Additional eyes are not required. Folks have the opportunity to follow here & object. I'll commit this once we're happy with it. We could add an @default annotation now, or commit this now and add that later, I don't have a strong feeling. |
I think there's some misunderstanding here - If I take this a protocol file named And then run In my JIRA post, I was attempting to provide reasonable answers to the question: "how should we handle cases when both (Currently on this branch, if I use both I get JSON like: ) |
…hat the @default annotation no longer works on enum avdl definitions. Updated the way that SchemaBuilder allows an enum to be built sith a default by separating out the default setter.
…adable and well-formatted.
| Schema expected = Schema.createEnum("myenum", null, null, symbols, enumDefault); | ||
| expected.addProp("p", "v"); | ||
| Schema schema = SchemaBuilder.enumeration("myenum") | ||
| .prop("p", "v").defaultSymbol(enumDefault).symbols("a", "b"); |
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.
So this particular code decision means that the default must be set before calling .symbols(). It will not work the other way around.
| Set reserved = SCHEMA_RESERVED; | ||
| if (type.equals("enum")) { | ||
| reserved = ENUM_RESERVED; | ||
| } |
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 intention here was to not cause any unintended side effects.
|
One more quick statement - I noticed that there is next to no testing around the usage of the "@default" notation. I know that my changes work only because I cannot use the "@default" notation on my enums anymore. However, I do not have any tests to prove that. Should this also be part of this JIRA? |
| Iterator<String> i = schema.getFieldNames(); | ||
|
|
||
| Set reserved = SCHEMA_RESERVED; | ||
| if (type.equals("enum")) { |
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 test should instead be 'type == ENUM', no?
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 just kept it consistent with the other references to it in this file. See line 1274 and 1330. "type" is actually a string here, believe it or not.
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.
Sorry, I somehow tagged the wrong line. I meant line 105, not this one.
doc/src/content/xdocs/idl.xml
Outdated
| <source> | ||
| enum Shapes { | ||
| SQUARE, TRIANGLE, CIRCLE | ||
| } = CIRCLE |
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.
This needs a semicolon. I don't know if that's the best syntax, but it is what we implemented!
|
As far as @default, I think it's probably a bug that this was not a reserved word in IDL from the start. We could fix that, but it might break folks who've relied on it (although you could not have used it to set field defaults previously, only to provide a property named "default" on other kinds of schemas). So only breaking folks who used it on enums is probably best, since we're changing the semantics of that case anyway. Does that seem reasonable? |
|
My preference is to impose a minimal change centered around the functionality. In this case I would prefer to leave the @default behaviour as it currently is except for changing it for enum. That being said, I would be happy to file a bug ticket for it as a separate fix - that way if there is a bunch of issues with it that it would be addressable on its own and not affect this commit. |
|
It sounds like we agree about @default. With the current patch its behavior is only changed for enums, and that this is appropriate. |
cutting
left a comment
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.
Once the semicolon is added to the example in the documentation, I'm ready to commit this.
doc/src/content/xdocs/idl.xml
Outdated
| <source> | ||
| enum Shapes { | ||
| SQUARE, TRIANGLE, CIRCLE | ||
| } = CIRCLE |
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.
need a semicolon at EOL
|
@cutting Thanks for your help Doug, I really appreciate all the feedback and assistance. |
To see an example table of this behaviour, see Final Default & Fallback tab in:
https://docs.google.com/spreadsheets/d/1YkTBnHCBPGz2t0gQI3OioKS_xP9h9lq_a-gS2jSGFAc/edit?usp=sharing