Skip to content
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
10 changes: 7 additions & 3 deletions json/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
type clientRequest struct {
// A String containing the name of the method to be invoked.
Method string `json:"method"`
// Object to pass as request parameter to the method.
Params [1]interface{} `json:"params"`
// Array of objects to pass as request parameters to the method.
Params []interface{} `json:"params"`
// The request id. This can be of any type. It is used to match the
// response with the request that it is replying to.
Id uint64 `json:"id"`
Expand All @@ -45,9 +45,13 @@ func EncodeClientRequest(method string, args interface{}) ([]byte, error) {
log.Fatal(err)
}

params := []interface{}{}
if args != nil {
params = []interface{}{args}
}
c := &clientRequest{
Method: method,
Params: [1]interface{}{args},
Params: params,
Id: val.Uint64(),
}
return json.Marshal(c)
Expand Down
30 changes: 30 additions & 0 deletions json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,33 @@ func TestServiceBeforeAfter(t *testing.T) {
t.Errorf("Expected after in context to be 'After is true', got %s", afterValue)
}
}

func TestEncodeClientRequestNilArgs(t *testing.T) {
buf, err := EncodeClientRequest("Service1.Ping", nil)
if err != nil {
t.Fatal(err)
}
var got map[string]interface{}
if err := json.Unmarshal(buf, &got); err != nil {
t.Fatal(err)
}
params, ok := got["params"].([]interface{})
if !ok {
t.Fatalf("params want array, got %T %v", got["params"], got["params"])
}
if len(params) != 0 {
t.Fatalf("nil args should encode as empty params array, got %#v", params)
}

buf, err = EncodeClientRequest("Service1.Multiply", &Service1Request{A: 4, B: 2})
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(buf, &got); err != nil {
t.Fatal(err)
}
params, ok = got["params"].([]interface{})
if !ok || len(params) != 1 {
t.Fatalf("non-nil args should encode as a one-element array, got %#v", got["params"])
}
}
10 changes: 7 additions & 3 deletions v2/json/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
type clientRequest struct {
// A String containing the name of the method to be invoked.
Method string `json:"method"`
// Object to pass as request parameter to the method.
Params [1]interface{} `json:"params"`
// Array of objects to pass as request parameters to the method.
Params []interface{} `json:"params"`
// The request id. This can be of any type. It is used to match the
// response with the request that it is replying to.
Id uint64 `json:"id"`
Expand All @@ -44,9 +44,13 @@ func EncodeClientRequest(method string, args interface{}) ([]byte, error) {
log.Fatal(err)
}

params := []interface{}{}
if args != nil {
params = []interface{}{args}
}
c := &clientRequest{
Method: method,
Params: [1]interface{}{args},
Params: params,
Id: val.Uint64(),
}
return json.Marshal(c)
Expand Down
30 changes: 30 additions & 0 deletions v2/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,33 @@ func TestClientNullResult(t *testing.T) {
t.Fatalf("Unexpected error: %s", err)
}
}

func TestEncodeClientRequestNilArgs(t *testing.T) {
buf, err := EncodeClientRequest("Service1.Ping", nil)
if err != nil {
t.Fatal(err)
}
var got map[string]interface{}
if err := json.Unmarshal(buf, &got); err != nil {
t.Fatal(err)
}
params, ok := got["params"].([]interface{})
if !ok {
t.Fatalf("params want array, got %T %v", got["params"], got["params"])
}
if len(params) != 0 {
t.Fatalf("nil args should encode as empty params array, got %#v", params)
}

buf, err = EncodeClientRequest("Service1.Multiply", &Service1Request{A: 4, B: 2})
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(buf, &got); err != nil {
t.Fatal(err)
}
params, ok = got["params"].([]interface{})
if !ok || len(params) != 1 {
t.Fatalf("non-nil args should encode as a one-element array, got %#v", got["params"])
}
}
2 changes: 1 addition & 1 deletion v2/json2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type clientRequest struct {
Method string `json:"method"`

// Object to pass as request parameter to the method.
Params interface{} `json:"params"`
Params interface{} `json:"params,omitempty"`

// The request id. This can be of any type. It is used to match the
// response with the request that it is replying to.
Expand Down
25 changes: 25 additions & 0 deletions v2/json2/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,28 @@ func TestDecodeNullResult(t *testing.T) {
t.Error("Expected result to be nil, but got:", result)
}
}

func TestEncodeClientRequestNilArgs(t *testing.T) {
buf, err := EncodeClientRequest("Service1.Ping", nil)
if err != nil {
t.Fatal(err)
}
var got map[string]interface{}
if err := json.Unmarshal(buf, &got); err != nil {
t.Fatal(err)
}
if _, ok := got["params"]; ok {
t.Fatalf("nil args should omit params, got %#v", got["params"])
}

buf, err = EncodeClientRequest("Service1.Multiply", &Service1Request{A: 4, B: 2})
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(buf, &got); err != nil {
t.Fatal(err)
}
if got["params"] == nil {
t.Fatal("non-nil args should include params")
}
}
Loading