forked from hprose/hprose-golang
/
bool_decoder.go
116 lines (104 loc) · 2.93 KB
/
bool_decoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* io/bool_decoder.go *
* *
* hprose bool decoder for Go. *
* *
* LastModified: Oct 15, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package io
import (
"errors"
"reflect"
"strconv"
"github.com/hprose/hprose-golang/util"
)
func readBoolFalse(r *Reader) bool {
return false
}
func readBoolTrue(r *Reader) bool {
return true
}
func readNumberAsBool(r *Reader) bool {
bytes := r.readUntil(TagSemicolon)
if len(bytes) == 0 {
return true
}
if len(bytes) == 1 {
return bytes[0] != '0'
}
return true
}
func readInfinityAsBool(r *Reader) bool {
r.readInf()
return true
}
func readUTF8CharAsBool(r *Reader) bool {
b, err := strconv.ParseBool(util.ByteString(r.readUTF8Slice(1)))
if err != nil {
panic(err)
}
return b
}
func readStringAsBool(r *Reader) bool {
b, err := strconv.ParseBool(r.ReadStringWithoutTag())
if err != nil {
panic(err)
}
return b
}
func readRefAsBool(r *Reader) bool {
ref := r.readRef()
if str, ok := ref.(string); ok {
b, err := strconv.ParseBool(str)
if err != nil {
panic(err)
}
return b
}
panic(errors.New("value of type " +
reflect.TypeOf(ref).String() +
" cannot be converted to type bool"))
}
var boolDecoders = [256]func(r *Reader) bool{
'0': readBoolFalse,
'1': readBoolTrue,
'2': readBoolTrue,
'3': readBoolTrue,
'4': readBoolTrue,
'5': readBoolTrue,
'6': readBoolTrue,
'7': readBoolTrue,
'8': readBoolTrue,
'9': readBoolTrue,
TagInteger: readNumberAsBool,
TagLong: readNumberAsBool,
TagDouble: readNumberAsBool,
TagNull: readBoolFalse,
TagEmpty: readBoolFalse,
TagFalse: readBoolFalse,
TagTrue: readBoolTrue,
TagNaN: readBoolTrue,
TagInfinity: readInfinityAsBool,
TagUTF8Char: readUTF8CharAsBool,
TagString: readStringAsBool,
TagRef: readRefAsBool,
}
func boolDecoder(r *Reader, v reflect.Value, tag byte) {
decoder := boolDecoders[tag]
if decoder != nil {
v.SetBool(decoder(r))
return
}
castError(tag, "bool")
}