Skip to content

Add support for plugin-defined custom components in union types #370

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

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
@JsonpDeserializable
public class SortOptions implements TaggedUnion<SortOptions.Kind, Object>, JsonpSerializable {

/**
* {@link SortOptions} variant kinds.
*/
/**
* {@link SortOptions} variant kinds.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
@JsonpDeserializable
public class Transform implements TaggedUnion<Transform.Kind, Object>, JsonpSerializable {

/**
* {@link Transform} variant kinds.
*/
/**
* {@link Transform} variant kinds.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package co.elastic.clients.elasticsearch._types.aggregations;

import co.elastic.clients.json.ExternallyTaggedUnion;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.json.JsonEnum;
import co.elastic.clients.json.JsonpDeserializable;
import co.elastic.clients.json.JsonpDeserializer;
Expand All @@ -34,9 +35,10 @@
import co.elastic.clients.util.ApiTypeHelper;
import co.elastic.clients.util.ObjectBuilder;
import co.elastic.clients.util.ObjectBuilderBase;
import co.elastic.clients.util.TaggedUnion;
import co.elastic.clients.util.OpenTaggedUnion;
import co.elastic.clients.util.TaggedUnionUtils;
import jakarta.json.stream.JsonGenerator;
import java.lang.Object;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -52,11 +54,8 @@
* specification</a>
*/

public class Aggregate implements TaggedUnion<Aggregate.Kind, AggregateVariant>, JsonpSerializable {
public class Aggregate implements OpenTaggedUnion<Aggregate.Kind, Object>, JsonpSerializable {

/**
* {@link Aggregate} variant kinds.
*/
/**
* {@link Aggregate} variant kinds.
*
Expand Down Expand Up @@ -198,6 +197,9 @@ public enum Kind implements JsonEnum {

WeightedAvg("weighted_avg"),

/** A custom {@code Aggregate} defined by a plugin */
_Custom(null)

;

private final String jsonValue;
Expand All @@ -213,36 +215,48 @@ public String jsonValue() {
}

private final Kind _kind;
private final AggregateVariant _value;
private final Object _value;

@Override
public final Kind _kind() {
return _kind;
}

@Override
public final AggregateVariant _get() {
public final Object _get() {
return _value;
}

public Aggregate(AggregateVariant value) {

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

}

private Aggregate(Builder builder) {

this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, "<variant kind>");
this._value = ApiTypeHelper.requireNonNull(builder._value, builder, "<variant value>");
this._customKind = builder._customKind;

}

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

/**
* Build a custom plugin-defined {@code Aggregate}, given its kind and some JSON
* data
*/
public Aggregate(String kind, JsonData value) {
this._kind = Kind._Custom;
this._value = value;
this._customKind = kind;
}

/**
* Is this variant instance of kind {@code adjacency_matrix}?
*/
Expand Down Expand Up @@ -1379,6 +1393,35 @@ public WeightedAvgAggregate weightedAvg() {
return TaggedUnionUtils.get(this, Kind.WeightedAvg);
}

@Nullable
private final String _customKind;

/**
* Is this a custom {@code Aggregate} defined by a plugin?
*/
public boolean _isCustom() {
return _kind == Kind._Custom;
}

/**
* Get the actual kind when {@code _kind()} equals {@link Kind#_Custom}
* (plugin-defined variant).
*/
@Nullable
public final String _customKind() {
return _customKind;
}

/**
* Get the custom plugin-defined variant value.
*
* @throws IllegalStateException
* if the current variant is not {@link Kind#_Custom}.
*/
public JsonData _custom() {
return TaggedUnionUtils.get(this, Kind._Custom);
}

@Override
public void serialize(JsonGenerator generator, JsonpMapper mapper) {

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

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<Aggregate> {
private Kind _kind;
private AggregateVariant _value;
private Object _value;
private String _customKind;

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

/**
* Define this {@code Aggregate} as a plugin-defined variant.
*
* @param name
* the plugin-defined identifier
* @param data
* the data for this custom {@code Aggregate}. It is converted
* internally to {@link JsonData}.
*/
public ObjectBuilder<Aggregate> _custom(String name, Object data) {
this._kind = Kind._Custom;
this._customKind = name;
this._value = JsonData.of(data);
return this;
}

public Aggregate build() {
_checkSingleUse();
return new Aggregate(this);
Expand Down Expand Up @@ -2187,7 +2247,7 @@ public Aggregate build() {
deserializers.put("variable_width_histogram", VariableWidthHistogramAggregate._DESERIALIZER);
deserializers.put("weighted_avg", WeightedAvgAggregate._DESERIALIZER);

_TYPED_KEYS_DESERIALIZER = new ExternallyTaggedUnion.Deserializer<>(deserializers,
(name, value) -> new Aggregate(value)).typedKeys();
_TYPED_KEYS_DESERIALIZER = new ExternallyTaggedUnion.Deserializer<>(deserializers, Aggregate::new,
Aggregate::new).typedKeys();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import co.elastic.clients.json.ObjectDeserializer;
import co.elastic.clients.util.ApiTypeHelper;
import co.elastic.clients.util.ObjectBuilder;
import co.elastic.clients.util.TaggedUnion;
import co.elastic.clients.util.OpenTaggedUnion;
import co.elastic.clients.util.TaggedUnionUtils;
import co.elastic.clients.util.WithJsonObjectBuilderBase;
import jakarta.json.stream.JsonGenerator;
Expand All @@ -55,11 +55,8 @@
* specification</a>
*/
@JsonpDeserializable
public class Aggregation implements TaggedUnion<Aggregation.Kind, Object>, JsonpSerializable {
public class Aggregation implements OpenTaggedUnion<Aggregation.Kind, Object>, JsonpSerializable {

/**
* {@link Aggregation} variant kinds.
*/
/**
* {@link Aggregation} variant kinds.
*
Expand Down Expand Up @@ -215,6 +212,9 @@ public enum Kind implements JsonEnum {

VariableWidthHistogram("variable_width_histogram"),

/** A custom {@code Aggregation} defined by a plugin */
_Custom(null)

;

private final String jsonValue;
Expand Down Expand Up @@ -250,6 +250,7 @@ public Aggregation(AggregationVariant value) {

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

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

this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, "<variant kind>");
this._value = ApiTypeHelper.requireNonNull(builder._value, builder, "<variant value>");
this._customKind = builder._customKind;

this.aggregations = ApiTypeHelper.unmodifiable(builder.aggregations);
this.meta = ApiTypeHelper.unmodifiable(builder.meta);
Expand Down Expand Up @@ -1544,6 +1546,35 @@ public VariableWidthHistogramAggregation variableWidthHistogram() {
return TaggedUnionUtils.get(this, Kind.VariableWidthHistogram);
}

@Nullable
private final String _customKind;

/**
* Is this a custom {@code Aggregation} defined by a plugin?
*/
public boolean _isCustom() {
return _kind == Kind._Custom;
}

/**
* Get the actual kind when {@code _kind()} equals {@link Kind#_Custom}
* (plugin-defined variant).
*/
@Nullable
public final String _customKind() {
return _customKind;
}

/**
* Get the custom plugin-defined variant value.
*
* @throws IllegalStateException
* if the current variant is not {@link Kind#_Custom}.
*/
public JsonData _custom() {
return TaggedUnionUtils.get(this, Kind._Custom);
}

@Override
@SuppressWarnings("unchecked")
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
Expand Down Expand Up @@ -1573,7 +1604,7 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {

}

generator.writeKey(_kind.jsonValue());
generator.writeKey(_kind == Kind._Custom ? _customKind : _kind.jsonValue());
if (_value instanceof JsonpSerializable) {
((JsonpSerializable) _value).serialize(generator, mapper);
}
Expand All @@ -1590,6 +1621,7 @@ public String toString() {
public static class Builder extends WithJsonObjectBuilderBase<Builder> {
private Kind _kind;
private Object _value;
private String _customKind;

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

/**
* Define this {@code Aggregation} as a plugin-defined variant.
*
* @param name
* the plugin-defined identifier
* @param data
* the data for this custom {@code Aggregation}. It is converted
* internally to {@link JsonData}.
*/
public ContainerBuilder _custom(String name, Object data) {
this._kind = Kind._Custom;
this._customKind = name;
this._value = JsonData.of(data);
return new ContainerBuilder();
}

protected Aggregation build() {
_checkSingleUse();
return new Aggregation(this);
Expand Down Expand Up @@ -2589,6 +2637,11 @@ protected static void setupAggregationDeserializer(ObjectDeserializer<Builder> o
op.add(Builder::variableWidthHistogram, VariableWidthHistogramAggregation._DESERIALIZER,
"variable_width_histogram");

op.setUnknownFieldHandler((builder, name, parser, mapper) -> {
JsonpUtils.ensureCustomVariantsAllowed(parser, mapper);
builder._custom(name, JsonData._DESERIALIZER.deserialize(parser, mapper));
});

}

public static final JsonpDeserializer<Aggregation> _DESERIALIZER = ObjectBuilderDeserializer.lazy(Builder::new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
@JsonpDeserializable
public class InferenceConfig implements TaggedUnion<InferenceConfig.Kind, Object>, JsonpSerializable {

/**
* {@link InferenceConfig} variant kinds.
*/
/**
* {@link InferenceConfig} variant kinds.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ public class MovingAverageAggregation
TaggedUnion<MovingAverageAggregation.Kind, MovingAverageAggregationVariant>,
JsonpSerializable {

/**
* {@link MovingAverageAggregation} variant kinds.
*/
/**
* {@link MovingAverageAggregation} variant kinds.
*
Expand Down
Loading