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.

86 lines
1.5 KiB

  1. package types
  2. import (
  3. "fmt"
  4. )
  5. // CONTRACT: a zero Result is OK.
  6. type Result struct {
  7. Code CodeType
  8. Data []byte
  9. Log string // Can be non-deterministic
  10. }
  11. func NewResult(code CodeType, data []byte, log string) Result {
  12. return Result{
  13. Code: code,
  14. Data: data,
  15. Log: log,
  16. }
  17. }
  18. func (res Result) IsOK() bool {
  19. return res.Code == CodeType_OK
  20. }
  21. func (res Result) IsErr() bool {
  22. return res.Code != CodeType_OK
  23. }
  24. func (res Result) Error() string {
  25. return fmt.Sprintf("ABCI{code:%v, data:%X, log:%v}", res.Code, res.Data, res.Log)
  26. }
  27. func (res Result) String() string {
  28. return fmt.Sprintf("ABCI{code:%v, data:%X, log:%v}", res.Code, res.Data, res.Log)
  29. }
  30. func (res Result) PrependLog(log string) Result {
  31. return Result{
  32. Code: res.Code,
  33. Data: res.Data,
  34. Log: log + ";" + res.Log,
  35. }
  36. }
  37. func (res Result) AppendLog(log string) Result {
  38. return Result{
  39. Code: res.Code,
  40. Data: res.Data,
  41. Log: res.Log + ";" + log,
  42. }
  43. }
  44. func (res Result) SetLog(log string) Result {
  45. return Result{
  46. Code: res.Code,
  47. Data: res.Data,
  48. Log: log,
  49. }
  50. }
  51. func (res Result) SetData(data []byte) Result {
  52. return Result{
  53. Code: res.Code,
  54. Data: data,
  55. Log: res.Log,
  56. }
  57. }
  58. //----------------------------------------
  59. // NOTE: if data == nil and log == "", same as zero Result.
  60. func NewResultOK(data []byte, log string) Result {
  61. return Result{
  62. Code: CodeType_OK,
  63. Data: data,
  64. Log: log,
  65. }
  66. }
  67. func NewError(code CodeType, log string) Result {
  68. return Result{
  69. Code: code,
  70. Log: log,
  71. }
  72. }