-
Notifications
You must be signed in to change notification settings - Fork 10
/
types.go
73 lines (67 loc) · 1.9 KB
/
types.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
package goP2
// Nil 判断当前元素是否为 nil
func Nil(state State) (interface{}, error) {
data, err := state.Next()
if err != nil {
return nil, state.Trap("Expect nil but error %v", err)
}
if data == nil {
return nil, nil
}
return nil, state.Trap("Expect nil but %v", data)
}
// AsRune 判断当前元素是否为一个 rune
func AsRune(state State) (interface{}, error) {
data, err := state.Next()
if err != nil {
return nil, state.Trap("Expect a rune value but error %v", err)
}
if _, ok := data.(rune); ok {
return data, nil
}
return nil, state.Trap("Expect a rune value but %v", data)
}
// AsInt 判断当前元素是否为一个 int
func AsInt(state State) (interface{}, error) {
data, err := state.Next()
if err != nil {
return nil, state.Trap("Expect a int value but error %v", err)
}
if _, ok := data.(int); ok {
return data, nil
}
return nil, state.Trap("Expect a int value but %v", data)
}
// AsFloat64 判断当前元素是否为 float64
func AsFloat64(state State) (interface{}, error) {
data, err := state.Next()
if err != nil {
return nil, state.Trap("Expect a float64 value but error %v", err)
}
if _, ok := data.(float64); ok {
return data, nil
}
return nil, state.Trap("Expect a float64 value but %v", data)
}
// AsFloat32 判断当前元素是否为 float32
func AsFloat32(state State) (interface{}, error) {
data, err := state.Next()
if err == nil {
return nil, state.Trap("Expect a float32 value but error %v", err)
}
if _, ok := data.(float32); ok {
return data, nil
}
return nil, state.Trap("Expect a float32 value but %v", data)
}
// AsString 判断当前元素是否为 string
func AsString(state State) (interface{}, error) {
data, err := state.Next()
if err == nil {
return nil, state.Trap("Expect a string value but error %v", err)
}
if _, ok := data.(string); ok {
return data, nil
}
return nil, state.Trap("Expect a string value but %v", data)
}