This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
155 lines (146 loc) · 4.65 KB
/
utils.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
// Package utils contains utility functions used by the other task-cluster-go
// packages, such as string manipulation functions and other utility methods.
package utils
import (
"fmt"
"io/ioutil"
"strings"
)
// indents a block of text with an indent string, see http://play.golang.org/p/nV1_VLau7C
func Indent(text, indent string, escapeText bool) string {
if text == "" {
return text
}
if escapeText {
text = strings.Replace(text, "*/", "* /", -1)
}
if text[len(text)-1:] == "\n" {
result := ""
for _, j := range strings.Split(text[:len(text)-1], "\n") {
result += indent + j + "\n"
}
return result
}
result := ""
for _, j := range strings.Split(strings.TrimRight(text, "\n"), "\n") {
result += indent + j + "\n"
}
return result[:len(result)-1]
}
func Underline(text string) string {
return text + "\n" + strings.Repeat("=", len(text)) + "\n"
}
func ExitOnFail(err error) {
if err != nil {
fmt.Printf("%v\n%T\n", err, err)
panic(err)
}
}
func WriteStringToFile(content, file string) {
bytes := []byte(content)
err := ioutil.WriteFile(file, bytes, 0644)
ExitOnFail(err)
}
// See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
var reservedKeyWords = map[string]bool{
"abstract": true,
"assert": true,
"boolean": true,
"break": true,
"byte": true,
"case": true,
"catch": true,
"char": true,
"class": true,
"const": true,
"continue": true,
"default": true,
"do": true,
"double": true,
"else": true,
"enum": true,
"extends": true,
"final": true,
"finally": true,
"float": true,
"for": true,
"goto": true,
"if": true,
"implements": true,
"import": true,
"instanceof": true,
"int": true,
"interface": true,
"long": true,
"native": true,
"new": true,
"package": true,
"private": true,
"protected": true,
"public": true,
"return": true,
"short": true,
"static": true,
"strictfp": true,
"super": true,
"switch": true,
"synchronized": true,
"this": true,
"throw": true,
"throws": true,
"transient": true,
"try": true,
"void": true,
"volatile": true,
"while": true,
}
func NormaliseLower(name string, dict map[string]bool) string {
// Capitalise words, and remove spaces and dashes, to acheive struct names in CamelCase,
// but starting with an upper case letter so that the structs are exported...
normalisedName := strings.NewReplacer(" ", "", "-", "").Replace(name)
// If name already exists, add an integer suffix to name. Start with "1" and increment
// by 1 until an unused name is found. Example: if name FooBar was generated four times
// , the first instance would be called FooBar, then the next would be FooBar1, the next
// FooBar2 and the last would be assigned a name of FooBar3. We do this to guarantee we
// don't use duplicate names for different logical entities.
for k, baseName := 1, normalisedName; dict[normalisedName] || reservedKeyWords[normalisedName]; {
normalisedName = fmt.Sprintf("%v%v", baseName, k)
k++
}
dict[normalisedName] = true
return normalisedName
}
func Normalise(name string, dict map[string]bool) string {
// Capitalise words, and remove spaces and dashes, to acheive struct names in CamelCase,
// but starting with an upper case letter so that the structs are exported...
normalisedName := strings.NewReplacer(" ", "", "-", "").Replace(strings.Title(name))
// If name already exists, add an integer suffix to name. Start with "1" and increment
// by 1 until an unused name is found. Example: if name FooBar was generated four times
// , the first instance would be called FooBar, then the next would be FooBar1, the next
// FooBar2 and the last would be assigned a name of FooBar3. We do this to guarantee we
// don't use duplicate names for different logical entities.
for k, baseName := 1, normalisedName; dict[normalisedName] || reservedKeyWords[normalisedName]; {
normalisedName = fmt.Sprintf("%v%v", baseName, k)
k++
}
dict[normalisedName] = true
return normalisedName
}
// Comments (Java-style) a block of text, with the given
// indentation (white-space prefix).
func Comment(text, indent string) string {
if text == "" {
return text
}
comment := indent + "/**\n"
comment += Indent(text, indent+" * ", true)
if comment[len(comment)-1:] != "\n" {
comment += "\n"
}
comment += indent + " */\n"
trimmed := strings.Split(comment, "\n")
for i, t := range trimmed {
trimmed[i] = strings.TrimRight(t, " ")
}
return strings.Join(trimmed, "\n")
}