Skip to content

Commit ae1f6bd

Browse files
committed
Add Pair and NamedValue tests
1 parent 473731d commit ae1f6bd

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

java-client/src/main/java/co/elastic/clients/util/NamedValue.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import java.util.EnumSet;
3030
import java.util.function.Supplier;
3131

32+
/**
33+
* A named value, i.e. a string/value pair.
34+
*/
3235
public class NamedValue<T> {
3336

3437
private final String name;
@@ -52,6 +55,10 @@ public static <T> NamedValue<T> of(String name, T value) {
5255
}
5356

5457
public static <T> JsonpDeserializer<NamedValue<T>> deserializer(Supplier<JsonpDeserializer<T>> valueParserBuilder) {
58+
return deserializer(valueParserBuilder.get());
59+
}
60+
61+
public static <T> JsonpDeserializer<NamedValue<T>> deserializer(JsonpDeserializer<T> valueDeserializer) {
5562
return new JsonpDeserializerBase<NamedValue<T>>(EnumSet.of(JsonParser.Event.START_OBJECT)) {
5663
@Override
5764
public NamedValue<T> deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
@@ -60,7 +67,7 @@ public NamedValue<T> deserialize(JsonParser parser, JsonpMapper mapper, JsonPars
6067
String name = parser.getString();
6168

6269
try {
63-
T value = valueParserBuilder.get().deserialize(parser, mapper);
70+
T value = valueDeserializer.deserialize(parser, mapper);
6471
JsonpUtils.expectNextEvent(parser, JsonParser.Event.END_OBJECT);
6572

6673
return new NamedValue<>(name, value);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.util;
21+
22+
import co.elastic.clients.json.JsonpDeserializer;
23+
import co.elastic.clients.json.JsonpDeserializerBase;
24+
import co.elastic.clients.json.JsonpMapper;
25+
import co.elastic.clients.json.JsonpMappingException;
26+
import co.elastic.clients.json.JsonpUtils;
27+
import jakarta.json.stream.JsonParser;
28+
29+
import java.util.EnumSet;
30+
import java.util.function.Function;
31+
32+
/**
33+
* A key/value pair. Used to represent pairs where the name is not a string (generally an enum).
34+
* <p>
35+
* The key must have a string representation in JSON.
36+
*/
37+
public class Pair<K, V> {
38+
39+
private final K name;
40+
private final V value;
41+
42+
public Pair(K name, V value) {
43+
this.name = name;
44+
this.value = value;
45+
}
46+
47+
public K key() {
48+
return this.name;
49+
}
50+
51+
public V value() {
52+
return this.value;
53+
}
54+
55+
public static <K, V> Pair<K, V> of(K name, V value) {
56+
return new Pair<>(name, value);
57+
}
58+
59+
public static <K, V> JsonpDeserializer<Pair<K, V>> deserializer(Function<String, K> keyDeserializer, JsonpDeserializer<V> valueDeserializer) {
60+
return new JsonpDeserializerBase<Pair<K, V>>(EnumSet.of(JsonParser.Event.START_OBJECT)) {
61+
@Override
62+
public Pair<K, V> deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
63+
64+
JsonpUtils.expectNextEvent(parser, JsonParser.Event.KEY_NAME);
65+
String name = parser.getString();
66+
67+
try {
68+
K key = keyDeserializer.apply(parser.getString());
69+
V value = valueDeserializer.deserialize(parser, mapper);
70+
JsonpUtils.expectNextEvent(parser, JsonParser.Event.END_OBJECT);
71+
72+
return new Pair<>(key, value);
73+
} catch (Exception e) {
74+
throw JsonpMappingException.from(e, null, name, parser);
75+
}
76+
}
77+
};
78+
}
79+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.util;
21+
22+
import co.elastic.clients.elasticsearch._types.SortOrder;
23+
import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregation;
24+
import co.elastic.clients.elasticsearch.model.ModelTestCase;
25+
import org.junit.jupiter.api.Test;
26+
27+
class NamedValueTest extends ModelTestCase {
28+
29+
@Test
30+
public void testTermAggregation() {
31+
32+
String json = "{\"order\":[{\"a\":\"asc\"},{\"b\":\"desc\"}]}";
33+
34+
TermsAggregation ta = TermsAggregation.of(b -> b
35+
.order(NamedValue.of("a", SortOrder.Asc))
36+
.order(NamedValue.of("b", SortOrder.Desc)
37+
)
38+
);
39+
40+
ta = checkJsonRoundtrip(ta, json);
41+
42+
assertEquals(2, ta.order().size());
43+
44+
assertEquals("a", ta.order().get(0).name());
45+
assertEquals(SortOrder.Asc, ta.order().get(0).value());
46+
assertEquals("b", ta.order().get(1).name());
47+
assertEquals(SortOrder.Desc, ta.order().get(1).value());
48+
}
49+
}

0 commit comments

Comments
 (0)