Skip to content

Commit

Permalink
fix: marshal,unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
樊金东 authored and fanjindong committed May 8, 2023
1 parent 7d2dcaf commit c0e82c5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 54 deletions.
18 changes: 9 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ func (s *Client) Head(url string, opts ...ReqOption) (*Response, error) {
var unmarshal = json.Unmarshal
var marshal = json.Marshal

//SetUnmarshal Set custom Unmarshal functions, default is json.Unmarshal
func SetUnmarshal(f func(data []byte, v interface{}) error) {
unmarshal = f
}

//SetMarshal Set custom Marshal functions, default is json.Marshal
func SetMarshal(f func(v interface{}) ([]byte, error)) {
marshal = f
}
////SetUnmarshal Set custom Unmarshal functions, default is json.Unmarshal
//func SetUnmarshal(f func(data []byte, v interface{}) error) {
// unmarshal = f
//}
//
////SetMarshal Set custom Marshal functions, default is json.Marshal
//func SetMarshal(f func(v interface{}) ([]byte, error)) {
// marshal = f
//}
82 changes: 41 additions & 41 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package requests

import (
"encoding/json"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -109,43 +108,44 @@ func BenchmarkGetRequest(b *testing.B) {
}
}

func TestSetUnmarshalAndSetMarshal(t *testing.T) {
type args struct {
v interface{}
unmarshal func(data []byte, v interface{}) error
marshal func(v interface{}) ([]byte, error)
}
tests := []struct {
name string
args args
}{
{args: args{v: map[string]interface{}{"a": float64(1), "b": "x"},
unmarshal: func(data []byte, v interface{}) error {
data = data[:len(data)-1]
return json.Unmarshal(data, v)
},
marshal: func(v interface{}) ([]byte, error) {
bytes, err := json.Marshal(v)
bytes = append(bytes, 's')
return bytes, err
}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetMarshal(tt.args.marshal)
SetUnmarshal(tt.args.unmarshal)
gotBytes, err := marshal(tt.args.v)
if err != nil {
panic(err)
}
t.Log(gotBytes, string(gotBytes))
got := make(map[string]interface{})
if err := unmarshal(gotBytes, &got); err != nil {
panic(err)
}
if !reflect.DeepEqual(got, tt.args.v) {
t.Errorf("SetUnmarshalAndSetMarshal() got = %v, want %v", got, tt.args.v)
}
})
}
}
//
//func TestSetUnmarshalAndSetMarshal(t *testing.T) {
// type args struct {
// v interface{}
// unmarshal func(data []byte, v interface{}) error
// marshal func(v interface{}) ([]byte, error)
// }
// tests := []struct {
// name string
// args args
// }{
// {args: args{v: map[string]interface{}{"a": float64(1), "b": "x"},
// unmarshal: func(data []byte, v interface{}) error {
// data = data[:len(data)-1]
// return json.Unmarshal(data, v)
// },
// marshal: func(v interface{}) ([]byte, error) {
// bytes, err := json.Marshal(v)
// bytes = append(bytes, 's')
// return bytes, err
// }}},
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// SetMarshal(tt.args.marshal)
// SetUnmarshal(tt.args.unmarshal)
// gotBytes, err := marshal(tt.args.v)
// if err != nil {
// panic(err)
// }
// t.Log(gotBytes, string(gotBytes))
// got := make(map[string]interface{})
// if err := unmarshal(gotBytes, &got); err != nil {
// panic(err)
// }
// if !reflect.DeepEqual(got, tt.args.v) {
// t.Errorf("SetUnmarshalAndSetMarshal() got = %v, want %v", got, tt.args.v)
// }
// })
// }
//}
7 changes: 3 additions & 4 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package requests

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -22,7 +21,7 @@ func getHandler(w http.ResponseWriter, r *http.Request) {
for k, v := range query {
params[k] = v[0]
}
body, _ := json.Marshal(params)
body, _ := marshal(params)
w.Write(body)
}

Expand All @@ -39,7 +38,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
for k, v := range r.PostForm {
data[k] = v[0]
}
body, _ := json.Marshal(data)
body, _ := marshal(data)
w.Write(body)
return
default:
Expand All @@ -60,7 +59,7 @@ func timeoutHandler(w http.ResponseWriter, r *http.Request) {
}

func headerHandler(w http.ResponseWriter, r *http.Request) {
bytes, _ := json.Marshal(r.Header)
bytes, _ := marshal(r.Header)
w.Write(bytes)
}

Expand Down

0 comments on commit c0e82c5

Please sign in to comment.