You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.1 KiB

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