Skip to content

Commit a18cb67

Browse files
Add support for plugin-defined custom components in union types (#370) (#372)
Co-authored-by: Sylvain Wallez <[email protected]>
1 parent 6f8f094 commit a18cb67

File tree

124 files changed

+1096
-347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+1096
-347
lines changed

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/SortOptions.java

-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@
5656
@JsonpDeserializable
5757
public class SortOptions implements TaggedUnion<SortOptions.Kind, Object>, JsonpSerializable {
5858

59-
/**
60-
* {@link SortOptions} variant kinds.
61-
*/
6259
/**
6360
* {@link SortOptions} variant kinds.
6461
*

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/Transform.java

-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353
@JsonpDeserializable
5454
public class Transform implements TaggedUnion<Transform.Kind, Object>, JsonpSerializable {
5555

56-
/**
57-
* {@link Transform} variant kinds.
58-
*/
5956
/**
6057
* {@link Transform} variant kinds.
6158
*

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/aggregations/Aggregate.java

+70-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package co.elastic.clients.elasticsearch._types.aggregations;
2525

2626
import co.elastic.clients.json.ExternallyTaggedUnion;
27+
import co.elastic.clients.json.JsonData;
2728
import co.elastic.clients.json.JsonEnum;
2829
import co.elastic.clients.json.JsonpDeserializable;
2930
import co.elastic.clients.json.JsonpDeserializer;
@@ -34,9 +35,10 @@
3435
import co.elastic.clients.util.ApiTypeHelper;
3536
import co.elastic.clients.util.ObjectBuilder;
3637
import co.elastic.clients.util.ObjectBuilderBase;
37-
import co.elastic.clients.util.TaggedUnion;
38+
import co.elastic.clients.util.OpenTaggedUnion;
3839
import co.elastic.clients.util.TaggedUnionUtils;
3940
import jakarta.json.stream.JsonGenerator;
41+
import java.lang.Object;
4042
import java.util.HashMap;
4143
import java.util.Map;
4244
import java.util.Objects;
@@ -52,11 +54,8 @@
5254
* specification</a>
5355
*/
5456

55-
public class Aggregate implements TaggedUnion<Aggregate.Kind, AggregateVariant>, JsonpSerializable {
57+
public class Aggregate implements OpenTaggedUnion<Aggregate.Kind, Object>, JsonpSerializable {
5658

57-
/**
58-
* {@link Aggregate} variant kinds.
59-
*/
6059
/**
6160
* {@link Aggregate} variant kinds.
6261
*
@@ -198,6 +197,9 @@ public enum Kind implements JsonEnum {
198197

199198
WeightedAvg("weighted_avg"),
200199

200+
/** A custom {@code Aggregate} defined by a plugin */
201+
_Custom(null)
202+
201203
;
202204

203205
private final String jsonValue;
@@ -213,36 +215,48 @@ public String jsonValue() {
213215
}
214216

215217
private final Kind _kind;
216-
private final AggregateVariant _value;
218+
private final Object _value;
217219

218220
@Override
219221
public final Kind _kind() {
220222
return _kind;
221223
}
222224

223225
@Override
224-
public final AggregateVariant _get() {
226+
public final Object _get() {
225227
return _value;
226228
}
227229

228230
public Aggregate(AggregateVariant value) {
229231

230232
this._kind = ApiTypeHelper.requireNonNull(value._aggregateKind(), this, "<variant kind>");
231233
this._value = ApiTypeHelper.requireNonNull(value, this, "<variant value>");
234+
this._customKind = null;
232235

233236
}
234237

235238
private Aggregate(Builder builder) {
236239

237240
this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, "<variant kind>");
238241
this._value = ApiTypeHelper.requireNonNull(builder._value, builder, "<variant value>");
242+
this._customKind = builder._customKind;
239243

240244
}
241245

242246
public static Aggregate of(Function<Builder, ObjectBuilder<Aggregate>> fn) {
243247
return fn.apply(new Builder()).build();
244248
}
245249

250+
/**
251+
* Build a custom plugin-defined {@code Aggregate}, given its kind and some JSON
252+
* data
253+
*/
254+
public Aggregate(String kind, JsonData value) {
255+
this._kind = Kind._Custom;
256+
this._value = value;
257+
this._customKind = kind;
258+
}
259+
246260
/**
247261
* Is this variant instance of kind {@code adjacency_matrix}?
248262
*/
@@ -1379,6 +1393,35 @@ public WeightedAvgAggregate weightedAvg() {
13791393
return TaggedUnionUtils.get(this, Kind.WeightedAvg);
13801394
}
13811395

1396+
@Nullable
1397+
private final String _customKind;
1398+
1399+
/**
1400+
* Is this a custom {@code Aggregate} defined by a plugin?
1401+
*/
1402+
public boolean _isCustom() {
1403+
return _kind == Kind._Custom;
1404+
}
1405+
1406+
/**
1407+
* Get the actual kind when {@code _kind()} equals {@link Kind#_Custom}
1408+
* (plugin-defined variant).
1409+
*/
1410+
@Nullable
1411+
public final String _customKind() {
1412+
return _customKind;
1413+
}
1414+
1415+
/**
1416+
* Get the custom plugin-defined variant value.
1417+
*
1418+
* @throws IllegalStateException
1419+
* if the current variant is not {@link Kind#_Custom}.
1420+
*/
1421+
public JsonData _custom() {
1422+
return TaggedUnionUtils.get(this, Kind._Custom);
1423+
}
1424+
13821425
@Override
13831426
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
13841427

@@ -1393,7 +1436,8 @@ public String toString() {
13931436

13941437
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<Aggregate> {
13951438
private Kind _kind;
1396-
private AggregateVariant _value;
1439+
private Object _value;
1440+
private String _customKind;
13971441

13981442
public ObjectBuilder<Aggregate> adjacencyMatrix(AdjacencyMatrixAggregate v) {
13991443
this._kind = Kind.AdjacencyMatrix;
@@ -2109,6 +2153,22 @@ public ObjectBuilder<Aggregate> weightedAvg(
21092153
return this.weightedAvg(fn.apply(new WeightedAvgAggregate.Builder()).build());
21102154
}
21112155

2156+
/**
2157+
* Define this {@code Aggregate} as a plugin-defined variant.
2158+
*
2159+
* @param name
2160+
* the plugin-defined identifier
2161+
* @param data
2162+
* the data for this custom {@code Aggregate}. It is converted
2163+
* internally to {@link JsonData}.
2164+
*/
2165+
public ObjectBuilder<Aggregate> _custom(String name, Object data) {
2166+
this._kind = Kind._Custom;
2167+
this._customKind = name;
2168+
this._value = JsonData.of(data);
2169+
return this;
2170+
}
2171+
21122172
public Aggregate build() {
21132173
_checkSingleUse();
21142174
return new Aggregate(this);
@@ -2187,7 +2247,7 @@ public Aggregate build() {
21872247
deserializers.put("variable_width_histogram", VariableWidthHistogramAggregate._DESERIALIZER);
21882248
deserializers.put("weighted_avg", WeightedAvgAggregate._DESERIALIZER);
21892249

2190-
_TYPED_KEYS_DESERIALIZER = new ExternallyTaggedUnion.Deserializer<>(deserializers,
2191-
(name, value) -> new Aggregate(value)).typedKeys();
2250+
_TYPED_KEYS_DESERIALIZER = new ExternallyTaggedUnion.Deserializer<>(deserializers, Aggregate::new,
2251+
Aggregate::new).typedKeys();
21922252
}
21932253
}

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/aggregations/Aggregation.java

+59-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import co.elastic.clients.json.ObjectDeserializer;
3636
import co.elastic.clients.util.ApiTypeHelper;
3737
import co.elastic.clients.util.ObjectBuilder;
38-
import co.elastic.clients.util.TaggedUnion;
38+
import co.elastic.clients.util.OpenTaggedUnion;
3939
import co.elastic.clients.util.TaggedUnionUtils;
4040
import co.elastic.clients.util.WithJsonObjectBuilderBase;
4141
import jakarta.json.stream.JsonGenerator;
@@ -55,11 +55,8 @@
5555
* specification</a>
5656
*/
5757
@JsonpDeserializable
58-
public class Aggregation implements TaggedUnion<Aggregation.Kind, Object>, JsonpSerializable {
58+
public class Aggregation implements OpenTaggedUnion<Aggregation.Kind, Object>, JsonpSerializable {
5959

60-
/**
61-
* {@link Aggregation} variant kinds.
62-
*/
6360
/**
6461
* {@link Aggregation} variant kinds.
6562
*
@@ -215,6 +212,9 @@ public enum Kind implements JsonEnum {
215212

216213
VariableWidthHistogram("variable_width_histogram"),
217214

215+
/** A custom {@code Aggregation} defined by a plugin */
216+
_Custom(null)
217+
218218
;
219219

220220
private final String jsonValue;
@@ -250,6 +250,7 @@ public Aggregation(AggregationVariant value) {
250250

251251
this._kind = ApiTypeHelper.requireNonNull(value._aggregationKind(), this, "<variant kind>");
252252
this._value = ApiTypeHelper.requireNonNull(value, this, "<variant value>");
253+
this._customKind = null;
253254

254255
this.aggregations = null;
255256
this.meta = null;
@@ -260,6 +261,7 @@ private Aggregation(Builder builder) {
260261

261262
this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, "<variant kind>");
262263
this._value = ApiTypeHelper.requireNonNull(builder._value, builder, "<variant value>");
264+
this._customKind = builder._customKind;
263265

264266
this.aggregations = ApiTypeHelper.unmodifiable(builder.aggregations);
265267
this.meta = ApiTypeHelper.unmodifiable(builder.meta);
@@ -1544,6 +1546,35 @@ public VariableWidthHistogramAggregation variableWidthHistogram() {
15441546
return TaggedUnionUtils.get(this, Kind.VariableWidthHistogram);
15451547
}
15461548

1549+
@Nullable
1550+
private final String _customKind;
1551+
1552+
/**
1553+
* Is this a custom {@code Aggregation} defined by a plugin?
1554+
*/
1555+
public boolean _isCustom() {
1556+
return _kind == Kind._Custom;
1557+
}
1558+
1559+
/**
1560+
* Get the actual kind when {@code _kind()} equals {@link Kind#_Custom}
1561+
* (plugin-defined variant).
1562+
*/
1563+
@Nullable
1564+
public final String _customKind() {
1565+
return _customKind;
1566+
}
1567+
1568+
/**
1569+
* Get the custom plugin-defined variant value.
1570+
*
1571+
* @throws IllegalStateException
1572+
* if the current variant is not {@link Kind#_Custom}.
1573+
*/
1574+
public JsonData _custom() {
1575+
return TaggedUnionUtils.get(this, Kind._Custom);
1576+
}
1577+
15471578
@Override
15481579
@SuppressWarnings("unchecked")
15491580
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
@@ -1573,7 +1604,7 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {
15731604

15741605
}
15751606

1576-
generator.writeKey(_kind.jsonValue());
1607+
generator.writeKey(_kind == Kind._Custom ? _customKind : _kind.jsonValue());
15771608
if (_value instanceof JsonpSerializable) {
15781609
((JsonpSerializable) _value).serialize(generator, mapper);
15791610
}
@@ -1590,6 +1621,7 @@ public String toString() {
15901621
public static class Builder extends WithJsonObjectBuilderBase<Builder> {
15911622
private Kind _kind;
15921623
private Object _value;
1624+
private String _customKind;
15931625

15941626
@Nullable
15951627
private Map<String, Aggregation> aggregations;
@@ -2438,6 +2470,22 @@ public ContainerBuilder variableWidthHistogram(
24382470
return this.variableWidthHistogram(fn.apply(new VariableWidthHistogramAggregation.Builder()).build());
24392471
}
24402472

2473+
/**
2474+
* Define this {@code Aggregation} as a plugin-defined variant.
2475+
*
2476+
* @param name
2477+
* the plugin-defined identifier
2478+
* @param data
2479+
* the data for this custom {@code Aggregation}. It is converted
2480+
* internally to {@link JsonData}.
2481+
*/
2482+
public ContainerBuilder _custom(String name, Object data) {
2483+
this._kind = Kind._Custom;
2484+
this._customKind = name;
2485+
this._value = JsonData.of(data);
2486+
return new ContainerBuilder();
2487+
}
2488+
24412489
protected Aggregation build() {
24422490
_checkSingleUse();
24432491
return new Aggregation(this);
@@ -2589,6 +2637,11 @@ protected static void setupAggregationDeserializer(ObjectDeserializer<Builder> o
25892637
op.add(Builder::variableWidthHistogram, VariableWidthHistogramAggregation._DESERIALIZER,
25902638
"variable_width_histogram");
25912639

2640+
op.setUnknownFieldHandler((builder, name, parser, mapper) -> {
2641+
JsonpUtils.ensureCustomVariantsAllowed(parser, mapper);
2642+
builder._custom(name, JsonData._DESERIALIZER.deserialize(parser, mapper));
2643+
});
2644+
25922645
}
25932646

25942647
public static final JsonpDeserializer<Aggregation> _DESERIALIZER = ObjectBuilderDeserializer.lazy(Builder::new,

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/aggregations/InferenceConfig.java

-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@
5555
@JsonpDeserializable
5656
public class InferenceConfig implements TaggedUnion<InferenceConfig.Kind, Object>, JsonpSerializable {
5757

58-
/**
59-
* {@link InferenceConfig} variant kinds.
60-
*/
6158
/**
6259
* {@link InferenceConfig} variant kinds.
6360
*

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/aggregations/MovingAverageAggregation.java

-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ public class MovingAverageAggregation
5555
TaggedUnion<MovingAverageAggregation.Kind, MovingAverageAggregationVariant>,
5656
JsonpSerializable {
5757

58-
/**
59-
* {@link MovingAverageAggregation} variant kinds.
60-
*/
6158
/**
6259
* {@link MovingAverageAggregation} variant kinds.
6360
*

0 commit comments

Comments
 (0)