-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterverse.go
270 lines (233 loc) · 7.03 KB
/
interverse.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"bytes"
"fmt"
"sort"
"strings"
"text/tabwriter"
)
// Interverse holds all data and logic to convert the data from
// one alterverse to another alterverse.
type Interverse struct {
lt lookupTable
}
// NewInterverse takes two manifests, builds a lookup table, sorts
// this table (long values of the 'source' alterverse must come first
// to ensure proper string substitution) and returns a ready to use
// Interverse.
func NewInterverse(from, to Manifest) (*Interverse, error) {
i := &Interverse{}
lt, err := newLookupTable(from, to)
// the reverse sort is important: it ensures that long strings are replaced
// first so shorter strings which are substrings of the longer ones do not
// interfer with those.
sort.Sort(sort.Reverse(lt))
i.lt = lt
return i, err
}
// Deduce performs the actual string substitution using the lookup table.
// This is done using the Tokenizer. Deduce can produce an alterverse that
// cannot be converted back to its source alterverse. To avoid this make
// use of the DeduceStrict method.
func (t Interverse) Deduce(in map[string][]byte) map[string][]byte {
out := map[string][]byte{}
for k, v := range in {
tokenizer := NewTokenizer(v)
for _, lr := range t.lt {
st := switchToken{A: lr.From, B: lr.To}
tokenizer.Tokenize(st)
}
out[k] = tokenizer.Mutate()
}
return out
}
// DeduceStrict performs the actual string substitution using the lookup table.
// This is done using the Tokenizer. DeduceStrict produces alterverses that
// can be converted back to its source alterverse but has a huge overhead compared
// to the Deduce method.
func (t Interverse) DeduceStrict(in map[string][]byte) (map[string][]byte, []error) {
tokenizers := map[string]Tokenizer{}
for k, v := range in {
tokenizer := NewTokenizer(v)
for _, lr := range t.lt {
st := switchToken{A: lr.From, B: lr.To}
tokenizer.Tokenize(st)
}
tokenizers[k] = tokenizer
}
out := map[string][]byte{}
for k, v := range tokenizers {
out[k] = v.Mutate()
}
errs := []error{}
for k, v := range tokenizers {
for _, lr := range t.lt {
if v.Contains([]byte(lr.To)) {
errs = append(errs, fmt.Errorf("file '%s' contains the string '%s' which is "+
"the value of the manifest key '%s' of the destination alterverse", k, lr.To, lr.Name))
}
}
}
if len(errs) > 0 {
return out, errs
}
reverse := map[string][]byte{}
for k, v := range out {
tokenizer := NewTokenizer(v)
for _, lr := range t.lt {
st := switchToken{A: lr.To, B: lr.From}
tokenizer.Tokenize(st)
}
reverse[k] = tokenizer.Mutate()
}
for k := range in {
if !bytes.Equal(in[k], reverse[k]) {
errs = append(errs, fmt.Errorf("full monty failed for '%s'", k))
}
}
return out, errs
}
type lookupRecord struct {
From string
To string
Name string
}
type lookupTable []*lookupRecord
func newLookupTable(from, to map[string]string) (lookupTable, error) {
lt := []*lookupRecord{}
if ok, missing := haveSameKeys(from, to); !ok {
return lookupTable(lt), fmt.Errorf("the following keys are missing: %s", strings.Join(missing, ", "))
}
for k := range from {
if from[k] == "" {
return lookupTable(lt), fmt.Errorf("key '%s' in 'from' manifest must not be empty", k)
}
if to[k] == "" {
return lookupTable(lt), fmt.Errorf("key '%s' in 'to' manifest must not be empty", k)
}
lr := &lookupRecord{
From: from[k],
To: to[k],
Name: k,
}
lt = append(lt, lr)
}
return lookupTable(lt), nil
}
// haveSameKeys checks two maps a and b if all keys present in a are also
// present in b (not vice versa!). A list of missing keys is returned as second
// return value.
func haveSameKeys(a, b map[string]string) (bool, []string) {
missing := []string{}
for k := range a {
if _, ok := b[k]; !ok {
missing = append(missing, k)
}
}
if len(missing) > 0 {
return false, missing
}
return true, missing
}
func (lt lookupTable) Len() int { return len(lt) }
func (lt lookupTable) Less(i, j int) bool { return len(lt[i].From) < len(lt[j].From) }
func (lt lookupTable) Swap(i, j int) { lt[i], lt[j] = lt[j], lt[i] }
func (lt lookupTable) dump() string {
var out bytes.Buffer
w := tabwriter.NewWriter(&out, 0, 0, 1, ' ', tabwriter.Debug)
fmt.Fprintln(w, "name\tfrom\tto")
for _, record := range lt {
fmt.Fprintf(w, "'%s'\t'%s'\t'%s'\n", record.Name, record.From, record.To)
}
w.Flush()
return out.String()
}
// Tokenizer allow no take a split a byte slice and split it into tokens where
// token is an interface. This allows to replace parts of a byte slice with elements
// of the lookup table
type Tokenizer struct {
tokens []token
}
func NewTokenizer(rawBytes []byte) Tokenizer {
return Tokenizer{tokens: []token{byteToken(rawBytes)}}
}
func (t *Tokenizer) Tokenize(by token) {
tmp := []token{}
for _, token := range t.tokens {
tmp = append(tmp, token.tokenize(by)...)
}
t.tokens = tmp
}
func (t *Tokenizer) Raw() []byte {
out := []byte{}
for _, token := range t.tokens {
out = append(out, token.raw()...)
}
return out
}
func (t *Tokenizer) Mutate() []byte {
out := []byte{}
for _, token := range t.tokens {
out = append(out, token.mutate()...)
}
return out
}
func (t *Tokenizer) Contains(b []byte) bool {
for _, token := range t.tokens {
if token.contains(b) {
return true
}
}
return false
}
func (t *Tokenizer) Dump() string {
out := ""
for _, token := range t.tokens {
out = fmt.Sprintf("%s'%s' is of kind %s\n", out, string(token.raw()), token.kind())
}
return out
}
type token interface {
tokenize(token) []token
raw() []byte
kind() string
mutate() []byte
contains([]byte) bool
}
type byteToken []byte
func (bt byteToken) tokenize(by token) []token {
split := bytes.SplitN(bt, by.raw(), 2)
noMatch := len(split) == 1
endsWithMatch := len(split) == 2 && len(split[0]) > 0 && len(split[1]) == 0
startsWithMatch := len(split) == 2 && len(split[0]) == 0 && len(split[1]) > 0
innerMatch := len(split) == 2 && len(split[0]) > 0 && len(split[1]) > 0
fullMatch := len(split) == 2 && len(split[0]) == 0 && len(split[1]) == 0
var out []token
if noMatch {
out = []token{bt}
} else if endsWithMatch {
out = []token{byteToken(split[0]), by}
} else if startsWithMatch {
next := byteToken(split[1])
out = append([]token{by}, next.tokenize(by)...)
} else if innerMatch {
next := byteToken(split[1])
out = append([]token{byteToken(split[0]), by}, next.tokenize(by)...)
} else if fullMatch {
out = []token{by}
}
return out
}
func (bt byteToken) contains(b []byte) bool { return bytes.Contains(bt, b) }
func (bt byteToken) raw() []byte { return []byte(bt) }
func (bt byteToken) mutate() []byte { return bt.raw() }
func (bt byteToken) kind() string { return "byteToken" }
type switchToken struct {
A string
B string
}
func (st switchToken) tokenize(by token) []token { return []token{st} }
func (st switchToken) contains(b []byte) bool { return false }
func (st switchToken) raw() []byte { return []byte(st.A) }
func (st switchToken) mutate() []byte { return []byte(st.B) }
func (st switchToken) kind() string { return "switchToken" }