Skip to content

Commit cf8b6fd

Browse files
authored
Better builder for Time (#1141)
* better builder for time * checkstyle
1 parent 7e39fa7 commit cf8b6fd

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

  • java-client/src

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/Time.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ public Time build() {
194194
return new Time(this);
195195
}
196196

197+
public ObjectBuilder<Time> time(Integer t, TimeUnit u) {
198+
this._kind = Kind.Time;
199+
this._value = String.valueOf(t).concat(u.jsonValue());
200+
return this;
201+
}
197202
}
198203

199204
private static JsonpDeserializer<Time> buildTimeDeserializer() {

java-client/src/test/java/co/elastic/clients/elasticsearch/model/OverloadsTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
import co.elastic.clients.elasticsearch._types.FieldSort;
2424
import co.elastic.clients.elasticsearch._types.FieldValue;
2525
import co.elastic.clients.elasticsearch._types.ScriptSource;
26+
import co.elastic.clients.elasticsearch._types.Time;
27+
import co.elastic.clients.elasticsearch._types.TimeUnit;
2628
import co.elastic.clients.elasticsearch._types.aggregations.TopMetrics;
2729
import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery;
2830
import co.elastic.clients.elasticsearch.core.MsearchRequest;
31+
import co.elastic.clients.elasticsearch.core.OpenPointInTimeRequest;
2932
import co.elastic.clients.elasticsearch.core.SearchRequest;
3033
import co.elastic.clients.elasticsearch.core.SearchResponse;
3134
import co.elastic.clients.testkit.ModelTestCase;
@@ -89,12 +92,12 @@ public void voidClassTDocumentOverload() throws IOException {
8992
// nor testing anything, just checking that this compiles
9093
ElasticsearchClient client = ElasticsearchClient.of(e -> e.host("http://localhost:9200"));
9194

92-
SearchResponse<Void> resp = client.search(s -> s,Void.class);
95+
SearchResponse<Void> resp = client.search(s -> s, Void.class);
9396
SearchResponse<Void> respDefault = client.search(s -> s);
9497
}
9598

9699
@Test
97-
public void searchRequestBodyOverloads() throws IOException {
100+
public void searchRequestBodyOverloads() {
98101

99102
// Normal search request
100103
SearchRequest searchRequest = SearchRequest.of(b -> b
@@ -147,4 +150,22 @@ public void searchRequestBodyOverloads() throws IOException {
147150
// Assert both variants result in the same serialization
148151
assertEquals(scriptSourceStandard.toString(), scriptSourceOverload.toString());
149152
}
153+
154+
@Test
155+
public void timeOverload() {
156+
OpenPointInTimeRequest opr = OpenPointInTimeRequest.of(o -> o.index("test").keepAlive(k -> k.time(
157+
"5s")));
158+
OpenPointInTimeRequest opr1 =
159+
OpenPointInTimeRequest.of(o -> o.index("test").keepAlive(k -> k.time(5, TimeUnit.Seconds)));
160+
OpenPointInTimeRequest opr2 =
161+
OpenPointInTimeRequest.of(o -> o.index("test").keepAlive(Time.of(t -> t.time("5s"))));
162+
OpenPointInTimeRequest opr3 =
163+
OpenPointInTimeRequest.of(o -> o.index("test").keepAlive(Time.of(t -> t.time(5,
164+
TimeUnit.Seconds))));
165+
166+
// Assert all variants result in the same serialization
167+
assertEquals(opr.toString(), opr1.toString());
168+
assertEquals(opr.toString(), opr2.toString());
169+
assertEquals(opr.toString(), opr3.toString());
170+
}
150171
}

0 commit comments

Comments
 (0)