-
Notifications
You must be signed in to change notification settings - Fork 13
/
functions.go
135 lines (119 loc) · 2.86 KB
/
functions.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
package tools
import (
"fmt"
"go/format"
"log"
"strings"
"github.com/huandu/xstrings"
)
func Sformat(buf []byte) []byte {
src, err := format.Source(buf)
if err != nil {
// Should never happen, but can arise when developing this code.
// The user can compile the output to see the error.
log.Printf("warning: internal error: invalid Go generated: %s", err)
log.Printf("warning: compile the package to analyze the error")
log.Printf("generated: \n%s", string(buf))
return buf
}
return src
}
var normalizationWords = map[string]string{
"IP": "ip",
}
func NormalizeResourceName(name string) string {
n := name
for k, v := range normalizationWords {
if strings.HasPrefix(name, k) {
n = strings.Replace(name, k, v, -1)
break
}
}
return n
}
func ToSnakeCaseName(name string) string {
return strings.Replace(xstrings.ToSnakeCase(NormalizeResourceName(name)), "-", "_", -1)
}
func ToDashedName(name string) string {
// From "CamelCase" to "dash-case"
return strings.Replace(xstrings.ToSnakeCase(NormalizeResourceName(name)), "_", "-", -1)
}
func ToCamelCaseName(name string) string {
return xstrings.ToCamelCase(strings.Replace(NormalizeResourceName(name), "-", "_", -1))
}
func ToCamelWithFirstLower(name string) string {
return xstrings.FirstRuneToLower(xstrings.ToCamelCase(strings.Replace(NormalizeResourceName(name), "-", "_", -1)))
}
func ToCLIFlagName(name string) string {
format := "--%s"
if len(name) == 1 {
format = "-%s"
}
return fmt.Sprintf(format, ToDashedName(name))
}
func FlattenStringList(list []string) string {
if len(list) > 0 {
return fmt.Sprintf("\"%s\"", strings.Join(list, "\",\""))
}
return ""
}
func FlattenIntList(list []int) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%d", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenUintList(list []uint) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%d", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenInt64List(list []int64) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%d", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenUint64List(list []uint64) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%d", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenFloatList(list []float32) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%f", s))
}
return strings.Join(tmp, ",")
}
return ""
}
func FlattenFloat64List(list []float64) string {
if len(list) > 0 {
tmp := []string{}
for _, s := range list {
tmp = append(tmp, fmt.Sprintf("%f", s))
}
return strings.Join(tmp, ",")
}
return ""
}