-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.go
69 lines (59 loc) · 1.27 KB
/
funcs.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
package squick
import (
"fmt"
"os"
"strings"
"text/template"
"time"
"unicode"
"github.com/go-openapi/swag"
"github.com/iancoleman/strcase"
)
var funcs = template.FuncMap{
"header": func() string { return fmt.Sprintf(header, time.Now().Format(time.RFC3339)) },
"command": func() string { return "// squick " + strings.Join(os.Args[1:], " ") },
"isint": func(t string) bool { return strings.HasPrefix(t, "int") },
"package": packageFromPath,
"in": inStrings,
"camel": swag.ToVarName,
"pascal": swag.ToGoName,
"plural": plur.Plural,
}
func init() {
// TODO: add an ability to specify acronyms (v1)
strcase.ConfigureAcronym("id", "ID")
}
func packageFromPath(pkg string) string {
if !strings.Contains(pkg, "/") {
return pkg
}
return pkg[strings.LastIndex(pkg, "/")+1:]
}
func inStrings(a []string, v string) bool {
for _, s := range a {
if s == v {
return true
}
}
return false
}
func replaceAcronyms(s, a string) string {
var (
word string
words []string
)
for _, r := range []rune(s) {
if unicode.IsUpper(r) {
words = append(words, word)
word = ""
}
word += string(r)
}
words = append(words, word)
for i, word := range words {
if word == a {
words[i] = strings.ToUpper(word)
}
}
return strings.Join(words, "")
}