Skip to content

Commit

Permalink
#76 adding support for registering external JSON library
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jul 10, 2017
1 parent 1eda6e4 commit e621018
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
14 changes: 14 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ type Client struct {
RetryWaitTime time.Duration
RetryMaxWaitTime time.Duration
RetryConditions []RetryConditionFunc
JSONMarshal func(v interface{}) ([]byte, error)
JSONUnmarshal func(data []byte, v interface{}) error

httpClient *http.Client
transport *http.Transport
Expand Down Expand Up @@ -811,6 +813,7 @@ func IsXMLType(ct string) bool {
}

// Unmarshal content into object from JSON or XML
// Deprecated: kept for backward compatibility
func Unmarshal(ct string, b []byte, d interface{}) (err error) {
if IsJSONType(ct) {
err = json.Unmarshal(b, d)
Expand All @@ -821,6 +824,17 @@ func Unmarshal(ct string, b []byte, d interface{}) (err error) {
return
}

// Unmarshalc content into object from JSON or XML
func Unmarshalc(c *Client, ct string, b []byte, d interface{}) (err error) {
if IsJSONType(ct) {
err = c.JSONUnmarshal(b, d)
} else if IsXMLType(ct) {
err = xml.Unmarshal(b, d)
}

return
}

func getLogger(w io.Writer) *log.Logger {
return log.New(w, "RESTY ", log.LstdFlags)
}
Expand Down
3 changes: 3 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package resty

import (
"crypto/tls"
"encoding/json"
"io"
"net/http"
"net/http/cookiejar"
Expand Down Expand Up @@ -36,6 +37,8 @@ func New() *Client {
RetryCount: 0,
RetryWaitTime: defaultWaitTime,
RetryMaxWaitTime: defaultMaxWaitTime,
JSONMarshal: json.Marshal,
JSONUnmarshal: json.Unmarshal,
httpClient: &http.Client{Jar: cookieJar},
transport: &http.Transport{},
}
Expand Down
7 changes: 3 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package resty

import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
Expand Down Expand Up @@ -245,7 +244,7 @@ func parseResponseBody(c *Client, res *Response) (err error) {
// Considered as Result
if res.StatusCode() > 199 && res.StatusCode() < 300 {
if res.Request.Result != nil {
err = Unmarshal(ct, res.body, res.Request.Result)
err = Unmarshalc(c, ct, res.body, res.Request.Result)
return
}
}
Expand All @@ -258,7 +257,7 @@ func parseResponseBody(c *Client, res *Response) (err error) {
}

if res.Request.Error != nil {
err = Unmarshal(ct, res.body, res.Request.Error)
err = Unmarshalc(c, ct, res.body, res.Request.Error)
}
}
}
Expand Down Expand Up @@ -356,7 +355,7 @@ func handleRequestBody(c *Client, r *Request) (err error) {
bodyBytes = []byte(s)
} else if IsJSONType(contentType) &&
(kind == reflect.Struct || kind == reflect.Map || kind == reflect.Slice) {
bodyBytes, err = json.Marshal(r.Body)
bodyBytes, err = c.JSONMarshal(r.Body)
} else if IsXMLType(contentType) && (kind == reflect.Struct) {
bodyBytes, err = xml.Marshal(r.Body)
}
Expand Down
17 changes: 17 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,23 @@ func TestSRVInvalidService(t *testing.T) {
assertEqual(t, true, strings.Contains(err.Error(), "no such host"))
}

func TestDeprecatedCodeCovergae(t *testing.T) {
var user1 User
err := Unmarshal("application/json",
[]byte(`{"username":"testuser", "password":"testpass"}`), &user1)
assertError(t, err)
assertEqual(t, "testuser", user1.Username)
assertEqual(t, "testpass", user1.Password)

var user2 User
err = Unmarshal("application/xml",
[]byte(`<?xml version="1.0" encoding="UTF-8"?><User><Username>testuser</Username><Password>testpass</Password></User>`),
&user2)
assertError(t, err)
assertEqual(t, "testuser", user1.Username)
assertEqual(t, "testpass", user1.Password)
}

func getTestDataPath() string {
pwd, _ := os.Getwd()
return pwd + "/test-data"
Expand Down

0 comments on commit e621018

Please sign in to comment.