-
Notifications
You must be signed in to change notification settings - Fork 4
/
arguments.go
105 lines (83 loc) · 1.92 KB
/
arguments.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
package porthos
import (
"encoding/json"
"errors"
)
// Argument represents an RPC method arument.
type Argument interface {
AsString() (string, error)
AsInt() (int, error)
AsInt8() (int8, error)
AsInt16() (int16, error)
AsInt32() (int32, error)
AsInt64() (int64, error)
AsByte() (byte, error)
AsBool() (bool, error)
AsFloat32() (float32, error)
AsFloat64() (float64, error)
// Raw returns the argument value as a interface{}.
Raw() interface{}
}
type argument struct {
value interface{}
}
var (
// ErrTypeCast returned when a type cast fails.
ErrTypeCast = errors.New("Error reading string argument")
)
// NewArgument creates a new Argument.
func NewArgument(value interface{}) Argument {
return &argument{value}
}
func (a *argument) AsString() (string, error) {
s, valid := a.value.(string)
if valid {
return s, nil
}
return s, ErrTypeCast
}
func (a *argument) AsInt() (int, error) {
i, err := a.value.(json.Number).Int64()
return int(i), err
}
func (a *argument) AsInt8() (int8, error) {
i, err := a.value.(json.Number).Int64()
return int8(i), err
}
func (a *argument) AsInt16() (int16, error) {
i, err := a.value.(json.Number).Int64()
return int16(i), err
}
func (a *argument) AsInt32() (int32, error) {
i, err := a.value.(json.Number).Int64()
return int32(i), err
}
func (a *argument) AsInt64() (int64, error) {
i, err := a.value.(json.Number).Int64()
return i, err
}
func (a *argument) AsByte() (byte, error) {
b, valid := a.value.(byte)
if valid {
return b, nil
}
return b, ErrTypeCast
}
func (a *argument) AsBool() (bool, error) {
b, valid := a.value.(bool)
if valid {
return b, nil
}
return b, ErrTypeCast
}
func (a *argument) AsFloat32() (float32, error) {
f, err := a.value.(json.Number).Float64()
return float32(f), err
}
func (a *argument) AsFloat64() (float64, error) {
f, err := a.value.(json.Number).Float64()
return f, err
}
func (a *argument) Raw() interface{} {
return a.value
}