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.

82 lines
1.3 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("TMSP code:%v, data:%X, log:%v", res.Code, res.Data, res.Log)
  26. }
  27. func (res Result) PrependLog(log string) Result {
  28. return Result{
  29. Code: res.Code,
  30. Data: res.Data,
  31. Log: log + ";" + res.Log,
  32. }
  33. }
  34. func (res Result) AppendLog(log string) Result {
  35. return Result{
  36. Code: res.Code,
  37. Data: res.Data,
  38. Log: res.Log + ";" + log,
  39. }
  40. }
  41. func (res Result) SetLog(log string) Result {
  42. return Result{
  43. Code: res.Code,
  44. Data: res.Data,
  45. Log: log,
  46. }
  47. }
  48. func (res Result) SetData(data []byte) Result {
  49. return Result{
  50. Code: res.Code,
  51. Data: data,
  52. Log: res.Log,
  53. }
  54. }
  55. //----------------------------------------
  56. // NOTE: if data == nil and log == "", same as zero Result.
  57. func NewResultOK(data []byte, log string) Result {
  58. return Result{
  59. Code: CodeType_OK,
  60. Data: data,
  61. Log: log,
  62. }
  63. }
  64. func NewError(code CodeType, log string) Result {
  65. return Result{
  66. Code: code,
  67. Log: log,
  68. }
  69. }