-
Notifications
You must be signed in to change notification settings - Fork 45
/
utils.go
174 lines (152 loc) · 3.12 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package importer
import (
"net/url"
"regexp"
"strings"
"github.com/anz-bank/sysl/pkg/syslutil"
)
func getDescription(d string) string {
if d == "" {
return "No description."
}
return d
}
func quote(s string) string {
if s == "" {
return ""
}
return `"` + s + `"`
}
func isBuiltInType(item Type) bool {
switch item.(type) {
case *SyslBuiltIn, *ImportedBuiltInAlias:
return true
}
return false
}
func isUnionType(item Type) bool {
if _, ok := item.(*Union); ok {
return true
}
return false
}
func isExternalAlias(item Type) bool {
switch item.(type) {
case *ExternalAlias, *Array, *Enum, *Alias:
return true
}
return false
}
func getSyslTypeName(item Type) string {
if item == nil {
return ""
}
switch t := item.(type) {
case *Array:
return "sequence of " + getSyslTypeName(t.Items)
case *Enum, *Alias, *Union:
return item.Name()
case *ImportedBuiltInAlias:
return getSyslTypeName(t.Target)
}
if isExternalAlias(item) {
return "EXTERNAL_" + item.Name()
}
if !isBuiltInType(item) {
lower := strings.ToLower(item.Name())
for _, bi := range syslutil.BuiltInTypes {
if strings.HasPrefix(lower, bi) {
return "_" + item.Name()
}
}
}
return item.Name()
}
func spaceSeparate(items ...string) string {
var t []string
for _, i := range items {
if i != "" {
t = append(t, i)
}
}
return strings.Join(t, " ")
}
// escapeUnsafeSyslChars escapes special characters
// returns a string with URL encoded replacements
func escapeUnsafeSyslChars(name string) string {
// url.PathEscape does not escape '. :'
charsToReplace := map[string]string{
".": `%2E`,
":": `%3A`,
"+": `%2B`,
"$": `%24`,
"&": `%26`,
}
name = url.PathEscape(name)
for realChar, hex := range charsToReplace {
name = strings.ReplaceAll(name, realChar, hex)
}
return name
}
func getSyslSafeName(name string) string {
name = escapeUnsafeSyslChars(name)
// Taken from `Name` in the .g4
validSyslNameStart := regexp.MustCompile("^(%[0-9a-fA-F][0-9a-fA-F])*[a-zA-Z_]")
if !validSyslNameStart.MatchString(name) {
name = "_" + name
}
return name
}
func getSyslSafeURI(endpoint string) string {
endpoint = escapeUnsafeSyslChars(endpoint)
charsToKeep := map[string]string{
`%2F`: "/",
`%7B`: "{",
`%7D`: "}",
`%3D`: "=",
`%3F`: "?",
`%26`: "&",
}
for hex, realChar := range charsToKeep {
endpoint = strings.ReplaceAll(endpoint, hex, realChar)
}
return endpoint
}
func cleanEndpointPath(path string) string {
return strings.NewReplacer(
"/", "_",
"{", "_",
"}", "_",
"-", "_").Replace(path)
}
func cleanMediaType(path string) string {
return strings.NewReplacer(
"/", "_",
"+", "_",
"-", "_",
".", "_",
"*", "_").Replace(path)
}
func convertToSyslSafe(name string) string {
if !strings.ContainsAny(name, "- ") {
return name
}
syslSafe := strings.Builder{}
toUppercase := false
for i := 0; i < len(name); i++ {
switch name[i] {
case '-':
toUppercase = true
case ' ':
continue
default:
if toUppercase {
syslSafe.WriteString(strings.ToUpper(string(name[i])))
toUppercase = false
} else {
syslSafe.WriteByte(name[i])
}
}
}
return syslSafe.String()
}