github.com/evdatsion/aphelion-dpos-bft@v0.32.1/abci/types/result.go (about) 1 package types 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "github.com/gogo/protobuf/jsonpb" 8 ) 9 10 const ( 11 CodeTypeOK uint32 = 0 12 ) 13 14 // IsOK returns true if Code is OK. 15 func (r ResponseCheckTx) IsOK() bool { 16 return r.Code == CodeTypeOK 17 } 18 19 // IsErr returns true if Code is something other than OK. 20 func (r ResponseCheckTx) IsErr() bool { 21 return r.Code != CodeTypeOK 22 } 23 24 // IsOK returns true if Code is OK. 25 func (r ResponseDeliverTx) IsOK() bool { 26 return r.Code == CodeTypeOK 27 } 28 29 // IsErr returns true if Code is something other than OK. 30 func (r ResponseDeliverTx) IsErr() bool { 31 return r.Code != CodeTypeOK 32 } 33 34 // IsOK returns true if Code is OK. 35 func (r ResponseQuery) IsOK() bool { 36 return r.Code == CodeTypeOK 37 } 38 39 // IsErr returns true if Code is something other than OK. 40 func (r ResponseQuery) IsErr() bool { 41 return r.Code != CodeTypeOK 42 } 43 44 //--------------------------------------------------------------------------- 45 // override JSON marshalling so we dont emit defaults (ie. disable omitempty) 46 // note we need Unmarshal functions too because protobuf had the bright idea 47 // to marshal int64->string. cool. cool, cool, cool: https://developers.google.com/protocol-buffers/docs/proto3#json 48 49 var ( 50 jsonpbMarshaller = jsonpb.Marshaler{ 51 EnumsAsInts: true, 52 EmitDefaults: false, 53 } 54 jsonpbUnmarshaller = jsonpb.Unmarshaler{} 55 ) 56 57 func (r *ResponseSetOption) MarshalJSON() ([]byte, error) { 58 s, err := jsonpbMarshaller.MarshalToString(r) 59 return []byte(s), err 60 } 61 62 func (r *ResponseSetOption) UnmarshalJSON(b []byte) error { 63 reader := bytes.NewBuffer(b) 64 return jsonpbUnmarshaller.Unmarshal(reader, r) 65 } 66 67 func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) { 68 s, err := jsonpbMarshaller.MarshalToString(r) 69 return []byte(s), err 70 } 71 72 func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error { 73 reader := bytes.NewBuffer(b) 74 return jsonpbUnmarshaller.Unmarshal(reader, r) 75 } 76 77 func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) { 78 s, err := jsonpbMarshaller.MarshalToString(r) 79 return []byte(s), err 80 } 81 82 func (r *ResponseDeliverTx) UnmarshalJSON(b []byte) error { 83 reader := bytes.NewBuffer(b) 84 return jsonpbUnmarshaller.Unmarshal(reader, r) 85 } 86 87 func (r *ResponseQuery) MarshalJSON() ([]byte, error) { 88 s, err := jsonpbMarshaller.MarshalToString(r) 89 return []byte(s), err 90 } 91 92 func (r *ResponseQuery) UnmarshalJSON(b []byte) error { 93 reader := bytes.NewBuffer(b) 94 return jsonpbUnmarshaller.Unmarshal(reader, r) 95 } 96 97 func (r *ResponseCommit) MarshalJSON() ([]byte, error) { 98 s, err := jsonpbMarshaller.MarshalToString(r) 99 return []byte(s), err 100 } 101 102 func (r *ResponseCommit) UnmarshalJSON(b []byte) error { 103 reader := bytes.NewBuffer(b) 104 return jsonpbUnmarshaller.Unmarshal(reader, r) 105 } 106 107 // Some compile time assertions to ensure we don't 108 // have accidental runtime surprises later on. 109 110 // jsonEncodingRoundTripper ensures that asserted 111 // interfaces implement both MarshalJSON and UnmarshalJSON 112 type jsonRoundTripper interface { 113 json.Marshaler 114 json.Unmarshaler 115 } 116 117 var _ jsonRoundTripper = (*ResponseCommit)(nil) 118 var _ jsonRoundTripper = (*ResponseQuery)(nil) 119 var _ jsonRoundTripper = (*ResponseDeliverTx)(nil) 120 var _ jsonRoundTripper = (*ResponseCheckTx)(nil) 121 var _ jsonRoundTripper = (*ResponseSetOption)(nil)