/
response.go
65 lines (55 loc) · 1.79 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2018 Adam S Levy. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package jsonrpc2
import (
"encoding/json"
)
// Version is the valid version string for the "jsonrpc" field required in all
// JSON RPC 2.0 objects.
const Version = "2.0"
// Response represents a JSON-RPC 2.0 Response object.
type Response struct {
JSONRPC string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
ID interface{} `json:"id"`
}
// response hides the json.Marshaler interface that Response implements.
// Response.MarshalJSON uses this type to avoid infinite recursion.
type response Response
// MarshalJSON outputs a JSON RPC Response object with the "jsonrpc" field
// populated to Version ("2.0").
func (r Response) MarshalJSON() ([]byte, error) {
r.JSONRPC = Version
return json.Marshal(response(r))
}
// IsValid returns true if JSONRPC is equal to the Version ("2.0") and one of
// Response.Result or Response.Error is not nil, but not both.
func (r Response) IsValid() bool {
return r.JSONRPC == Version &&
(r.Result != nil || r.Error != nil) &&
(r.Result == nil || r.Error == nil)
}
// String returns a string of the JSON with "<-- " prefixed to represent a
// Response object.
func (r Response) String() string {
b, _ := json.Marshal(r)
return "<-- " + string(b)
}
// BatchResponse is a type that implements fmt.Stringer for a slice of
// Responses.
type BatchResponse []Response
// String returns a string of the JSON array with "<-- " prefixed to represent
// a BatchResponse object.
func (br BatchResponse) String() string {
s := "<-- [\n"
for i, res := range br {
s += " " + res.String()[4:]
if i < len(br)-1 {
s += ","
}
s += "\n"
}
return s + "]"
}