/
set.go
168 lines (144 loc) · 3.85 KB
/
set.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
package v1alpha1
import (
"fmt"
)
//////////////////// SetProfile ////////////////
// Exposes ValueProfile interface
// A Slice of tokens
type SetProfile []string
func (profile *SetProfile) profileI(args ...interface{}) {
switch v := args[0].(type) {
case string:
profile.ProfileString(v)
case []string:
profile.ProfileStringSlice(v)
default:
panic("Unsupported type in SetProfile")
}
}
func (profile *SetProfile) ProfileString(str string) {
*profile = []string{str}
}
func (profile *SetProfile) ProfileStringSlice(strSlice []string) {
*profile = make(SetProfile, len(strSlice))
copy(*profile, strSlice)
}
//////////////////// SetPile ////////////////
// Exposes ValuePile interface
// During json.Marshal(), SetPile exposes only the List
// After json.Unmarshal(), the map will be nil even when the List is not empty
// If the map is nil, it should be populated from the information in List
// If the map is populated it is always kept in-sync with the information in List
type SetPile struct {
List []string `json:"set"`
m map[string]bool
}
func (pile *SetPile) addI(valProfile ValueProfile) {
pile.Add(valProfile.(*SetProfile))
}
func (pile *SetPile) Add(profile *SetProfile) {
if *profile == nil {
return
}
if pile.m == nil {
pile.m = make(map[string]bool, len(pile.List)+len(*profile))
// Populate the map from the information in List
for _, v := range pile.List {
pile.m[v] = true
}
}
for _, v := range *profile {
if !pile.m[v] {
pile.m[v] = true
pile.List = append(pile.List, v)
}
}
}
func (pile *SetPile) Clear() {
pile.m = nil
pile.List = nil
}
func (pile *SetPile) mergeI(otherValPile ValuePile) {
pile.Merge(otherValPile.(*SetPile))
}
func (pile *SetPile) Merge(otherPile *SetPile) {
if pile.List == nil {
pile.List = otherPile.List
pile.m = otherPile.m
return
}
if pile.m == nil {
pile.m = make(map[string]bool, len(pile.List)+len(otherPile.List))
// Populate the map from the information in List
for _, v := range pile.List {
pile.m[v] = true
}
}
for _, v := range otherPile.List {
if !pile.m[v] {
pile.m[v] = true
pile.List = append(pile.List, v)
}
}
}
//////////////////// SetConfig ////////////////
// Exposes ValueConfig interface
// During json.Marshal(), SetConfig exposes only the List
// After json.Unmarshal(), the map will be nil even when the List is not empty
// If the map is nil, it should be populated from the information in List
// If the map is populated it is always kept in-sync with the information in List
type SetConfig struct {
List []string `json:"set"`
m map[string]bool
}
func (config *SetConfig) decideI(valProfile ValueProfile) string {
return config.Decide(valProfile.(*SetProfile))
}
func (config *SetConfig) Decide(profile *SetProfile) string {
if *profile == nil {
return ""
}
if config.m == nil {
config.m = make(map[string]bool, len(config.List))
// Populate the map from the information in List
for _, v := range config.List {
config.m[v] = true
}
}
for _, v := range *profile {
if !config.m[v] {
return fmt.Sprintf("Unexpected key %s in Set ", v)
}
}
return ""
}
func (config *SetConfig) learnI(valPile ValuePile) {
config.Learn(valPile.(*SetPile))
}
func (config *SetConfig) Learn(pile *SetPile) {
config.List = pile.List
config.m = pile.m
}
func (config *SetConfig) fuseI(otherValConfig ValueConfig) {
config.Fuse(otherValConfig.(*SetConfig))
}
func (config *SetConfig) Fuse(otherConfig *SetConfig) {
if config.List == nil {
config.List = otherConfig.List
config.m = otherConfig.m
return
}
if config.m == nil {
config.m = make(map[string]bool, len(config.List)+len(otherConfig.List))
// Populate the map from the information in List
for _, v := range config.List {
config.m[v] = true
}
}
for _, v := range otherConfig.List {
if !config.m[v] {
config.m[v] = true
config.List = append(config.List, v)
}
}
}