-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
} |