db_args.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "encoding/json"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/rpc/shared"
  21. )
  22. type DbArgs struct {
  23. Database string
  24. Key string
  25. Value []byte
  26. }
  27. func (args *DbArgs) UnmarshalJSON(b []byte) (err error) {
  28. var obj []interface{}
  29. if err := json.Unmarshal(b, &obj); err != nil {
  30. return shared.NewDecodeParamError(err.Error())
  31. }
  32. if len(obj) < 2 {
  33. return shared.NewInsufficientParamsError(len(obj), 2)
  34. }
  35. var objstr string
  36. var ok bool
  37. if objstr, ok = obj[0].(string); !ok {
  38. return shared.NewInvalidTypeError("database", "not a string")
  39. }
  40. args.Database = objstr
  41. if objstr, ok = obj[1].(string); !ok {
  42. return shared.NewInvalidTypeError("key", "not a string")
  43. }
  44. args.Key = objstr
  45. if len(obj) > 2 {
  46. objstr, ok = obj[2].(string)
  47. if !ok {
  48. return shared.NewInvalidTypeError("value", "not a string")
  49. }
  50. args.Value = []byte(objstr)
  51. }
  52. return nil
  53. }
  54. func (a *DbArgs) requirements() error {
  55. if len(a.Database) == 0 {
  56. return shared.NewValidationError("Database", "cannot be blank")
  57. }
  58. if len(a.Key) == 0 {
  59. return shared.NewValidationError("Key", "cannot be blank")
  60. }
  61. return nil
  62. }
  63. type DbHexArgs struct {
  64. Database string
  65. Key string
  66. Value []byte
  67. }
  68. func (args *DbHexArgs) UnmarshalJSON(b []byte) (err error) {
  69. var obj []interface{}
  70. if err := json.Unmarshal(b, &obj); err != nil {
  71. return shared.NewDecodeParamError(err.Error())
  72. }
  73. if len(obj) < 2 {
  74. return shared.NewInsufficientParamsError(len(obj), 2)
  75. }
  76. var objstr string
  77. var ok bool
  78. if objstr, ok = obj[0].(string); !ok {
  79. return shared.NewInvalidTypeError("database", "not a string")
  80. }
  81. args.Database = objstr
  82. if objstr, ok = obj[1].(string); !ok {
  83. return shared.NewInvalidTypeError("key", "not a string")
  84. }
  85. args.Key = objstr
  86. if len(obj) > 2 {
  87. objstr, ok = obj[2].(string)
  88. if !ok {
  89. return shared.NewInvalidTypeError("value", "not a string")
  90. }
  91. args.Value = common.FromHex(objstr)
  92. }
  93. return nil
  94. }
  95. func (a *DbHexArgs) requirements() error {
  96. if len(a.Database) == 0 {
  97. return shared.NewValidationError("Database", "cannot be blank")
  98. }
  99. if len(a.Key) == 0 {
  100. return shared.NewValidationError("Key", "cannot be blank")
  101. }
  102. return nil
  103. }