-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
134 lines (120 loc) · 3.02 KB
/
util.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
package main
import (
"log"
"os"
"reflect"
"strings"
"unicode"
"unicode/utf8"
"github.com/pascaldekloe/name"
)
// isDirectory reports whether the named file is a directory.
func isDirectory(name string) bool {
info, err := os.Stat(name)
if err != nil {
log.Fatal(err)
}
return info.IsDir()
}
func trimValueNames(values []Value, prefix string) []Value {
for i := range values {
values[i].name = strings.TrimPrefix(values[i].name, prefix)
}
return values
}
// prefixValueNames adds a prefix to each name
func prefixValueNames(values []Value, prefix string) []Value {
for i := range values {
values[i].name = prefix + values[i].name
}
return values
}
func transformValueNames(values []Value, transformMethod string) []Value {
var fn func(src string) string
switch transformMethod {
case "snake":
fn = func(s string) string {
return strings.ToLower(name.Delimit(s, '_'))
}
case "snake_upper", "snake-upper":
fn = func(s string) string {
return strings.ToUpper(name.Delimit(s, '_'))
}
case "kebab":
fn = func(s string) string {
return strings.ToLower(name.Delimit(s, '-'))
}
case "kebab_upper", "kebab-upper":
fn = func(s string) string {
return strings.ToUpper(name.Delimit(s, '-'))
}
case "upper":
fn = func(s string) string {
return strings.ToUpper(s)
}
case "lower":
fn = func(s string) string {
return strings.ToLower(s)
}
case "title":
fn = func(s string) string {
return strings.Title(s)
}
case "title-lower":
fn = func(s string) string {
title := []rune(strings.Title(s))
title[0] = unicode.ToLower(title[0])
return string(title)
}
case "first":
fn = func(s string) string {
r, _ := utf8.DecodeRuneInString(s)
return string(r)
}
case "first_upper", "first-upper":
fn = func(s string) string {
r, _ := utf8.DecodeRuneInString(s)
return strings.ToUpper(string(r))
}
case "first_lower", "first-lower":
fn = func(s string) string {
r, _ := utf8.DecodeRuneInString(s)
return strings.ToLower(string(r))
}
case "whitespace":
fn = func(s string) string {
return strings.ToLower(name.Delimit(s, ' '))
}
default:
fn = func(s string) string {
return s
}
}
for i, v := range values {
after := fn(v.name)
// If the original one was "" or the one before the transformation
// was "" (most commonly if linecomment defines it as empty) we
// do not care if it's empty.
// But if any of them was not empty before then it means that
// the transformed emptied the value
if v.originalName != "" && v.name != "" && after == "" {
log.Fatalf("transformation of %q (%s) got an empty result", v.name, v.originalName)
}
values[i].name = after
}
return values
}
func structToMap(obj interface{}) map[string]interface{} {
result := make(map[string]interface{})
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
fieldName := typ.Field(i).Name
fieldValue := val.Field(i).Interface()
result[fieldName] = fieldValue
}
return result
}