2727import co .elastic .clients .json .JsonpDeserializer ;
2828import co .elastic .clients .json .JsonpMapper ;
2929import co .elastic .clients .json .JsonpSerializable ;
30+ import co .elastic .clients .json .JsonpUtils ;
3031import co .elastic .clients .json .ObjectDeserializer ;
31- import co .elastic .clients .json .UnionDeserializer ;
3232import co .elastic .clients .util .ModelTypeHelper ;
3333import co .elastic .clients .util .ObjectBuilder ;
3434import co .elastic .clients .util .ObjectBuilderBase ;
3535import co .elastic .clients .util .TaggedUnion ;
3636import co .elastic .clients .util .TaggedUnionUtils ;
3737import jakarta .json .stream .JsonGenerator ;
38+ import jakarta .json .stream .JsonParser ;
39+
3840import java .lang .Boolean ;
3941import java .lang .Double ;
4042import java .lang .Long ;
4143import java .lang .Object ;
4244import java .lang .String ;
45+ import java .util .EnumSet ;
4346import java .util .Objects ;
4447import java .util .function .Consumer ;
4548import javax .annotation .Nullable ;
5053public class FieldValue implements TaggedUnion <FieldValue .Kind , Object >, JsonpSerializable {
5154
5255public enum Kind {
53- Double , Long , Boolean , String
54-
56+ Double , Long , Boolean , String , Null
5557}
5658
5759private final Kind _kind ;
@@ -67,11 +69,6 @@ public final Object _get() {
6769return _value ;
6870}
6971
70- public FieldValue (Kind kind , Object value ) {
71- this ._kind = kind ;
72- this ._value = value ;
73- }
74-
7572public String _toJsonString () {
7673switch (_kind ) {
7774case Double :
@@ -82,6 +79,8 @@ public String _toJsonString() {
8279return String .valueOf (this .boolean_ ());
8380case String :
8481return this .string ();
82+ case Null :
83+ return "null" ;
8584
8685default :
8786throw new IllegalStateException ("Unknown kind " + _kind );
@@ -91,7 +90,9 @@ public String _toJsonString() {
9190private FieldValue (Builder builder ) {
9291
9392this ._kind = ModelTypeHelper .requireNonNull (builder ._kind , builder , "<variant kind>" );
94- this ._value = ModelTypeHelper .requireNonNull (builder ._value , builder , "<variant value>" );
93+ this ._value = this ._kind == Kind .Null
94+ ? null
95+ : ModelTypeHelper .requireNonNull (builder ._value , builder , "<variant value>" );
9596
9697}
9798
@@ -114,7 +115,7 @@ public boolean isDouble() {
114115 * @throws IllegalStateException
115116 * if the current variant is not of the {@code double} kind.
116117 */
117- public Double double_ () {
118+ public double double_ () {
118119return TaggedUnionUtils .get (this , Kind .Double );
119120}
120121
@@ -131,7 +132,7 @@ public boolean isLong() {
131132 * @throws IllegalStateException
132133 * if the current variant is not of the {@code long} kind.
133134 */
134- public Long long_ () {
135+ public long long_ () {
135136return TaggedUnionUtils .get (this , Kind .Long );
136137}
137138
@@ -148,7 +149,7 @@ public boolean isBoolean() {
148149 * @throws IllegalStateException
149150 * if the current variant is not of the {@code boolean} kind.
150151 */
151- public Boolean boolean_ () {
152+ public boolean boolean_ () {
152153return TaggedUnionUtils .get (this , Kind .Boolean );
153154}
154155
@@ -169,76 +170,113 @@ public String string() {
169170return TaggedUnionUtils .get (this , Kind .String );
170171}
171172
173+ /**
174+ * Is this variant instance of kind {@code null}?
175+ */
176+ public boolean isNull () {
177+ return _kind == Kind .Null ;
178+ }
179+
180+ /**
181+ * Get the {@code null} variant value.
182+ *
183+ * @throws IllegalStateException
184+ * if the current variant is not of the {@code null} kind.
185+ */
186+ public String null_ () {
187+ return TaggedUnionUtils .get (this , Kind .Null );
188+ }
189+
172190@ Override
173191public void serialize (JsonGenerator generator , JsonpMapper mapper ) {
174- if (_value instanceof JsonpSerializable ) {
175- ((JsonpSerializable ) _value ).serialize (generator , mapper );
176- } else {
177- switch (_kind ) {
178- case Double :
179- generator .write (((Double ) this ._value ));
180-
181- break ;
182- case Long :
183- generator .write (((Long ) this ._value ));
184-
185- break ;
186- case Boolean :
187- generator .write (((Boolean ) this ._value ));
188-
189- break ;
190- case String :
191- generator .write (((String ) this ._value ));
192-
193- break ;
194- }
192+ switch (_kind ) {
193+ case Double :
194+ generator .write (((Double ) this ._value ));
195+ break ;
196+ case Long :
197+ generator .write (((Long ) this ._value ));
198+ break ;
199+ case Boolean :
200+ generator .write (((Boolean ) this ._value ));
201+ break ;
202+ case String :
203+ generator .write (((String ) this ._value ));
204+ break ;
205+ case Null :
206+ generator .writeNull ();
207+ break ;
195208}
196-
197209}
198210
199211public static class Builder extends ObjectBuilderBase implements ObjectBuilder <FieldValue > {
200212private Kind _kind ;
201213private Object _value ;
202214
203- public Builder double_ (Double v ) {
215+ public ObjectBuilder < FieldValue > double_ (double v ) {
204216this ._kind = Kind .Double ;
205217this ._value = v ;
206218return this ;
207219}
208220
209- public Builder long_ (Long v ) {
221+ public ObjectBuilder < FieldValue > long_ (long v ) {
210222this ._kind = Kind .Long ;
211223this ._value = v ;
212224return this ;
213225}
214226
215- public Builder boolean_ (Boolean v ) {
227+ public ObjectBuilder < FieldValue > boolean_ (boolean v ) {
216228this ._kind = Kind .Boolean ;
217229this ._value = v ;
218230return this ;
219231}
220232
221- public Builder string (String v ) {
233+ public ObjectBuilder < FieldValue > string (String v ) {
222234this ._kind = Kind .String ;
223235this ._value = v ;
224236return this ;
225237}
226238
239+ public ObjectBuilder <FieldValue > null_ () {
240+ this ._kind = Kind .Null ;
241+ this ._value = null ;
242+ return this ;
243+ }
244+
227245public FieldValue build () {
228246_checkSingleUse ();
229247return new FieldValue (this );
230248}
231249
232250}
233251
234- private static JsonpDeserializer <FieldValue > buildFieldValueDeserializer () {
235- return new UnionDeserializer .Builder <FieldValue , Kind , Object >(FieldValue ::new , true )
236- .addMember (Kind .Double , JsonpDeserializer .doubleDeserializer ())
237- .addMember (Kind .Long , JsonpDeserializer .longDeserializer ())
238- .addMember (Kind .Boolean , JsonpDeserializer .booleanDeserializer ())
239- .addMember (Kind .String , JsonpDeserializer .stringDeserializer ()).build ();
240- }
241-
242252public static final JsonpDeserializer <FieldValue > _DESERIALIZER = JsonpDeserializer
243- .lazy (FieldValue ::buildFieldValueDeserializer );
253+ .lazy (() -> JsonpDeserializer .of (
254+ EnumSet .of (JsonParser .Event .VALUE_STRING , JsonParser .Event .VALUE_NUMBER ,
255+ JsonParser .Event .VALUE_NULL , JsonParser .Event .VALUE_TRUE , JsonParser .Event .VALUE_FALSE ),
256+ (parser , mapper , event ) -> {
257+ FieldValue .Builder b = new FieldValue .Builder ();
258+ switch (event ) {
259+ case VALUE_NULL :
260+ b .null_ ();
261+ break ;
262+ case VALUE_STRING :
263+ b .string (parser .getString ());
264+ break ;
265+ case VALUE_TRUE :
266+ b .boolean_ (Boolean .TRUE );
267+ break ;
268+ case VALUE_FALSE :
269+ b .boolean_ (Boolean .FALSE );
270+ break ;
271+ case VALUE_NUMBER :
272+ if (parser .isIntegralNumber ()) {
273+ b .long_ (parser .getLong ());
274+ } else {
275+ b .double_ (parser .getBigDecimal ().doubleValue ());
276+ }
277+ break ;
278+ }
279+ return b .build ();
280+ }));
281+
244282}
0 commit comments