-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnames.go
55 lines (48 loc) · 1.2 KB
/
names.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
package main
import (
"strings"
"sync"
"unicode"
)
var (
nameMux = sync.RWMutex{}
graphQLNameToOriginal = make(map[string]string)
originalNameToGraphQL = make(map[string]string)
)
func addName(originalName string) {
graphQLName := normalizeName(originalName)
nameMux.Lock()
defer nameMux.Unlock()
graphQLNameToOriginal[graphQLName] = originalName
originalNameToGraphQL[originalName] = graphQLName
}
func getGraphQLName(originalName string) string {
nameMux.RLock()
defer nameMux.RUnlock()
return originalNameToGraphQL[originalName]
}
func getOriginalName(graphQLName string) string {
nameMux.RLock()
defer nameMux.RUnlock()
return graphQLNameToOriginal[graphQLName]
}
func replaceAtIndex(s, r string, i int) string {
return s[:i] + r + s[i+1:]
}
func normalizeName(name string) string {
if strings.HasPrefix(name, "+") {
name = replaceAtIndex(name, "plus_", 0)
}
if strings.HasPrefix(name, "-") {
name = replaceAtIndex(name, "minus_", 0)
}
for i := range name {
if !(unicode.IsLetter(rune(name[i])) || unicode.IsNumber(rune(name[i]))) {
name = replaceAtIndex(name, "_", i)
}
}
if len(name) > 0 && name[0] >= '0' && name[0] <= '9' {
name = "_" + name
}
return name
}