2016-03-20 17:10:13 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2017-12-06 03:15:38 -05:00
|
|
|
"bytes"
|
2016-03-20 17:10:13 -07:00
|
|
|
"fmt"
|
2017-12-06 01:56:39 -05:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/jsonpb"
|
2016-03-20 17:10:13 -07:00
|
|
|
)
|
|
|
|
|
2017-11-30 14:29:12 -05:00
|
|
|
const (
|
|
|
|
CodeTypeOK uint32 = 0
|
|
|
|
)
|
|
|
|
|
2017-12-02 01:48:37 -05:00
|
|
|
// IsOK returns true if Code is OK.
|
|
|
|
func (r ResponseCheckTx) IsOK() bool {
|
|
|
|
return r.Code == CodeTypeOK
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:33:34 -06:00
|
|
|
// IsErr returns true if Code is something other than OK.
|
2017-11-20 20:26:37 -06:00
|
|
|
func (r ResponseCheckTx) IsErr() bool {
|
2017-11-30 14:29:12 -05:00
|
|
|
return r.Code != CodeTypeOK
|
2017-11-20 20:26:37 -06:00
|
|
|
}
|
|
|
|
|
2017-11-22 16:17:34 -06:00
|
|
|
// Error implements error interface by formatting response as string.
|
|
|
|
func (r ResponseCheckTx) Error() string {
|
|
|
|
return fmtError(r.Code, r.Log)
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:48:37 -05:00
|
|
|
// IsOK returns true if Code is OK.
|
|
|
|
func (r ResponseDeliverTx) IsOK() bool {
|
|
|
|
return r.Code == CodeTypeOK
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:33:34 -06:00
|
|
|
// IsErr returns true if Code is something other than OK.
|
2017-11-20 20:26:37 -06:00
|
|
|
func (r ResponseDeliverTx) IsErr() bool {
|
2017-11-30 14:29:12 -05:00
|
|
|
return r.Code != CodeTypeOK
|
2017-11-20 20:26:37 -06:00
|
|
|
}
|
|
|
|
|
2017-11-22 16:17:34 -06:00
|
|
|
// Error implements error interface by formatting response as string.
|
|
|
|
func (r ResponseDeliverTx) Error() string {
|
|
|
|
return fmtError(r.Code, r.Log)
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:48:37 -05:00
|
|
|
// IsOK returns true if Code is OK.
|
|
|
|
func (r ResponseCommit) IsOK() bool {
|
|
|
|
return r.Code == CodeTypeOK
|
|
|
|
}
|
|
|
|
|
2017-11-22 17:44:39 -06:00
|
|
|
// IsErr returns true if Code is something other than OK.
|
|
|
|
func (r ResponseCommit) IsErr() bool {
|
2017-11-30 14:29:12 -05:00
|
|
|
return r.Code != CodeTypeOK
|
2017-11-22 17:44:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements error interface by formatting response as string.
|
|
|
|
func (r ResponseCommit) Error() string {
|
|
|
|
return fmtError(r.Code, r.Log)
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:48:37 -05:00
|
|
|
// IsOK returns true if Code is OK.
|
|
|
|
func (r ResponseQuery) IsOK() bool {
|
|
|
|
return r.Code == CodeTypeOK
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:19:41 -06:00
|
|
|
// IsErr returns true if Code is something other than OK.
|
2017-11-30 15:31:10 -05:00
|
|
|
func (r ResponseQuery) IsErr() bool {
|
2017-11-30 14:29:12 -05:00
|
|
|
return r.Code != CodeTypeOK
|
2017-11-22 18:19:41 -06:00
|
|
|
}
|
|
|
|
|
2017-11-30 15:31:10 -05:00
|
|
|
// Error implements error interface by formatting response as string.
|
|
|
|
func (r ResponseQuery) Error() string {
|
2017-11-22 18:19:41 -06:00
|
|
|
return fmtError(r.Code, r.Log)
|
|
|
|
}
|
2017-11-30 15:31:10 -05:00
|
|
|
|
|
|
|
func fmtError(code uint32, log string) string {
|
|
|
|
return fmt.Sprintf("Error code (%d): %s", code, log)
|
|
|
|
}
|
2017-12-06 01:56:39 -05:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// override JSON marshalling so we dont emit defaults (ie. disable omitempty)
|
2017-12-06 03:15:38 -05:00
|
|
|
// note we need Unmarshal functions too because protobuf had the bright idea
|
|
|
|
// to marshal int64->string. cool. cool, cool, cool: https://developers.google.com/protocol-buffers/docs/proto3#json
|
|
|
|
|
|
|
|
var (
|
|
|
|
jsonpbMarshaller = jsonpb.Marshaler{
|
|
|
|
EnumsAsInts: true,
|
|
|
|
EmitDefaults: true,
|
|
|
|
}
|
|
|
|
jsonpbUnmarshaller = jsonpb.Unmarshaler{}
|
|
|
|
)
|
2017-12-06 01:56:39 -05:00
|
|
|
|
|
|
|
func (r *ResponseSetOption) MarshalJSON() ([]byte, error) {
|
2017-12-06 03:15:38 -05:00
|
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
2017-12-06 01:56:39 -05:00
|
|
|
return []byte(s), err
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:15:38 -05:00
|
|
|
func (r *ResponseSetOption) UnmarshalJSON(b []byte) error {
|
|
|
|
reader := bytes.NewBuffer(b)
|
|
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:56:39 -05:00
|
|
|
func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
|
2017-12-06 03:15:38 -05:00
|
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
2017-12-06 01:56:39 -05:00
|
|
|
return []byte(s), err
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:15:38 -05:00
|
|
|
func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error {
|
|
|
|
reader := bytes.NewBuffer(b)
|
|
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:56:39 -05:00
|
|
|
func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) {
|
2017-12-06 03:15:38 -05:00
|
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
2017-12-06 01:56:39 -05:00
|
|
|
return []byte(s), err
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:15:38 -05:00
|
|
|
func (r *ResponseDeliverTx) UnmarshalJSON(b []byte) error {
|
|
|
|
reader := bytes.NewBuffer(b)
|
|
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:56:39 -05:00
|
|
|
func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
|
2017-12-06 03:15:38 -05:00
|
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
2017-12-06 01:56:39 -05:00
|
|
|
return []byte(s), err
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:15:38 -05:00
|
|
|
func (r *ResponseQuery) UnmarshalJSON(b []byte) error {
|
|
|
|
reader := bytes.NewBuffer(b)
|
|
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:56:39 -05:00
|
|
|
func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
|
2017-12-06 03:15:38 -05:00
|
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
2017-12-06 01:56:39 -05:00
|
|
|
return []byte(s), err
|
|
|
|
}
|
2017-12-06 03:15:38 -05:00
|
|
|
|
|
|
|
func (r *ResponseCommit) UnmarshalJSON(b []byte) error {
|
|
|
|
reader := bytes.NewBuffer(b)
|
|
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
|
|
}
|