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.

156 lines
4.0 KiB

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