-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Teach ObjectParser a happy pattern #50691
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
We *very* commonly have object with ctors like: ``` public Foo(String name) ``` And then declare a bunch of setters on the object. Every aggregation works like this, for example. This change teaches `ObjectParser` how to build these aggregations all on its own, without any help. This'll make it much cleaner to parse aggs, and, probably, a bunch of other things. It'll let us remove lots of wrapping. I've used this new power for the `avg` aggregation just to prove that it works outside of a unit test.
|
Pinging @elastic/es-core-infra (:Core/Infra/Core) |
|
|
||
| public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException { | ||
| return PARSER.parse(parser, new AvgAggregationBuilder(aggregationName), null); | ||
| return PARSER.parse(parser, aggregationName); |
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.
From here the payoff doesn't look that good, but we can remove the parses method entirely in a follow-up and call the parser directly. We can't do it right now because the order of the arguments is backwards.
| private static final ParseField SCORE_MODE_FIELD = new ParseField("score_mode"); | ||
|
|
||
| private static final ObjectParser<InnerBuilder, Void> QUERY_RESCORE_PARSER = new ObjectParser<>(NAME, null); | ||
| private static final ObjectParser<InnerBuilder, Void> QUERY_RESCORE_PARSER = new ObjectParser<>(NAME); |
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.
All of these are because it couldn't compile now that there are two methods that take two args. It is better not to have the null anyway.
| assertThat(ex.getMessage(), containsString("[foo] failed to parse field [int_array]")); | ||
| } | ||
|
|
||
| private static final Supplier<AtomicReference<String>> NEW_EMPTY_STRING_REF = AtomicReference::new; |
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 is because there are two ctors on atomic reference. One that looks like a function and one that looks like a supplier. The compiler was confused when I passed ::new to the ctor.
romseygeek
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.
LGTM, this will make a whole bunch of stuff much cleaner.
|
Oh neat! These compile failures don't show up in Eclipse. |
OK! I have a solution. A static method instead of a ctor. Testing locally. This is really a place where the builder pattern could be nice, but we've historically not liked it so I'm not using it. |
We *very* commonly have object with ctors like: ``` public Foo(String name) ``` And then declare a bunch of setters on the object. Every aggregation works like this, for example. This change teaches `ObjectParser` how to build these aggregations all on its own, without any help. This'll make it much cleaner to parse aggs, and, probably, a bunch of other things. It'll let us remove lots of wrapping. I've used this new power for the `avg` aggregation just to prove that it works outside of a unit test.
We *very* commonly have object with ctors like: ``` public Foo(String name) ``` And then declare a bunch of setters on the object. Every aggregation works like this, for example. This change teaches `ObjectParser` how to build these aggregations all on its own, without any help. This'll make it much cleaner to parse aggs, and, probably, a bunch of other things. It'll let us remove lots of wrapping. I've used this new power for the `avg` aggregation just to prove that it works outside of a unit test.
We *very* commonly have object with ctors like: ``` public Foo(String name) ``` And then declare a bunch of setters on the object. Every aggregation works like this, for example. This change teaches `ObjectParser` how to build these aggregations all on its own, without any help. This'll make it much cleaner to parse aggs, and, probably, a bunch of other things. It'll let us remove lots of wrapping. I've used this new power for the `avg` aggregation just to prove that it works outside of a unit test.
We very commonly have object with ctors like:
And then declare a bunch of setters on the object. Every aggregation
works like this, for example. This change teaches
ObjectParserhow tobuild these aggregations all on its own, without any help. This'll make
it much cleaner to parse aggs, and, probably, a bunch of other things.
It'll let us remove lots of wrapping. I've used this new power for the
avgaggregation just to prove that it works outside of a unit test.