Skip to content

Commit 116f4e9

Browse files
committed
As requested, changed JSON to upper case
Updated params so that Params.Bind() does not blindly bind json data Added Params.BindJSON() to provide JSON bind functionality
1 parent 0b90a83 commit 116f4e9

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

binder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package revel
66

77
import (
8+
"encoding/json"
89
"fmt"
910
"io"
1011
"io/ioutil"
@@ -14,7 +15,6 @@ import (
1415
"strconv"
1516
"strings"
1617
"time"
17-
"encoding/json"
1818
)
1919

2020
// A Binder translates between string parameters and Go data structures.
@@ -293,9 +293,9 @@ func unbindSlice(output map[string]string, name string, val interface{}) {
293293
func bindStruct(params *Params, name string, typ reflect.Type) reflect.Value {
294294
resultPointer := reflect.New(typ)
295295
result := resultPointer.Elem()
296-
if params.Json!=nil {
296+
if params.JSON != nil {
297297
// Try to inject the response as a json into the created result
298-
if err := json.Unmarshal(params.Json, resultPointer.Interface()); err != nil {
298+
if err := json.Unmarshal(params.JSON, resultPointer.Interface()); err != nil {
299299
WARN.Println("W: bindStruct: Unable to unmarshal request:", name, err)
300300
}
301301
return result
@@ -422,9 +422,9 @@ func bindMap(params *Params, name string, typ reflect.Type) reflect.Value {
422422
result = resultPtr.Elem()
423423
)
424424
result.Set(reflect.MakeMap(typ))
425-
if params.Json!=nil {
425+
if params.JSON != nil {
426426
// Try to inject the response as a json into the created result
427-
if err := json.Unmarshal(params.Json, resultPtr.Interface()); err != nil {
427+
if err := json.Unmarshal(params.JSON, resultPtr.Interface()); err != nil {
428428
WARN.Println("W: bindMap: Unable to unmarshal request:", name, err)
429429
}
430430
return result

binder_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,24 @@ func TestJsonBinder(t *testing.T) {
170170
// create a structure to be populated
171171
{
172172
d, _ := json.Marshal(map[string]int{"a": 1})
173-
params := &Params{Json: d}
173+
params := &Params{JSON: d}
174174
foo := struct{ A int }{}
175175
ParseParams(params, NewRequest(getMultipartRequest()))
176176
actual := Bind(params, "test", reflect.TypeOf(foo))
177177
valEq(t, "TestJsonBinder", reflect.ValueOf(actual.Interface().(struct{ A int }).A), reflect.ValueOf(1))
178178
}
179179
{
180-
d, _ := json.Marshal(map[string]interface{}{"a": map[string]int{"b":45}})
181-
params := &Params{Json: d}
180+
d, _ := json.Marshal(map[string]interface{}{"a": map[string]int{"b": 45}})
181+
params := &Params{JSON: d}
182182
testMap := map[string]interface{}{}
183183
actual := Bind(params, "test", reflect.TypeOf(testMap)).Interface().(map[string]interface{})
184-
if actual["a"].(map[string]interface{})["b"].(float64)!=45 {
185-
t.Errorf("Failed to fetch map value %#v",actual["a"])
184+
if actual["a"].(map[string]interface{})["b"].(float64) != 45 {
185+
t.Errorf("Failed to fetch map value %#v", actual["a"])
186186
}
187187
// Check to see if a named map works
188188
actualb := Bind(params, "test", reflect.TypeOf(map[string]map[string]float64{})).Interface().(map[string]map[string]float64)
189-
if actualb["a"]["b"]!=45 {
190-
t.Errorf("Failed to fetch map value %#v",actual["a"])
189+
if actualb["a"]["b"] != 45 {
190+
t.Errorf("Failed to fetch map value %#v", actual["a"])
191191
}
192192

193193
}

params.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
package revel
66

77
import (
8+
"encoding/json"
9+
"io/ioutil"
810
"mime/multipart"
911
"net/url"
1012
"os"
1113
"reflect"
12-
"io/ioutil"
14+
"errors"
1315
)
1416

1517
// Params provides a unified view of the request params.
@@ -32,7 +34,7 @@ type Params struct {
3234

3335
Files map[string][]*multipart.FileHeader // Files uploaded in a multipart form
3436
tmpFiles []*os.File // Temp files used during the request.
35-
Json []byte // JSON data from request body
37+
JSON []byte // JSON data from request body
3638
}
3739

3840
// ParseParams parses the `http.Request` params into `revel.Controller.Params`
@@ -61,12 +63,12 @@ func ParseParams(params *Params, req *Request) {
6163
case "application/json":
6264
fallthrough
6365
case "text/json":
64-
if req.Body!=nil {
65-
if content, err := ioutil.ReadAll(req.Body);err==nil{
66+
if req.Body != nil {
67+
if content, err := ioutil.ReadAll(req.Body); err == nil {
6668
// We wont bind it until we determine what we are binding too
67-
params.Json = content
69+
params.JSON = content
6870
} else {
69-
ERROR.Println("Failed to ready request body bytes",err)
71+
ERROR.Println("Failed to ready request body bytes", err)
7072
}
7173
} else {
7274
INFO.Println("Json post received with empty body")
@@ -88,7 +90,29 @@ func (p *Params) Bind(dest interface{}, name string) {
8890
if !value.CanSet() {
8991
panic("revel/params: non-settable variable passed to Bind: " + name)
9092
}
93+
94+
// Remove the json from the Params, this will stop the binder from attempting
95+
// to use the json data to populate the destination interface. We do not want
96+
// to do this on a named bind directly against the param, it is ok to happen when
97+
// the action is invoked.
98+
jsonData := p.JSON
99+
p.JSON = nil
91100
value.Set(Bind(p, name, value.Type()))
101+
p.JSON = jsonData
102+
}
103+
104+
// Bind binds the JSON data to the dest.
105+
func (p *Params) BindJSON(dest interface{}) error {
106+
value := reflect.ValueOf(dest)
107+
if value.Kind() != reflect.Ptr {
108+
WARN.Println("BindJSON not a pointer")
109+
return errors.New("BindJSON not a pointer")
110+
}
111+
if err := json.Unmarshal(p.JSON, dest); err != nil {
112+
WARN.Println("W: bindMap: Unable to unmarshal request:", err)
113+
return err
114+
}
115+
return nil
92116
}
93117

94118
// calcValues returns a unified view of the component param maps.

validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (v *Validation) Check(obj interface{}, checks ...Validator) *ValidationResu
187187
func ValidationFilter(c *Controller, fc []Filter) {
188188
// If json request, we shall assume json response is intended,
189189
// as such no validation cookies should be tied response
190-
if c.Params != nil && c.Params.Json != nil {
190+
if c.Params != nil && c.Params.JSON != nil {
191191
c.Validation = &Validation{}
192192
fc[0](c, fc[1:])
193193
} else {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy