Skip to content

Commit

Permalink
#typed_http_builder: Add types to http query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Dec 20, 2021
1 parent da6d084 commit fb287d0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ str, err := pgo.StrReplace([]string{"fox", "dog"}, []string{"cat", "elephant"},

#### HTTPBuildQuery

Bulding a http query string:
Building a http query string:

```go
queryStr := pgo.HTTPBuildQuery(map[string]interface{}{
"foo": "bar",
"bar": "baz",
"s": []string{"1", "foo", "2", "bar", "3", "baz"},
}) // bar=baz&foo=bar&s=1&s=foo&s=2&s=bar&s=3&s=baz
"foo": "bar",
"bar": "baz",
"s": []string{"1", "foo", "2", "bar", "3", "baz"},
"num": 123,
"bigNum": int64(1238873737737737373),
"amount": 623.937,
"isActive": true,
}) // amount=623.937&bar=baz&bigNum=1238873737737737373&foo=bar&isActive=true&num=123&s=1&s=foo&s=2&s=bar&s=3&s=baz
```

#### StripTags
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
8 changes: 8 additions & 0 deletions str.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pgo

import (
"errors"
"fmt"
"net/url"
"strings"
)
Expand Down Expand Up @@ -122,7 +123,14 @@ func HTTPBuildQuery(pairs map[string]interface{}) string {
break
case []string:
q[k] = v.([]string)
case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64:
q.Add(k, fmt.Sprintf("%d", v))
case float32, float64:
q.Add(k, fmt.Sprintf("%v", v))
case bool:
q.Add(k, fmt.Sprintf("%t", v))
}
}

return q.Encode()
}
12 changes: 8 additions & 4 deletions str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ func TestStrReplaceErrs(t *testing.T) {

func TestHTTPBuildQuery(t *testing.T) {
queryStr := pgo.HTTPBuildQuery(map[string]interface{}{
"foo": "bar",
"bar": "baz",
"s": []string{"1", "foo", "2", "bar", "3", "baz"},
"foo": "bar",
"bar": "baz",
"s": []string{"1", "foo", "2", "bar", "3", "baz"},
"num": 123,
"bigNum": int64(1238873737737737373),
"amount": 623.937,
"isActive": true,
})

want := "bar=baz&foo=bar&s=1&s=foo&s=2&s=bar&s=3&s=baz"
want := "amount=623.937&bar=baz&bigNum=1238873737737737373&foo=bar&isActive=true&num=123&s=1&s=foo&s=2&s=bar&s=3&s=baz"
assert.Equal(t, queryStr, want, "want %s, got %s", queryStr, want)

queryStr2 := pgo.HTTPBuildQuery(map[string]interface{}{})
Expand Down

0 comments on commit fb287d0

Please sign in to comment.