package types
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/gogo/protobuf/jsonpb"
|
|
)
|
|
|
|
const (
|
|
CodeTypeOK uint32 = 0
|
|
)
|
|
|
|
// IsOK returns true if Code is OK.
|
|
func (r ResponseCheckTx) IsOK() bool {
|
|
return r.Code == CodeTypeOK
|
|
}
|
|
|
|
// IsErr returns true if Code is something other than OK.
|
|
func (r ResponseCheckTx) IsErr() bool {
|
|
return r.Code != CodeTypeOK
|
|
}
|
|
|
|
// Error implements error interface by formatting response as string.
|
|
func (r ResponseCheckTx) Error() string {
|
|
return fmtError(r.Code, r.Log)
|
|
}
|
|
|
|
// IsOK returns true if Code is OK.
|
|
func (r ResponseDeliverTx) IsOK() bool {
|
|
return r.Code == CodeTypeOK
|
|
}
|
|
|
|
// IsErr returns true if Code is something other than OK.
|
|
func (r ResponseDeliverTx) IsErr() bool {
|
|
return r.Code != CodeTypeOK
|
|
}
|
|
|
|
// Error implements error interface by formatting response as string.
|
|
func (r ResponseDeliverTx) Error() string {
|
|
return fmtError(r.Code, r.Log)
|
|
}
|
|
|
|
// IsOK returns true if Code is OK.
|
|
func (r ResponseCommit) IsOK() bool {
|
|
return r.Code == CodeTypeOK
|
|
}
|
|
|
|
// IsErr returns true if Code is something other than OK.
|
|
func (r ResponseCommit) IsErr() bool {
|
|
return r.Code != CodeTypeOK
|
|
}
|
|
|
|
// Error implements error interface by formatting response as string.
|
|
func (r ResponseCommit) Error() string {
|
|
return fmtError(r.Code, r.Log)
|
|
}
|
|
|
|
// IsOK returns true if Code is OK.
|
|
func (r ResponseQuery) IsOK() bool {
|
|
return r.Code == CodeTypeOK
|
|
}
|
|
|
|
// IsErr returns true if Code is something other than OK.
|
|
func (r ResponseQuery) IsErr() bool {
|
|
return r.Code != CodeTypeOK
|
|
}
|
|
|
|
// Error implements error interface by formatting response as string.
|
|
func (r ResponseQuery) Error() string {
|
|
return fmtError(r.Code, r.Log)
|
|
}
|
|
|
|
func fmtError(code uint32, log string) string {
|
|
return fmt.Sprintf("Error code (%d): %s", code, log)
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
// override JSON marshalling so we dont emit defaults (ie. disable omitempty)
|
|
// 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{}
|
|
)
|
|
|
|
func (r *ResponseSetOption) MarshalJSON() ([]byte, error) {
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
|
return []byte(s), err
|
|
}
|
|
|
|
func (r *ResponseSetOption) UnmarshalJSON(b []byte) error {
|
|
reader := bytes.NewBuffer(b)
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
}
|
|
|
|
func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
|
return []byte(s), err
|
|
}
|
|
|
|
func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error {
|
|
reader := bytes.NewBuffer(b)
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
}
|
|
|
|
func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) {
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
|
return []byte(s), err
|
|
}
|
|
|
|
func (r *ResponseDeliverTx) UnmarshalJSON(b []byte) error {
|
|
reader := bytes.NewBuffer(b)
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
}
|
|
|
|
func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
|
return []byte(s), err
|
|
}
|
|
|
|
func (r *ResponseQuery) UnmarshalJSON(b []byte) error {
|
|
reader := bytes.NewBuffer(b)
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
}
|
|
|
|
func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
|
|
s, err := jsonpbMarshaller.MarshalToString(r)
|
|
return []byte(s), err
|
|
}
|
|
|
|
func (r *ResponseCommit) UnmarshalJSON(b []byte) error {
|
|
reader := bytes.NewBuffer(b)
|
|
return jsonpbUnmarshaller.Unmarshal(reader, r)
|
|
}
|
|
|
|
// Some compile time assertions to ensure we don't
|
|
// have accidental runtime surprises later on.
|
|
|
|
// jsonEncodingRoundTripper ensures that asserted
|
|
// interfaces implement both MarshalJSON and UnmarshalJSON
|
|
type jsonRoundTripper interface {
|
|
json.Marshaler
|
|
json.Unmarshaler
|
|
}
|
|
|
|
var _ jsonRoundTripper = (*ResponseCommit)(nil)
|
|
var _ jsonRoundTripper = (*ResponseQuery)(nil)
|
|
var _ jsonRoundTripper = (*ResponseDeliverTx)(nil)
|
|
var _ jsonRoundTripper = (*ResponseCheckTx)(nil)
|
|
var _ jsonRoundTripper = (*ResponseSetOption)(nil)
|