Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid time duration formatting #25

Closed
andrewkroh opened this issue Feb 27, 2019 · 2 comments
Closed

Invalid time duration formatting #25

andrewkroh opened this issue Feb 27, 2019 · 2 comments
Labels

Comments

@andrewkroh
Copy link
Member

I was trying to do a scroll query and hit an issue caused by the formatting of time durations that are passed to Elasticsearch. You can see and example in:

https://play.golang.org/p/fKd7ErkUTJB

I tracked down the line in the generator but I wasn't exactly sure how to regenerate the source. (I think this would be a good topic for the readme or maybe a contributing doc.)

The scroll time was being formatted based on the Go time.Duration format. So for one minute it would use 16666h40m0s. Elasticsearch supports nanoseconds as a time unit so it can pass the Go time.Duration value through without any conversion just by appending "nanos".

diff --git a/internal/cmd/generate/commands/gensource/generator.go b/internal/cmd/generate/commands/gensource/generator.go
index cf05218..50b7936 100755
--- a/internal/cmd/generate/commands/gensource/generator.go
+++ b/internal/cmd/generate/commands/gensource/generator.go
@@ -625,7 +625,7 @@ func (r ` + g.Endpoint.MethodWithNamespace() + `Request) Do(ctx context.Context,
                                fieldValue = `strings.Join(r.` + fieldName + `, ",")`
                        case "time.Duration":
                                fieldCondition = `r.` + fieldName + ` != 0`
-                               fieldValue = `time.Duration(r.` + fieldName + ` * time.Millisecond).String()`
+                               fieldValue = `strconv.FormatInt(int64(r.` + fieldName +`), 10) + "nanos"`
                        default: // interface{}
                                fieldCondition = `r.` + fieldName + ` != nil`
                                // TODO: Use type switching instead?
@andrewkroh andrewkroh added the bug label Feb 27, 2019
@karmi
Copy link
Contributor

karmi commented Feb 27, 2019

Thanks for the report, @andrewkroh! I was expecting to hit this problem in time, and was aware that the conversion in that spot is too naïve. I'll have a look into that soon.

And yes, documentation on the generator is a good idea — when the setup is right, make gen-api is the command to run.

andrewkroh added a commit to andrewkroh/go-elasticsearch that referenced this issue Feb 27, 2019
The scroll time was being formatted based on the Go time.Duration format. So for one minute
it would use 16666h40m0s. Elasticsearch supports nanoseconds as a time unit so so it can
pass the Go time.Duration value through without any conversion just by appending "nanos".

Fixes elastic#25
andrewkroh added a commit to andrewkroh/go-elasticsearch that referenced this issue Feb 27, 2019
The scroll time was being formatted based on the Go time.Duration format. So for one minute
it would use 16666h40m0s. Elasticsearch supports nanoseconds as a time unit so so it can
pass the Go time.Duration value through without any conversion just by appending "nanos".

This updates the generator but doesn't regenerate the API sources.

Fixes elastic#25
karmi added a commit that referenced this issue Mar 16, 2019
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)
    }

Related: #27, #25
karmi added a commit that referenced this issue Mar 24, 2019
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)
    }

Related: #27, #25
karmi added a commit that referenced this issue Mar 25, 2019
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
karmi added a commit that referenced this issue Mar 25, 2019
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
karmi added a commit that referenced this issue Mar 25, 2019
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
(cherry picked from commit c7dbbb6)
karmi added a commit that referenced this issue Mar 25, 2019
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
(cherry picked from commit c7dbbb6)
@karmi
Copy link
Contributor

karmi commented Mar 30, 2019

This has been closed via c7dbbb6 and 6b95886 — thanks for the report again!

@karmi karmi closed this as completed Mar 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants