-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatypes.go
252 lines (229 loc) · 6.3 KB
/
datatypes.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package common
import (
"errors"
"strings"
)
// Generic Type
type Type interface{}
// Generic Message
type Message Type
//Storage Unit representing type
type StorageUnit int
const (
//bytes = 1b
Bt StorageUnit = 1
//KiloBytes = 1024b
Kbt StorageUnit = 2
//MegaBytes = 1048576b
Mbt StorageUnit = 3
//GigaBytes = 1073741824b
Gbt StorageUnit = 4
//TeraBytes = 1099511627776b
Tbt StorageUnit = 5
)
// Transform text in Storage Unit.
//
// Parameters:
// text (string) Text to convert
//
// Returns:
// common.StorageUnit Storage Unit representing the text data or 0 anyway
// error Any suitable error risen during code execution
func StringToStorageUnit(text string) (StorageUnit, error) {
var testText = strings.ToUpper(strings.TrimSpace(text))
if testText == "" {
return 0, errors.New("common :: StringToStorageUnit : Empty input text")
}
switch testText {
case "B":
return Bt, nil
case "KB":
return Kbt, nil
case "MB":
return Mbt, nil
case "GB":
return Gbt, nil
case "TB":
return Tbt, nil
}
return 0, errors.New("common :: StringToStorageUnit : Storage Unit is undefined")
}
// Transform Storage Unit in representing text.
//
// Parameters:
// unit (common.StorageUnit) Storage Unit
//
// Returns:
// string Text representing the Storage Unit or 0 anyway
// error Any suitable error risen during code execution
func StorageUnitToString(unit StorageUnit) (string, error) {
if unit < Bt || unit > Tbt {
return "", errors.New("common :: StorageUnitToString : Storage Unit out of range")
}
switch unit {
case Bt:
return "b", nil
case Kbt:
return "Kb", nil
case Mbt:
return "Mb", nil
case Gbt:
return "Gb", nil
case Tbt:
return "Tb", nil
}
return "", errors.New("common :: StorageUnitToString : Storage Unit is undefined")
}
// Returns Storage Unit representin factor in number of bytes.
//
// Parameters:
// unit (common.StorageUnit) Reference Storage Unit
//
// Returns:
// int64 Factor of conversion to bytes or 0 anyway
// error Any suitable error risen during code execution
func StorageUnitBytesFactor(unit StorageUnit) (int64, error) {
if unit < Bt || unit > Tbt {
return 0, errors.New("common :: StorageUnitMultiplier : Storage Unit out of range")
}
switch unit {
case Bt:
return 1, nil
case Kbt:
return 1024, nil
case Mbt:
return 1048576, nil
case Gbt:
return 1073741824, nil
case Tbt:
return 1099511627776, nil
}
return 0, errors.New("common :: StorageUnitMultiplier : Storage Unit is undefined")
}
//Storage Unit representing type
type StreamInOutFormat int
const (
//Plain text input/output format
PlainTextFormat StreamInOutFormat = 11
//Json input/output format
JsonFormat StreamInOutFormat = 12
//Yaml input/output format
YamlFormat StreamInOutFormat = 13
//Xml input/output format
XmlFormat StreamInOutFormat = 14
//Base64 encrypted text input/output format
EncryptedFormat StreamInOutFormat = 15
//Text encrypted input/output format
BinaryFormat StreamInOutFormat = 16
//Text Go Format input/output format
GoStructFormat StreamInOutFormat = 17
)
// Transform text in Stream Input/Output format.
//
// Parameters:
// text (string) Text to convert
//
// Returns:
// common.StreamInOutFormat Stream Input/Output format representing the text data or 0 anyway
// error Any suitable error risen during code execution
func StringToStreamInOutFormat(text string) (StreamInOutFormat, error) {
var testText = strings.ToLower(strings.TrimSpace(text))
if testText == "" {
return 0, errors.New("common :: StringToStreamInOutFormat : Empty input text")
}
switch testText {
case "text/plain":
return PlainTextFormat, nil
case "application/json":
return JsonFormat, nil
case "application/yaml":
return YamlFormat, nil
case "application/xml":
return XmlFormat, nil
case "application/base64":
return EncryptedFormat, nil
case "application/binary":
return BinaryFormat, nil
}
return 0, errors.New("common :: StringToStreamInOutFormat : Stream Input/Output format is undefined")
}
// Transform Stream Input/Output format in representing text.
//
// Parameters:
// format (common.StreamInOutFormat) Stream Input/Output format
//
// Returns:
// string Text representing the Stream Input/Output format or 0 anyway
// error Any suitable error risen during code execution
func StreamInOutFormatToString(format StreamInOutFormat) (string, error) {
if format < PlainTextFormat || format > BinaryFormat {
return "", errors.New("common :: StorageUnitToString : Stream Input/Output format out of range")
}
switch format {
case PlainTextFormat:
return "text/plain", nil
case JsonFormat:
return "application/json", nil
case YamlFormat:
return "application/yaml", nil
case XmlFormat:
return "application/xml", nil
case EncryptedFormat:
return "application/base64", nil
case BinaryFormat:
return "application/binary", nil
}
return "", errors.New("common :: StringToStreamInOutFormat : Stream Input/Output format is undefined")
}
// Type WriterType describe any Writer Option in the cofiguration
type WriterType string
const (
// Stadard Output Writer Type
StdOutWriter WriterType = "StdOut"
// File Output Writer Type
FileWriter WriterType = "File"
// URL Output Writer Type
UrlWriter WriterType = "Url"
)
// Transform text in Writer Type.
//
// Parameters:
// text (string) Text to convert
//
// Returns:
// common.WriterType Writer Type representing the text data or 0 anyway
// error Any suitable error risen during code execution
func StringToWriterType(text string) (WriterType, error) {
var testText = strings.ToLower(strings.TrimSpace(text))
if testText == "" {
return "", errors.New("common :: StringToWriterType : Empty input text")
}
switch testText {
case "stdout":
return StdOutWriter, nil
case "file":
return FileWriter, nil
case "url":
return UrlWriter, nil
}
return "", errors.New("common :: StringToWriterType : Writer Type is undefined")
}
// Transform Writer Type in representing text.
//
// Parameters:
// wType (common.WriterType) Writer Type
//
// Returns:
// string Text representing the WriterType or 0 anyway
// error Any suitable error risen during code execution
func WriterTypeToString(wType WriterType) (string, error) {
switch wType {
case StdOutWriter:
return "StdOut", nil
case FileWriter:
return "File", nil
case UrlWriter:
return "Url", nil
}
return "", errors.New("common :: WriterTypeToString : Unknown Writer Type")
}