Skip to content
New issue

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

Add support for JSON encoding nested struct #54

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/google/go-querystring
module github.com/schmorrison/go-querystring

go 1.10

Expand Down
23 changes: 21 additions & 2 deletions query/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package query

import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"reflect"
Expand Down Expand Up @@ -119,6 +120,14 @@ type Encoder interface {
//
// "user[name]=acme&user[addr][postcode]=1234&user[addr][city]=SFO"
//
// Structs can be encoded as JSON by including the "json" option in that fields
// tag. The entire struct is then passed to encoding/json.Marshal where those
// tag signatures apply.
//
// // Encoding a struct as JSON
// Field A `url: "myName,json"`
// // Result: myName={"someField": "cat", "numField": 1}
//
// All other values are encoded using their default string representation.
//
// Multiple fields that encode to the same URL parameter name will be included
Expand Down Expand Up @@ -252,8 +261,18 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error {
}

if sv.Kind() == reflect.Struct {
if err := reflectValue(values, sv, name); err != nil {
return err
if opts.Contains("json") {
var b []byte
b, err := json.Marshal(sv.Interface())
if err != nil {
return err
}

values.Add(name, valueString(reflect.ValueOf(string(b)), opts, sf))
} else {
if err := reflectValue(values, sv, name); err != nil {
return err
}
}
continue
}
Expand Down
69 changes: 69 additions & 0 deletions query/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,75 @@ func TestValues_EmbeddedStructs(t *testing.T) {
}
}

func TestValues_StructsAsJSON(t *testing.T) {
type Nested struct {
L bool `json:"l"`
M bool `json:"m"`
}
type Inner struct {
A string `json:"a"`
B int `json:"b"`
T Nested `json:"t"`
}

type Outer struct {
S Inner `url:"str,json"`
P *Inner `url:"ptr,json,omitempty"`
}

tests := []struct {
input interface{}
want url.Values
}{
{
Outer{
S: Inner{
A: "abc",
B: 5,
T: Nested{
L: true,
M: false,
},
},
P: nil,
},
url.Values{
"str": {`{"a":"abc","b":5,"t":{"l":true,"m":false}}`},
},
},
{
Outer{
P: &Inner{
A: "def",
B: 22,
T: Nested{
L: true,
M: true,
},
},
},
url.Values{
"str": {`{"a":"","b":0,"t":{"l":false,"m":false}}`},
"ptr": {`{"a":"def","b":22,"t":{"l":true,"m":true}}`},
},
},
{
Outer{},
url.Values{
"str": {`{"a":"","b":0,"t":{"l":false,"m":false}}`},
},
},
{
nil,
url.Values{},
},
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
}
}

func TestValues_InvalidInput(t *testing.T) {
_, err := Values("")
if err == nil {
Expand Down