-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
identifier.go
77 lines (67 loc) · 1.51 KB
/
identifier.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
package main
import (
"fmt"
"strings"
)
// Identifier ...
type Identifier struct {
scope string
key string
}
func (id *Identifier) prompt() string {
if id.scope == "" {
return fmt.Sprintf("%s: ", id.key)
}
return fmt.Sprintf("[%s] %s: ", id.scope, id.key)
}
// IdentifierGroup ...
type IdentifierGroup struct {
scope string
keys []string
}
func (idg *IdentifierGroup) prompt() string {
return fmt.Sprintf("[%s] %s: ", idg.scope, strings.Join(idg.keys, ", "))
}
func found(values map[string]map[string]string, id *Identifier) bool {
if v, ok := values[id.scope]; ok {
if _, ok := v[id.key]; ok {
return true
}
}
return false
}
func collect(identifiers []*Identifier, scope string) *IdentifierGroup {
var keys []string
added := make(map[string]bool)
for _, id := range identifiers {
if scope == id.scope && !added[id.key] {
keys = append(keys, id.key)
added[id.key] = true
}
}
return &IdentifierGroup{scope: scope, keys: keys}
}
func insert(values map[string]map[string]string, id *Identifier, value string) {
if _, ok := values[id.scope]; !ok {
values[id.scope] = make(map[string]string)
}
values[id.scope][id.key] = value
}
func empty(values map[string]map[string]string) bool {
for scope := range values {
for key := range values[scope] {
if values[scope][key] != "" {
return false
}
}
}
return true
}
func lookup(values map[string]map[string]string, id *Identifier) string {
if v, ok := values[id.scope]; ok {
if v, ok := v[id.key]; ok {
return v
}
}
return ""
}