-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
167 lines (149 loc) · 2.84 KB
/
convert.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package goutils
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/mitchellh/mapstructure"
)
const (
date_layoutUS = "January 2, 2006"
)
// Generics
func String_To_Date(date_str string) time.Time {
date, _ := time.Parse(date_layoutUS, date_str)
return date
}
func To_Struct(inter interface{}, struc interface{}) {
mapstructure.Decode(inter, struc)
}
func Bytes_To_Json(byteValue []byte, json_data interface{}) {
json.Unmarshal(byteValue, json_data)
}
// Int Converters
func (i Int) Value() int {
return int(i)
}
func (i_slice Int_Slice) Value() []int {
tmp_slice := make([]int, len(i_slice))
for i := range i_slice {
tmp_slice[i] = i_slice[i].Value()
}
return tmp_slice
}
func (i Int) To_Float64() Float64 {
return Float64(i)
}
func (i Int) To_Int64() Int64 {
return Int64(i)
}
func (i Int) To_String() String {
return String(strconv.Itoa(int(i)))
}
func (i Int) To_Bool() Bool {
if i == 1 {
return Bool(true)
}
return Bool(false)
}
// Float64 Converters
func (f Float64) Value() float64 {
return float64(f)
}
func (f_slice Float64_Slice) Value() []float64 {
tmp_slice := make([]float64, len(f_slice))
for i := range f_slice {
tmp_slice[i] = f_slice[i].Value()
}
return tmp_slice
}
func (f Float64) To_Int() Int {
return Int(f)
}
func (f Float64) To_Int64() Int64 {
return Int64(f)
}
func (f Float64) To_String() String {
return String(fmt.Sprintf("%g", float64(f)))
}
func (f Float64) To_Bool() Bool {
if f == 1.0 {
return Bool(true)
}
return Bool(false)
}
// String converters
func (s String) Value() string {
return string(s)
}
func (s_slice String_Slice) Value() []string {
tmp_slice := make([]string, len(s_slice))
for i := range s_slice {
tmp_slice[i] = s_slice[i].Value()
}
return tmp_slice
}
func (s String) To_Int() Int {
i, _ := strconv.Atoi(string(s))
return Int(i)
}
func (s String) To_Float64() Float64 {
f, _ := strconv.ParseFloat(string(s), 64)
return Float64(f)
}
func (s String) To_Bool() Bool {
switch s {
case "true", "True", "TRUE", "1":
return Bool(true)
default:
return Bool(false)
}
}
func (s String) To_Int64() Int64 {
i, _ := strconv.Atoi(string(s))
return Int64(i)
}
// Bool converters
func (b Bool) Value() bool {
return bool(b)
}
func (b Bool) To_Int() Int {
if b {
return 1
}
return 0
}
func (b Bool) To_Int64() Int64 {
if b {
return Int64(1)
}
return Int64(0)
}
func (b Bool) To_Float64() Float64 {
return Float64(b.To_Int())
}
func (b Bool) To_String() String {
if b {
return String("true")
}
return String("false")
}
// Int64 Converters
func (i Int64) Value() int64 {
return int64(i)
}
func (i Int64) To_Int() Int {
return Int(i)
}
func (i Int64) To_Float64() Float64 {
return Float64(i)
}
func (i Int64) To_String() String {
return String(strconv.FormatInt(int64(i), 10))
}
func (i Int64) To_Bool() Bool {
if int(i) == 1 {
return Bool(true)
}
return Bool(false)
}