Skip to content

Commit

Permalink
Fix Range header formatting (#44)
Browse files Browse the repository at this point in the history
Previously there was an edge case in Range header formatting that cased
`ListRange` structs that set both `Field` and `Descending` but not `Max`
to be rendered without a semicolon separator before parameters:

    version ..order=desc

After this commit the correct header is rendered for all combinations of
struct values:

    version ..; order=desc
  • Loading branch information
vanstee authored and cyberdelia committed Apr 25, 2017
1 parent bd08bb1 commit 6925dfc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *Schema) Generate() ([]byte, error) {
// TODO: Check if we need time.
templates.ExecuteTemplate(&buf, "imports.tmpl", []string{
"encoding/json", "fmt", "io", "reflect", "net/http", "runtime",
"time", "bytes", "context",
"time", "bytes", "context", "strings",
"github.com/google/go-querystring/query",
})
templates.ExecuteTemplate(&buf, "service.tmpl", struct {
Expand Down
13 changes: 7 additions & 6 deletions templates/service.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ func (lr *ListRange) SetHeader(req *http.Request) {
hdrval += lr.Field + " "
}
hdrval += lr.FirstID + ".." + lr.LastID

params := make([]string, 0, 2)
if lr.Max != 0 {
hdrval += fmt.Sprintf("; max=%d", lr.Max)
if lr.Descending {
hdrval += "; "
}
params = append(params, fmt.Sprintf("max=%d", lr.Max))
}

if lr.Descending {
hdrval += "order=desc"
params = append(params, "order=desc")
}
if len(params) > 0 {
hdrval += fmt.Sprintf("; %s", strings.Join(params, ","))
}

req.Header.Set("Range", hdrval)
Expand Down
13 changes: 7 additions & 6 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ func (lr *ListRange) SetHeader(req *http.Request) {
hdrval += lr.Field + " "
}
hdrval += lr.FirstID + ".." + lr.LastID
params := make([]string, 0, 2)
if lr.Max != 0 {
hdrval += fmt.Sprintf("; max=%d", lr.Max)
if lr.Descending {
hdrval += "; "
}
params = append(params, fmt.Sprintf("max=%d", lr.Max))
}
if lr.Descending {
hdrval += "order=desc"
params = append(params, "order=desc")
}
if len(params) > 0 {
hdrval += fmt.Sprintf("; %s", strings.Join(params, ","))
}
req.Header.Set("Range", hdrval)
Expand Down

0 comments on commit 6925dfc

Please sign in to comment.