forked from kazarena/json-gold
/
unique_namer.go
67 lines (57 loc) · 1.52 KB
/
unique_namer.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
package ld
import (
"fmt"
)
// UniqueNamer issues unique names, keeping track of any previously issued names.
type UniqueNamer struct {
prefix string
counter int
existing map[string]string
existingOrder []string
}
// NewUniqueNamer creates and returns a new UniqueNamer.
func NewUniqueNamer(prefix string) *UniqueNamer {
return &UniqueNamer{
prefix: prefix,
counter: 0,
existing: make(map[string]string),
existingOrder: make([]string, 0),
}
}
// Clone copies this UniqueNamer.
func (un *UniqueNamer) Clone() *UniqueNamer {
copy := &UniqueNamer{
prefix: un.prefix,
counter: un.counter,
existing: make(map[string]string, len(un.existing)),
existingOrder: make([]string, len(un.existingOrder)),
}
i := 0
for k, v := range un.existing {
copy.existing[k] = v
copy.existingOrder[i] = un.existingOrder[i]
i++
}
return copy
}
// GetName gets the new name for the given old name, where if no old name
// is given a new name will be generated.
func (un *UniqueNamer) GetName(oldName string) string {
if oldName != "" {
if ex, present := un.existing[oldName]; present {
return ex
}
}
name := un.prefix + fmt.Sprintf("%d", un.counter)
un.counter++
if oldName != "" {
un.existing[oldName] = name
un.existingOrder = append(un.existingOrder, oldName)
}
return name
}
// IsNamed returns true if there was already a name created for the given old name.
func (un *UniqueNamer) IsNamed(oldName string) bool {
_, hasKey := un.existing[oldName]
return hasKey
}