-
Notifications
You must be signed in to change notification settings - Fork 2
/
alias.go
52 lines (39 loc) · 909 Bytes
/
alias.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
package qb
import "strconv"
type noAlias struct{}
func (n *noAlias) Get(_ Source) string {
return ``
}
// NoAlias returns no alias.
// This function is not intended to be called directly
func NoAlias() Alias {
return &noAlias{}
}
type aliasGenerator struct {
cache map[Source]string
counters map[string]int
}
// AliasGenerator returns an incrementing alias for each new Source.
// This function is not intended to be called directly
func AliasGenerator() Alias {
return &aliasGenerator{make(map[Source]string), make(map[string]int)}
}
func (g *aliasGenerator) Get(src Source) string {
if src == nil {
return ``
}
if v, ok := g.cache[src]; ok {
return v
}
new := g.new(src)
g.cache[src] = new
return new
}
func (g *aliasGenerator) new(src Source) string {
a := src.aliasString()
g.counters[a]++
if g.counters[a] == 1 {
return a
}
return a + strconv.Itoa(g.counters[a])
}