Skip to content

Commit

Permalink
Merge c0da5a9 into 9b5cd50
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbus committed Jul 24, 2020
2 parents 9b5cd50 + c0da5a9 commit da8301f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,37 @@ type APIResult struct {
f := dynjson.NewFormatter()

res := &APIResult{Foo:1, Bar:"bar"}
o, err := f.Format(res, []string{"foo"}, nil)
o, err := f.Format(res, dynjson.FieldsFromRequest(r))
if err != nil {
// handle error
}
err := json.Marshal(w, o) // {"foo": 1}
```

With struct fields :


```go
type APIResult struct {
Foo int `json:"foo"`
Bar APIIncluded `json:"bar"`
}

type APIIncluded struct {
BarFoo int `json:"barfoo"`
BarBar string `json:"barbar"`
}

f := dynjson.NewFormatter()

res := &APIResult{Foo: 1, Bar: APIIncluded{BarFoo:1, BarBar: "bar"}}
o, err := f.Format(res, []string{"foo", "bar.barfoo"})
if err != nil {
// handle error
}
err := json.Marshal(w, o) // {"foo": 1, "bar":{"barfoo": 1}}
```

With slices:

```go
Expand All @@ -50,8 +74,8 @@ type APIResult struct {

f := dynjson.NewFormatter()

res := []APIResult{{Foo:1, Bar:"bar"}}
o, err := f.Format(res, []string{"foo"}, nil)
res := []APIResult{{Foo: 1, Bar: "bar"}}
o, err := f.Format(res, []string{"foo"})
if err != nil {
// handle error
}
Expand All @@ -72,8 +96,8 @@ type APIItem struct {

f := dynjson.NewFormatter()

res := &APIResult{Foo:1, Bar:[]APIItem{{BarFoo:1, BarBar: "bar"}}}
o, err := f.Format(res, []string{"foo", "bar.barfoo"}, nil)
res := &APIResult{Foo: 1, Bar: []APIItem{{BarFoo: 1, BarBar: "bar"}}}
o, err := f.Format(res, []string{"foo", "bar.barfoo"})
if err != nil {
// handle error
}
Expand Down
30 changes: 30 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dynjson

import (
"net/http"
"net/url"
"strings"
)

type Option int

const (
OptionMultipleFields Option = iota
OptionCommaList
)

// FieldsFromRequest returns the list of fields requested from a http.Request
// Without opt or with OptionMultipleFields, the expected format is:
// http://api.example.com/endpoint?select=foo&select=bar
// With OptionCommaList, the expected format is:
// http://api.example.com/endpoint?select=foo,bar
func FieldsFromRequest(r *http.Request, opt ...Option) []string {
vals, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
return nil
}
if len(opt) == 1 && opt[0] == OptionCommaList && len(vals["select"]) > 0 {
return strings.Split(vals["select"][0], ",")
}
return vals["select"]
}
49 changes: 49 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dynjson

import (
"net/http"
"testing"
)

func TestFieldsFromRequest(t *testing.T) {
{
r, err := http.NewRequest(http.MethodGet, "http://api.example.com/endpoint?select=foo&select=bar", nil)
if err != nil {
t.Error("Should not have returned", err)
}
fields := FieldsFromRequest(r)
if len(fields) != 2 {
t.Error("2 fields were expected")
}
if fields[0] != "foo" || fields[1] != "bar" {
t.Errorf("Expected [foo bar] but got %v", fields)
}
}
{
r, err := http.NewRequest(http.MethodGet, "http://api.example.com/endpoint?select=foo&select=bar", nil)
if err != nil {
t.Error("Should not have returned", err)
}
fields := FieldsFromRequest(r, OptionMultipleFields)
if len(fields) != 2 {
t.Error("2 fields were expected")
}
if fields[0] != "foo" || fields[1] != "bar" {
t.Errorf("Expected [foo bar] but got %v", fields)
}
}
{
r, err := http.NewRequest(http.MethodGet, "http://api.example.com/endpoint?select=foo,bar", nil)
if err != nil {
t.Error("Should not have returned", err)
}
fields := FieldsFromRequest(r, OptionCommaList)
if len(fields) != 2 {
t.Error("2 fields were expected")
}
if fields[0] != "foo" || fields[1] != "bar" {
t.Errorf("Expected [foo bar] but got %v", fields)
}
}

}

0 comments on commit da8301f

Please sign in to comment.