aboutsummaryrefslogtreecommitdiffstats
path: root/rlp/decode.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-08-13 16:12:38 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-08-13 17:05:39 +0800
commit1d2420323ca555f8ac45969bd39415c5e619e4e0 (patch)
tree7ca11784e810dfab9fa9770fa9957bfe452bdb35 /rlp/decode.go
parent0dd6911c6222ce65d6aa0760f7c322246464b2d0 (diff)
downloadgo-tangerine-1d2420323ca555f8ac45969bd39415c5e619e4e0.tar
go-tangerine-1d2420323ca555f8ac45969bd39415c5e619e4e0.tar.gz
go-tangerine-1d2420323ca555f8ac45969bd39415c5e619e4e0.tar.bz2
go-tangerine-1d2420323ca555f8ac45969bd39415c5e619e4e0.tar.lz
go-tangerine-1d2420323ca555f8ac45969bd39415c5e619e4e0.tar.xz
go-tangerine-1d2420323ca555f8ac45969bd39415c5e619e4e0.tar.zst
go-tangerine-1d2420323ca555f8ac45969bd39415c5e619e4e0.zip
rlp: add support for boolean encoding/decoding
Diffstat (limited to 'rlp/decode.go')
-rw-r--r--rlp/decode.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/rlp/decode.go b/rlp/decode.go
index c0b5f0699..1381f5274 100644
--- a/rlp/decode.go
+++ b/rlp/decode.go
@@ -183,6 +183,8 @@ func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
return decodeBigIntNoPtr, nil
case isUint(kind):
return decodeUint, nil
+ case kind == reflect.Bool:
+ return decodeBool, nil
case kind == reflect.String:
return decodeString, nil
case kind == reflect.Slice || kind == reflect.Array:
@@ -211,6 +213,15 @@ func decodeUint(s *Stream, val reflect.Value) error {
return nil
}
+func decodeBool(s *Stream, val reflect.Value) error {
+ b, err := s.Bool()
+ if err != nil {
+ return wrapStreamError(err, val.Type())
+ }
+ val.SetBool(b)
+ return nil
+}
+
func decodeString(s *Stream, val reflect.Value) error {
b, err := s.Bytes()
if err != nil {
@@ -697,6 +708,24 @@ func (s *Stream) uint(maxbits int) (uint64, error) {
}
}
+// Bool reads an RLP string of up to 1 byte and returns its contents
+// as an boolean. If the input does not contain an RLP string, the
+// returned error will be ErrExpectedString.
+func (s *Stream) Bool() (bool, error) {
+ num, err := s.uint(8)
+ if err != nil {
+ return false, err
+ }
+ switch num {
+ case 0:
+ return false, nil
+ case 1:
+ return true, nil
+ default:
+ return false, fmt.Errorf("rlp: invalid boolean value: %d", num)
+ }
+}
+
// List starts decoding an RLP list. If the input does not contain a
// list, the returned error will be ErrExpectedList. When the list's
// end has been reached, any Stream operation will return EOL.