-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoders.go
124 lines (111 loc) · 2.61 KB
/
encoders.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
117
118
119
120
121
122
123
124
package dyanmic_params
import (
"errors"
"regexp"
"strconv"
"strings"
"time"
)
const ErrCnvFailed = "conversion failed"
func convertToString(val interface{}) (string, error) {
if v, ok := val.(string); ok {
return v, nil
} else if v, ok := val.(*string); ok {
return *v, nil
}
return "", errors.New(ErrCnvFailed)
}
func convertToBytes(val interface{}) ([]byte, error) {
if v, ok := val.([]byte); ok {
return v, nil
} else if v, ok := val.(*[]byte); ok {
return *v, nil
}
return nil, errors.New(ErrCnvFailed)
}
func convertToInt(val interface{}) (int, error) {
if v, ok := val.(int); ok {
return v, nil
} else if v, ok := val.(*int); ok {
return *v, nil
}
return 0, errors.New(ErrCnvFailed)
}
func convertNumericStrToInt(val interface{}) (int, error) {
str, err := convertToString(val)
if err != nil {
return 0, err
}
if strings.Index(str, "0") == 0 {
return 0, errors.New("numeric string starts with zero")
}
rg := regexp.MustCompile(`^[0-9]+$`)
if rg.Match([]byte(str)) {
numInt, err := strconv.Atoi(str)
if err != nil {
return 0, err
}
return numInt, nil
}
return 0, errors.New(ErrCnvFailed)
}
func convertNumericStrToBool(val interface{}) (bool, error) {
str, err := convertToString(val)
if err != nil {
return false, err
}
if str == "0" || str == "false" {
return false, nil
} else if str == "1" || str == "true" {
return true, nil
}
return false, errors.New(ErrCnvFailed)
}
func convertToBool(val interface{}) (bool, error) {
if v, ok := val.(bool); ok {
return v, nil
} else if v, ok := val.(*bool); ok {
return *v, nil
}
return false, errors.New(ErrCnvFailed)
}
func convertToInt32(val interface{}) (int32, error) {
if v, ok := val.(int32); ok {
return v, nil
} else if v, ok := val.(*int32); ok {
return *v, nil
}
return 0, errors.New(ErrCnvFailed)
}
func convertToInt64(val interface{}) (int64, error) {
if v, ok := val.(int64); ok {
return v, nil
} else if v, ok := val.(*int64); ok {
return *v, nil
}
return 0, errors.New(ErrCnvFailed)
}
func convertToInt8(val interface{}) (int8, error) {
if v, ok := val.(int8); ok {
return v, nil
} else if v, ok := val.(*int8); ok {
return *v, nil
}
return 0, errors.New(ErrCnvFailed)
}
func convertToInt16(val interface{}) (int16, error) {
if v, ok := val.(int16); ok {
return v, nil
} else if v, ok := val.(*int16); ok {
return *v, nil
}
return 0, errors.New(ErrCnvFailed)
}
func convertToTimeDuration(val interface{}) (*time.Duration, error) {
if v, ok := val.(time.Duration); ok {
return &v, nil
} else if v, ok := val.(*time.Duration); ok {
return v, nil
}
return nil, errors.New(ErrCnvFailed)
}