Skip to content

Commit c7dbbb6

Browse files
committed
API: Add formatDuration() helper function
This patch adds a helper method, `formatDuration()`, which properly converts a Go `time.Duration` value into the format accepted by Elasticsearch time units; see: https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#time-units. The generated code will use the function like this: if r.Timeout != 0 { params["timeout"] = formatDuration(r.Timeout) } An integration test has been added as well. Related: #27, #25 Closes: #36
1 parent 956397d commit c7dbbb6

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

esapi/api.search.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esapi/esapi.go

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package esapi // import "github.com/elastic/go-elasticsearch/esapi"
22

33
import (
44
"net/http"
5+
"strconv"
6+
"time"
57
)
68

79
// Transport defines the interface for an API client.
@@ -25,3 +27,13 @@ func BoolPtr(v bool) *bool { return &v }
2527
// which expects a pointer.
2628
//
2729
func IntPtr(v int) *int { return &v }
30+
31+
// formatDuration converts duration to a string in the format
32+
// accepted by Elasticsearch.
33+
//
34+
func formatDuration(d time.Duration) string {
35+
if d < time.Millisecond {
36+
return strconv.FormatInt(int64(d), 10) + "nanos"
37+
}
38+
return strconv.FormatInt(int64(d)/int64(time.Millisecond), 10) + "ms"
39+
}

esapi/esapi_integration_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// +build integration
2+
3+
package esapi_test
4+
5+
import (
6+
"encoding/json"
7+
"fmt"
8+
"testing"
9+
"time"
10+
11+
"github.com/elastic/go-elasticsearch"
12+
)
13+
14+
func TestAPI(t *testing.T) {
15+
t.Run("Search", func(t *testing.T) {
16+
es, err := elasticsearch.NewDefaultClient()
17+
if err != nil {
18+
t.Fatalf("Error creating the client: %s\n", err)
19+
}
20+
21+
res, err := es.Search(es.Search.WithTimeout(500 * time.Millisecond))
22+
if err != nil {
23+
t.Fatalf("Error getting the response: %s\n", err)
24+
}
25+
defer res.Body.Close()
26+
27+
if res.IsError() {
28+
t.Fatalf("Error response: %s", res.String())
29+
}
30+
31+
var d map[string]interface{}
32+
err = json.NewDecoder(res.Body).Decode(&d)
33+
if err != nil {
34+
t.Fatalf("Error parsing the response: %s\n", err)
35+
}
36+
fmt.Printf("took=%vms\n", d["took"])
37+
})
38+
}

esapi/esapi_internal_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package esapi
44

55
import (
66
"testing"
7+
"time"
78
)
89

910
func TestAPIHelpers(t *testing.T) {
@@ -25,4 +26,28 @@ func TestAPIHelpers(t *testing.T) {
2526
t.Errorf("Expected 0, got: %v", v)
2627
}
2728
})
29+
30+
t.Run("FormatDuration", func(t *testing.T) {
31+
var tt = []struct {
32+
duration time.Duration
33+
expected string
34+
}{
35+
{1 * time.Nanosecond, "1nanos"},
36+
{100 * time.Nanosecond, "100nanos"},
37+
{1 * time.Microsecond, "1000nanos"},
38+
{1 * time.Millisecond, "1ms"},
39+
{100 * time.Millisecond, "100ms"},
40+
{1 * time.Minute, "60000ms"},
41+
{10 * time.Minute, "600000ms"},
42+
{1 * time.Hour, "3600000ms"},
43+
{10 * time.Hour, "36000000ms"},
44+
}
45+
46+
for _, tc := range tt {
47+
actual := formatDuration(tc.duration)
48+
if actual != tc.expected {
49+
t.Errorf("Unexpected output: got=%s, want=%s", actual, tc.expected)
50+
}
51+
}
52+
})
2853
}

0 commit comments

Comments
 (0)