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.

120 lines
2.9 KiB

  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 emit defaults (ie. disable omitempty)
  36. var (
  37. jsonpbMarshaller = jsonpb.Marshaler{
  38. EnumsAsInts: true,
  39. EmitDefaults: true,
  40. }
  41. jsonpbUnmarshaller = jsonpb.Unmarshaler{}
  42. )
  43. func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
  44. s, err := jsonpbMarshaller.MarshalToString(r)
  45. return []byte(s), err
  46. }
  47. func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error {
  48. reader := bytes.NewBuffer(b)
  49. return jsonpbUnmarshaller.Unmarshal(reader, r)
  50. }
  51. func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) {
  52. s, err := jsonpbMarshaller.MarshalToString(r)
  53. return []byte(s), err
  54. }
  55. func (r *ResponseDeliverTx) UnmarshalJSON(b []byte) error {
  56. reader := bytes.NewBuffer(b)
  57. return jsonpbUnmarshaller.Unmarshal(reader, r)
  58. }
  59. func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
  60. s, err := jsonpbMarshaller.MarshalToString(r)
  61. return []byte(s), err
  62. }
  63. func (r *ResponseQuery) UnmarshalJSON(b []byte) error {
  64. reader := bytes.NewBuffer(b)
  65. return jsonpbUnmarshaller.Unmarshal(reader, r)
  66. }
  67. func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
  68. s, err := jsonpbMarshaller.MarshalToString(r)
  69. return []byte(s), err
  70. }
  71. func (r *ResponseCommit) UnmarshalJSON(b []byte) error {
  72. reader := bytes.NewBuffer(b)
  73. return jsonpbUnmarshaller.Unmarshal(reader, r)
  74. }
  75. func (r *EventAttribute) MarshalJSON() ([]byte, error) {
  76. s, err := jsonpbMarshaller.MarshalToString(r)
  77. return []byte(s), err
  78. }
  79. func (r *EventAttribute) UnmarshalJSON(b []byte) error {
  80. reader := bytes.NewBuffer(b)
  81. return jsonpbUnmarshaller.Unmarshal(reader, r)
  82. }
  83. // Some compile time assertions to ensure we don't
  84. // have accidental runtime surprises later on.
  85. // jsonEncodingRoundTripper ensures that asserted
  86. // interfaces implement both MarshalJSON and UnmarshalJSON
  87. type jsonRoundTripper interface {
  88. json.Marshaler
  89. json.Unmarshaler
  90. }
  91. var _ jsonRoundTripper = (*ResponseCommit)(nil)
  92. var _ jsonRoundTripper = (*ResponseQuery)(nil)
  93. var _ jsonRoundTripper = (*ResponseDeliverTx)(nil)
  94. var _ jsonRoundTripper = (*ResponseCheckTx)(nil)
  95. var _ jsonRoundTripper = (*EventAttribute)(nil)