Skip to content
This repository has been archived by the owner on Jul 15, 2018. It is now read-only.

Commit

Permalink
Merge pull request #155 from tendermint/develop
Browse files Browse the repository at this point in the history
v0.8.0 take II
  • Loading branch information
ebuchman authored Dec 6, 2017
2 parents 12dca48 + 4721653 commit fca2b50
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
4 changes: 1 addition & 3 deletions cmd/abci-cli/abci-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,7 @@ func or(err1 error, err2 error) error {
func cmdTest(cmd *cobra.Command, args []string) error {
fmt.Println("Running tests")

var err error

err = servertest.InitChain(client)
err := servertest.InitChain(client)
fmt.Println("")
err = or(err, servertest.SetOption(client, "serial", "on"))
fmt.Println("")
Expand Down
14 changes: 14 additions & 0 deletions types/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ func TestMarshalJSON(t *testing.T) {
b, err := json.Marshal(&ResponseDeliverTx{})
assert.Nil(t, err)
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) {
Expand Down
51 changes: 41 additions & 10 deletions types/result.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"bytes"
"fmt"

"github.com/gogo/protobuf/jsonpb"
Expand Down Expand Up @@ -76,33 +77,63 @@ func fmtError(code uint32, log string) string {

//---------------------------------------------------------------------------
// 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) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
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) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
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) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
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) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
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) {
m := jsonpb.Marshaler{EmitDefaults: true}
s, err := m.MarshalToString(r)
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)
}

0 comments on commit fca2b50

Please sign in to comment.