Browse Source

types: add MarshalJSON funcs for Response types with a Code

pull/1780/head
Ethan Buchman 7 years ago
parent
commit
e1ee4d6bf5
2 changed files with 43 additions and 0 deletions
  1. +8
    -0
      types/messages_test.go
  2. +35
    -0
      types/result.go

+ 8
- 0
types/messages_test.go View File

@ -2,12 +2,20 @@ package types
import (
"bytes"
"encoding/json"
"strings"
"testing"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
)
func TestMarshalJSON(t *testing.T) {
b, err := json.Marshal(&ResponseDeliverTx{})
assert.Nil(t, err)
assert.True(t, strings.Contains(string(b), "code"))
}
func TestWriteReadMessage(t *testing.T) {
cases := []proto.Message{
&Header{


+ 35
- 0
types/result.go View File

@ -2,6 +2,8 @@ package types
import (
"fmt"
"github.com/gogo/protobuf/jsonpb"
)
const (
@ -71,3 +73,36 @@ func (r ResponseQuery) Error() string {
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)
func (r *ResponseSetOption) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
return []byte(s), err
}
func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
return []byte(s), err
}
func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
return []byte(s), err
}
func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
return []byte(s), err
}
func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
return []byte(s), err
}

Loading…
Cancel
Save