Skip to content
Merged
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
11 changes: 11 additions & 0 deletions transport/http/jsonrpc/request_response_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ func (id *RequestID) UnmarshalJSON(b []byte) error {
return nil
}

func (id *RequestID) MarshalJSON() ([]byte, error) {
if id.intError == nil {
return json.Marshal(id.intValue)
} else if id.floatError == nil {
return json.Marshal(id.floatValue)
} else {
return json.Marshal(id.stringValue)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My sense is this would be better expressed without the short-circuit logic, as

switch {
case id.intError == nil && id.floatError != nil:
    return json.Marshal(id.intValue)
case id.intError != nil && id.floatError == nil:
    return json.Marshal(id.floatValue)
default:
    return json.Marshal(id.stringValue)
}

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think so, intError and floatError can both be nil

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if intErr == nil and floatErr == nil, the final default case is triggered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For integer value, intErr and floatErr are nil and the default case will return empty string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, OK, I understand. Then the original implementation is best.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!


// Int returns the ID as an integer value.
// An error is returned if the ID can't be treated as an int.
func (id *RequestID) Int() (int, error) {
Expand All @@ -59,6 +69,7 @@ type Response struct {
JSONRPC string `json:"jsonrpc"`
Result json.RawMessage `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
ID *RequestID `json:"id"`
}

const (
Expand Down
27 changes: 27 additions & 0 deletions transport/http/jsonrpc/request_response_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,30 @@ func TestCanUnmarshalNullID(t *testing.T) {
t.Fatalf("Expected ID to be nil, got %+v.\n", r.ID)
}
}

func TestCanMarshalID(t *testing.T) {
cases := []struct {
JSON string
expType string
expValue interface{}
}{
{`12345`, "int", 12345},
{`12345.6`, "float", 12345.6},
{`"stringaling"`, "string", "stringaling"},
{`null`, "null", nil},
}

for _, c := range cases {
req := jsonrpc.Request{}
JSON := fmt.Sprintf(`{"jsonrpc":"2.0","id":%s}`, c.JSON)
json.Unmarshal([]byte(JSON), &req)
resp := jsonrpc.Response{ID: req.ID, JSONRPC: req.JSONRPC}

want := JSON
bol, _ := json.Marshal(resp)
got := string(bol)
if got != want {
t.Fatalf("'%s': want %s, got %s.", c.expType, want, got)
}
}
}
1 change: 1 addition & 0 deletions transport/http/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

res := Response{
ID: req.ID,
JSONRPC: Version,
}

Expand Down