Browse Source

types: add UnmarshalJSON funcs for Response types

pull/1780/head
Ethan Buchman 7 years ago
parent
commit
47216538fd
3 changed files with 56 additions and 13 deletions
  1. +1
    -3
      cmd/abci-cli/abci-cli.go
  2. +14
    -0
      types/messages_test.go
  3. +41
    -10
      types/result.go

+ 1
- 3
cmd/abci-cli/abci-cli.go View File

@ -311,9 +311,7 @@ func or(err1 error, err2 error) error {
func cmdTest(cmd *cobra.Command, args []string) error { func cmdTest(cmd *cobra.Command, args []string) error {
fmt.Println("Running tests") fmt.Println("Running tests")
var err error
err = servertest.InitChain(client)
err := servertest.InitChain(client)
fmt.Println("") fmt.Println("")
err = or(err, servertest.SetOption(client, "serial", "on")) err = or(err, servertest.SetOption(client, "serial", "on"))
fmt.Println("") fmt.Println("")


+ 14
- 0
types/messages_test.go View File

@ -14,6 +14,20 @@ func TestMarshalJSON(t *testing.T) {
b, err := json.Marshal(&ResponseDeliverTx{}) b, err := json.Marshal(&ResponseDeliverTx{})
assert.Nil(t, err) assert.Nil(t, err)
assert.True(t, strings.Contains(string(b), "code")) assert.True(t, strings.Contains(string(b), "code"))
r1 := ResponseCheckTx{
Code: 1,
Data: []byte("hello"),
Gas: 43,
Fee: 12,
}
b, err = json.Marshal(&r1)
assert.Nil(t, err)
var r2 ResponseCheckTx
err = json.Unmarshal(b, &r2)
assert.Nil(t, err)
assert.Equal(t, r1, r2)
} }
func TestWriteReadMessage(t *testing.T) { func TestWriteReadMessage(t *testing.T) {


+ 41
- 10
types/result.go View File

@ -1,6 +1,7 @@
package types package types
import ( import (
"bytes"
"fmt" "fmt"
"github.com/gogo/protobuf/jsonpb" "github.com/gogo/protobuf/jsonpb"
@ -76,33 +77,63 @@ func fmtError(code uint32, log string) string {
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// override JSON marshalling so we dont emit defaults (ie. disable omitempty) // 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) { func (r *ResponseSetOption) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err 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) { func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err 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) { func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err 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) { func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err 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) { func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err return []byte(s), err
} }
func (r *ResponseCommit) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}

Loading…
Cancel
Save