Skip to content

Commit

Permalink
add ability to parse float and bool to string with queryStruct()
Browse files Browse the repository at this point in the history
  • Loading branch information
davyzhang committed Jan 9, 2017
1 parent 72b36f5 commit b221adc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 16 additions & 1 deletion gorequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,22 @@ func (s *SuperAgent) queryStruct(content interface{}) *SuperAgent {
} else {
for k, v := range val {
k = strings.ToLower(k)
s.QueryData.Add(k, v.(string))
var queryVal string
switch t := v.(type) {
case string:
queryVal = t
case float64:
queryVal = strconv.FormatFloat(t, 'f', -1, 64)
case time.Time:
queryVal = t.Format(time.RFC3339)
default:
j, err := json.Marshal(v)
if err != nil {
continue
}
queryVal = string(j)
}
s.QueryData.Add(k, queryVal)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions gorequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,8 @@ func TestQueryFunc(t *testing.T) {
case case4_send_map:
checkQuery(t, v, "query1", "test1")
checkQuery(t, v, "query2", "test2")
checkQuery(t, v, "query3", "3.1415926")
checkQuery(t, v, "query4", "true")
}
}))
defer ts.Close()
Expand Down Expand Up @@ -1350,6 +1352,8 @@ func TestQueryFunc(t *testing.T) {
Query(map[string]interface{}{
"query1": "test1",
"query2": "test2",
"query3": 3.1415926,
"query4": true,
}).
End()
}
Expand Down

0 comments on commit b221adc

Please sign in to comment.