Skip to content

Commit

Permalink
Fix ScoreFunction: variants are optional (#257) (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
swallez authored Apr 29, 2022
1 parent 0a47bd2 commit 3446073
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import co.elastic.clients.json.JsonpDeserializable;
import co.elastic.clients.json.JsonpDeserializer;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.JsonpSerializable;
import co.elastic.clients.json.ObjectBuilderDeserializer;
import co.elastic.clients.json.ObjectDeserializer;
import co.elastic.clients.util.ObjectBuilder;
Expand All @@ -43,14 +44,13 @@
* specification</a>
*/

public abstract class DecayFunctionBase extends ScoreFunctionBase {
public abstract class DecayFunctionBase implements JsonpSerializable {
@Nullable
private final MultiValueMode multiValueMode;

// ---------------------------------------------------------------------------------------------

protected DecayFunctionBase(AbstractBuilder<?> builder) {
super(builder);

this.multiValueMode = builder.multiValueMode;

Expand All @@ -64,9 +64,17 @@ public final MultiValueMode multiValueMode() {
return this.multiValueMode;
}

/**
* Serialize this object to JSON.
*/
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartObject();
serializeInternal(generator, mapper);
generator.writeEnd();
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

super.serializeInternal(generator, mapper);
if (this.multiValueMode != null) {
generator.writeKey("multi_value_mode");
this.multiValueMode.serialize(generator, mapper);
Expand All @@ -76,7 +84,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

protected abstract static class AbstractBuilder<BuilderT extends AbstractBuilder<BuilderT>>
extends
ScoreFunctionBase.AbstractBuilder<BuilderT> {
WithJsonObjectBuilderBase<BuilderT> {
@Nullable
private MultiValueMode multiValueMode;

Expand All @@ -88,12 +96,14 @@ public final BuilderT multiValueMode(@Nullable MultiValueMode value) {
return self();
}

protected abstract BuilderT self();

}

// ---------------------------------------------------------------------------------------------
protected static <BuilderT extends AbstractBuilder<BuilderT>> void setupDecayFunctionBaseDeserializer(
ObjectDeserializer<BuilderT> op) {
ScoreFunctionBase.setupScoreFunctionBaseDeserializer(op);

op.add(AbstractBuilder::multiValueMode, MultiValueMode._DESERIALIZER, "multi_value_mode");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import co.elastic.clients.json.JsonpDeserializable;
import co.elastic.clients.json.JsonpDeserializer;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.JsonpSerializable;
import co.elastic.clients.json.ObjectBuilderDeserializer;
import co.elastic.clients.json.ObjectDeserializer;
import co.elastic.clients.util.ApiTypeHelper;
import co.elastic.clients.util.ObjectBuilder;
import co.elastic.clients.util.WithJsonObjectBuilderBase;
import jakarta.json.stream.JsonGenerator;
import java.lang.Double;
import java.lang.String;
Expand All @@ -46,7 +48,7 @@
* specification</a>
*/
@JsonpDeserializable
public class FieldValueFactorScoreFunction extends ScoreFunctionBase implements FunctionScoreVariant {
public class FieldValueFactorScoreFunction implements FunctionScoreVariant, JsonpSerializable {
private final String field;

@Nullable
Expand All @@ -61,7 +63,6 @@ public class FieldValueFactorScoreFunction extends ScoreFunctionBase implements
// ---------------------------------------------------------------------------------------------

private FieldValueFactorScoreFunction(Builder builder) {
super(builder);

this.field = ApiTypeHelper.requireNonNull(builder.field, this, "field");
this.factor = builder.factor;
Expand Down Expand Up @@ -113,9 +114,17 @@ public final FieldValueFactorModifier modifier() {
return this.modifier;
}

/**
* Serialize this object to JSON.
*/
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartObject();
serializeInternal(generator, mapper);
generator.writeEnd();
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

super.serializeInternal(generator, mapper);
generator.writeKey("field");
generator.write(this.field);

Expand All @@ -142,7 +151,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
* Builder for {@link FieldValueFactorScoreFunction}.
*/

public static class Builder extends ScoreFunctionBase.AbstractBuilder<Builder>
public static class Builder extends WithJsonObjectBuilderBase<Builder>
implements
ObjectBuilder<FieldValueFactorScoreFunction> {
private String field;
Expand Down Expand Up @@ -216,7 +225,7 @@ public FieldValueFactorScoreFunction build() {

protected static void setupFieldValueFactorScoreFunctionDeserializer(
ObjectDeserializer<FieldValueFactorScoreFunction.Builder> op) {
ScoreFunctionBase.setupScoreFunctionBaseDeserializer(op);

op.add(Builder::field, JsonpDeserializer.stringDeserializer(), "field");
op.add(Builder::factor, JsonpDeserializer.doubleDeserializer(), "factor");
op.add(Builder::missing, JsonpDeserializer.doubleDeserializer(), "missing");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,19 @@ public String jsonValue() {

}

@Nullable
private final Kind _kind;
@Nullable
private final Object _value;

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

@Override
@Nullable
public final Object _get() {
return _value;
}
Expand All @@ -110,10 +114,15 @@ public final Object _get() {
@Nullable
private final Double weight;

public FunctionScore(FunctionScoreVariant value) {
public FunctionScore(@Nullable FunctionScoreVariant value) {

this._kind = ApiTypeHelper.requireNonNull(value._functionScoreKind(), this, "<variant kind>");
this._value = ApiTypeHelper.requireNonNull(value, this, "<variant value>");
if (value != null) {
this._kind = ApiTypeHelper.requireNonNull(value._functionScoreKind(), this, "<variant kind>");
this._value = ApiTypeHelper.requireNonNull(value, this, "<variant value>");
} else {
this._kind = null;
this._value = null;
}

this.filter = null;
this.weight = null;
Expand All @@ -122,8 +131,13 @@ public FunctionScore(FunctionScoreVariant value) {

private FunctionScore(Builder builder) {

this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, "<variant kind>");
this._value = ApiTypeHelper.requireNonNull(builder._value, builder, "<variant value>");
if (builder._value != null) {
this._kind = ApiTypeHelper.requireNonNull(builder._kind, builder, "<variant kind>");
this._value = ApiTypeHelper.requireNonNull(builder._value, builder, "<variant value>");
} else {
this._kind = null;
this._value = null;
}

this.filter = builder.filter;
this.weight = builder.weight;
Expand Down Expand Up @@ -270,16 +284,19 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {

}

generator.writeKey(_kind.jsonValue());
if (_value instanceof JsonpSerializable) {
((JsonpSerializable) _value).serialize(generator, mapper);
if (this._value != null) {
generator.writeKey(_kind.jsonValue());
if (_value instanceof JsonpSerializable) {
((JsonpSerializable) _value).serialize(generator, mapper);
}

}

generator.writeEnd();

}

public static class Builder extends WithJsonObjectBuilderBase<Builder> {
public static class Builder extends WithJsonObjectBuilderBase<Builder> implements ObjectBuilder<FunctionScore> {
private Kind _kind;
private Object _value;

Expand Down Expand Up @@ -379,7 +396,7 @@ public ContainerBuilder scriptScore(
return this.scriptScore(fn.apply(new ScriptScoreFunction.Builder()).build());
}

protected FunctionScore build() {
public FunctionScore build() {
_checkSingleUse();
return new FunctionScore(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import co.elastic.clients.json.JsonpDeserializable;
import co.elastic.clients.json.JsonpDeserializer;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.JsonpSerializable;
import co.elastic.clients.json.ObjectBuilderDeserializer;
import co.elastic.clients.json.ObjectDeserializer;
import co.elastic.clients.util.ObjectBuilder;
import co.elastic.clients.util.WithJsonObjectBuilderBase;
import jakarta.json.stream.JsonGenerator;
import java.lang.String;
import java.util.Objects;
Expand All @@ -44,7 +46,7 @@
* specification</a>
*/
@JsonpDeserializable
public class RandomScoreFunction extends ScoreFunctionBase implements FunctionScoreVariant {
public class RandomScoreFunction implements FunctionScoreVariant, JsonpSerializable {
@Nullable
private final String field;

Expand All @@ -54,7 +56,6 @@ public class RandomScoreFunction extends ScoreFunctionBase implements FunctionSc
// ---------------------------------------------------------------------------------------------

private RandomScoreFunction(Builder builder) {
super(builder);

this.field = builder.field;
this.seed = builder.seed;
Expand Down Expand Up @@ -89,9 +90,17 @@ public final String seed() {
return this.seed;
}

/**
* Serialize this object to JSON.
*/
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartObject();
serializeInternal(generator, mapper);
generator.writeEnd();
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

super.serializeInternal(generator, mapper);
if (this.field != null) {
generator.writeKey("field");
generator.write(this.field);
Expand All @@ -111,7 +120,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
* Builder for {@link RandomScoreFunction}.
*/

public static class Builder extends ScoreFunctionBase.AbstractBuilder<Builder>
public static class Builder extends WithJsonObjectBuilderBase<Builder>
implements
ObjectBuilder<RandomScoreFunction> {
@Nullable
Expand Down Expand Up @@ -163,7 +172,7 @@ public RandomScoreFunction build() {
.lazy(Builder::new, RandomScoreFunction::setupRandomScoreFunctionDeserializer);

protected static void setupRandomScoreFunctionDeserializer(ObjectDeserializer<RandomScoreFunction.Builder> op) {
ScoreFunctionBase.setupScoreFunctionBaseDeserializer(op);

op.add(Builder::field, JsonpDeserializer.stringDeserializer(), "field");
op.add(Builder::seed, JsonpDeserializer.stringDeserializer(), "seed");

Expand Down
Loading

0 comments on commit 3446073

Please sign in to comment.