8000 Add FieldsFromRequest by jfbus · Pull Request #4 · cocoonspace/dynjson · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add FieldsFromRequest #4

New issue
8000

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

Merged
merged 5 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ GET https://api.example.com/v1/foos/1?select=foo
{"foo":1}
```

dynjson mimicks the original struct using the original types and json tags.
The field order is the same as the select parameters.

## Installation

go get github.com/cocoonspace/dynjson
Expand All @@ -33,13 +36,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 +77,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 +99,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