primitives.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2016 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. // Contains various wrappers for primitive types.
  17. package geth
  18. import (
  19. "errors"
  20. "fmt"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. // Strings represents s slice of strs.
  24. type Strings struct{ strs []string }
  25. // Size returns the number of strs in the slice.
  26. func (s *Strings) Size() int {
  27. return len(s.strs)
  28. }
  29. // Get returns the string at the given index from the slice.
  30. func (s *Strings) Get(index int) (str string, _ error) {
  31. if index < 0 || index >= len(s.strs) {
  32. return "", errors.New("index out of bounds")
  33. }
  34. return s.strs[index], nil
  35. }
  36. // Set sets the string at the given index in the slice.
  37. func (s *Strings) Set(index int, str string) error {
  38. if index < 0 || index >= len(s.strs) {
  39. return errors.New("index out of bounds")
  40. }
  41. s.strs[index] = str
  42. return nil
  43. }
  44. // String implements the Stringer interface.
  45. func (s *Strings) String() string {
  46. return fmt.Sprintf("%v", s.strs)
  47. }
  48. // Bools represents a slice of bool.
  49. type Bools struct{ bools []bool }
  50. // Size returns the number of bool in the slice.
  51. func (bs *Bools) Size() int {
  52. return len(bs.bools)
  53. }
  54. // Get returns the bool at the given index from the slice.
  55. func (bs *Bools) Get(index int) (b bool, _ error) {
  56. if index < 0 || index >= len(bs.bools) {
  57. return false, errors.New("index out of bounds")
  58. }
  59. return bs.bools[index], nil
  60. }
  61. // Set sets the bool at the given index in the slice.
  62. func (bs *Bools) Set(index int, b bool) error {
  63. if index < 0 || index >= len(bs.bools) {
  64. return errors.New("index out of bounds")
  65. }
  66. bs.bools[index] = b
  67. return nil
  68. }
  69. // String implements the Stringer interface.
  70. func (bs *Bools) String() string {
  71. return fmt.Sprintf("%v", bs.bools)
  72. }
  73. // Binaries represents a slice of byte slice
  74. type Binaries struct{ binaries [][]byte }
  75. // Size returns the number of byte slice in the slice.
  76. func (bs *Binaries) Size() int {
  77. return len(bs.binaries)
  78. }
  79. // Get returns the byte slice at the given index from the slice.
  80. func (bs *Binaries) Get(index int) (binary []byte, _ error) {
  81. if index < 0 || index >= len(bs.binaries) {
  82. return nil, errors.New("index out of bounds")
  83. }
  84. return common.CopyBytes(bs.binaries[index]), nil
  85. }
  86. // Set sets the byte slice at the given index in the slice.
  87. func (bs *Binaries) Set(index int, binary []byte) error {
  88. if index < 0 || index >= len(bs.binaries) {
  89. return errors.New("index out of bounds")
  90. }
  91. bs.binaries[index] = common.CopyBytes(binary)
  92. return nil
  93. }
  94. // String implements the Stringer interface.
  95. func (bs *Binaries) String() string {
  96. return fmt.Sprintf("%v", bs.binaries)
  97. }