/
helpers.go
96 lines (84 loc) · 1.83 KB
/
helpers.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
package codegen
import (
"errors"
"os"
"regexp"
"strings"
)
var (
ErrNotAStruct = errors.New("given objects underlying type is not a struct")
ErrUnsupportedFieldType = errors.New("given field type is not supported")
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
mapRegex = regexp.MustCompile(`map\[(.*)\](.*)`)
)
func Deduplicate(s []string) []string {
m := make(map[string]bool)
for _, v := range s {
m[v] = true
}
var res []string
for k := range m {
res = append(res, k)
}
return res
}
func makeDir(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.Mkdir(path, 0755)
if err != nil {
panic(err)
}
}
}
func ToSnakeCase(str string) string {
str = strings.Replace(str, "ARNs", "Arns", -1) // ARNs breaks the snake case comversion and makes it ..._ar_ns
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
func SnakeToHyphen(str string) string {
return strings.Replace(str, "_", "-", -1)
}
func stringInList(s string, list []string) bool {
for _, s2 := range list {
if s == s2 {
return true
}
}
return false
}
func isTimeField(s string) bool {
return s == "time.Time" || s == "*time.Time"
}
func isTagField(s string) bool {
return s == "Tags" || s == "TagSet" || s == "TagList"
}
func typeToParquetType(s string) string {
switch s {
case "string":
return "BYTE_ARRAY"
case "int32":
return "INT32"
case "int":
return "INT32"
case "bool":
return "BOOLEAN"
case "int64":
return "INT64"
case "float32":
return "FLOAT"
case "float64":
return "DOUBLE"
default:
return ""
}
}
func typeToParquetConvertedType(s string) string {
switch s {
case "string":
return "UTF8"
default:
return ""
}
}