/
value.go
63 lines (54 loc) · 1.58 KB
/
value.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
package common
import (
"fmt"
"strconv"
tcerr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
)
type value struct {
raw string
}
func (v *value) int() (int, error) {
i, e := strconv.Atoi(v.raw)
if e != nil {
msg := fmt.Sprintf("failed to parsing %s to int: %s", v.raw, e.Error())
e = tcerr.NewTencentCloudSDKError(iniErr, msg, "")
}
return i, e
}
func (v *value) int64() (int64, error) {
i, e := strconv.ParseInt(v.raw, 10, 64)
if e != nil {
msg := fmt.Sprintf("failed to parsing %s to int64: %s", v.raw, e.Error())
e = tcerr.NewTencentCloudSDKError(iniErr, msg, "")
}
return i, e
}
func (v *value) string() string {
return v.raw
}
func (v *value) bool() (bool, error) {
switch v.raw {
case "1", "t", "T", "true", "TRUE", "True", "YES", "yes", "Yes", "y", "ON", "on", "On":
return true, nil
case "0", "f", "F", "false", "FALSE", "False", "NO", "no", "No", "n", "OFF", "off", "Off":
return false, nil
}
errorMsg := fmt.Sprintf("failed to parsing \"%s\" to Bool: invalid syntax", v.raw)
return false, tcerr.NewTencentCloudSDKError(iniErr, errorMsg, "")
}
func (v *value) float32() (float32, error) {
f, e := strconv.ParseFloat(v.raw, 32)
if e != nil {
msg := fmt.Sprintf("failed to parse %s to Float32: %s", v.raw, e.Error())
e = tcerr.NewTencentCloudSDKError(iniErr, msg, "")
}
return float32(f), e
}
func (v *value) float64() (float64, error) {
f, e := strconv.ParseFloat(v.raw, 64)
if e != nil {
msg := fmt.Sprintf("failed to parse %s to Float64: %s", v.raw, e.Error())
e = tcerr.NewTencentCloudSDKError(iniErr, msg, "")
}
return f, e
}