Skip to content

Commit 4e3c0e6

Browse files
authored
Add support for plugin-defined custom components in union types (#370) (#373)
1 parent a4f0224 commit 4e3c0e6

File tree

95 files changed

+1050
-455
lines changed

Some content is hidden

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

95 files changed

+1050
-455
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
*
@@ -207,6 +204,9 @@ public enum Kind implements JsonEnum {
207204

208205
VariableWidthHistogram("variable_width_histogram"),
209206

207+
/** A custom {@code Aggregation} defined by a plugin */
208+
_Custom(null)
209+
210210
;
211211

212212
private final String jsonValue;
@@ -242,6 +242,7 @@ public Aggregation(AggregationVariant value) {
242242

243243
this._kind = ApiTypeHelper.requireNonNull(value._aggregationKind(), this, "<variant kind>");
244244
this._value = ApiTypeHelper.requireNonNull(value, this, "<variant value>");
245+
this._customKind = null;
245246

246247
this.aggregations = null;
247248
this.meta = null;
@@ -252,6 +253,7 @@ private Aggregation(Builder builder) {
252253

253254
this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, "<variant kind>");
254255
this._value = ApiTypeHelper.requireNonNull(builder._value, builder, "<variant value>");
256+
this._customKind = builder._customKind;
255257

256258
this.aggregations = ApiTypeHelper.unmodifiable(builder.aggregations);
257259
this.meta = ApiTypeHelper.unmodifiable(builder.meta);
@@ -1465,6 +1467,35 @@ public VariableWidthHistogramAggregation variableWidthHistogram() {
14651467
return TaggedUnionUtils.get(this, Kind.VariableWidthHistogram);
14661468
}
14671469

1470+
@Nullable
1471+
private final String _customKind;
1472+
1473+
/**
1474+
* Is this a custom {@code Aggregation} defined by a plugin?
1475+
*/
1476+
public boolean _isCustom() {
1477+
return _kind == Kind._Custom;
1478+
}
1479+
1480+
/**
1481+
* Get the actual kind when {@code _kind()} equals {@link Kind#_Custom}
1482+
* (plugin-defined variant).
1483+
*/
1484+
@Nullable
1485+
public final String _customKind() {
1486+
return _customKind;
1487+
}
1488+
1489+
/**
1490+
* Get the custom plugin-defined variant value.
1491+
*
1492+
* @throws IllegalStateException
1493+
* if the current variant is not {@link Kind#_Custom}.
1494+
*/
1495+
public JsonData _custom() {
1496+
return TaggedUnionUtils.get(this, Kind._Custom);
1497+
}
1498+
14681499
@Override
14691500
@SuppressWarnings("unchecked")
14701501
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
@@ -1494,7 +1525,7 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {
14941525

14951526
}
14961527

1497-
generator.writeKey(_kind.jsonValue());
1528+
generator.writeKey(_kind == Kind._Custom ? _customKind : _kind.jsonValue());
14981529
if (_value instanceof JsonpSerializable) {
14991530
((JsonpSerializable) _value).serialize(generator, mapper);
15001531
}
@@ -1511,6 +1542,7 @@ public String toString() {
15111542
public static class Builder extends WithJsonObjectBuilderBase<Builder> {
15121543
private Kind _kind;
15131544
private Object _value;
1545+
private String _customKind;
15141546

15151547
@Nullable
15161548
private Map<String, Aggregation> aggregations;
@@ -2315,6 +2347,22 @@ public ContainerBuilder variableWidthHistogram(
23152347
return this.variableWidthHistogram(fn.apply(new VariableWidthHistogramAggregation.Builder()).build());
23162348
}
23172349

2350+
/**
2351+
* Define this {@code Aggregation} as a plugin-defined variant.
2352+
*
2353+
* @param name
2354+
* the plugin-defined identifier
2355+
* @param data
2356+
* the data for this custom {@code Aggregation}. It is converted
2357+
* internally to {@link JsonData}.
2358+
*/
2359+
public ContainerBuilder _custom(String name, Object data) {
2360+
this._kind = Kind._Custom;
2361+
this._customKind = name;
2362+
this._value = JsonData.of(data);
2363+
return new ContainerBuilder();
2364+
}
2365+
23182366
protected Aggregation build() {
23192367
_checkSingleUse();
23202368
return new Aggregation(this);
@@ -2462,6 +2510,11 @@ protected static void setupAggregationDeserializer(ObjectDeserializer<Builder> o
24622510
op.add(Builder::variableWidthHistogram, VariableWidthHistogramAggregation._DESERIALIZER,
24632511
"variable_width_histogram");
24642512

2513+
op.setUnknownFieldHandler((builder, name, parser, mapper) -> {
2514+
JsonpUtils.ensureCustomVariantsAllowed(parser, mapper);
2515+
builder._custom(name, JsonData._DESERIALIZER.deserialize(parser, mapper));
2516+
});
2517+
24652518
}
24662519

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

0 commit comments

Comments
 (0)