-
Notifications
You must be signed in to change notification settings - Fork 0
/
documentSetting.go
320 lines (254 loc) · 8.92 KB
/
documentSetting.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package atv
import (
"fmt"
"strings"
"github.com/alecthomas/participle/lexer"
)
// documentSetting represents a setting node in an ATV document.
type documentSetting struct {
Pos lexer.Position
Name string `@Ident "="`
SimpleValue *documentSimpleValue `( @@`
TableValue *documentTableValue `| @@`
ValueWithMetadata *documentValueWithMetadata `| @@ )`
}
// Dupe returns a deep copy of the setting.
func (setting *documentSetting) Dupe() *documentSetting {
if setting == nil {
return nil
}
var copy = &documentSetting{Name: setting.Name}
if setting.SimpleValue != nil {
copy.SimpleValue = setting.SimpleValue.Dupe()
} else if setting.ValueWithMetadata != nil {
copy.ValueWithMetadata = setting.ValueWithMetadata.Dupe()
} else if setting.TableValue != nil {
copy.TableValue = setting.TableValue.Dupe()
} else {
panic("Unhandled setting value")
}
return copy
}
// ClearValue sets the value of all setting value types to nil (only one of them should be initialized).
func (setting *documentSetting) ClearValue() {
if setting == nil {
return
}
setting.SimpleValue = nil
setting.ValueWithMetadata = nil
setting.TableValue = nil
}
// String returns the setting as a string.
func (setting *documentSetting) String() string {
if setting == nil {
return "<nil>"
}
builder := strings.Builder{}
err := setting.WriteDocumentPart(&builder, 0)
if err != nil {
return fmt.Sprintf("Error: %s", err)
}
return strings.TrimSpace(builder.String())
}
// mergeInto merges the current setting into the specified document.
func (setting *documentSetting) mergeInto(doc *document) error {
if setting == nil {
return nil
}
if setting.SimpleValue != nil || setting.ValueWithMetadata != nil {
// top level simple value
// => simply overwrite the setting in the document
if err := doc.SetSetting(setting); err != nil {
return err // document seems to contain a value with that name, but a different type (not a simple/complex value)
}
} else if setting.TableValue != nil {
// a table value => merge table settings
if err := doc.MergeTableSetting(setting); err != nil {
return err // document seems to contain a value with that name, but a different type (not a table)
}
} else {
panic("Unhandled setting type")
}
return nil
}
// GetRowReferences returns all row references recursively.
func (setting *documentSetting) GetRowReferences() []RowRef {
if setting != nil {
var allRowRefs []RowRef
parts := []getRowReferences{setting.SimpleValue, setting.ValueWithMetadata, setting.TableValue}
for _, part := range parts {
allRowRefs = append(allRowRefs, part.GetRowReferences()...)
}
return allRowRefs
}
return []RowRef{}
}
// GetRowIDs returns all row ids recursively.
func (setting *documentSetting) GetRowIDs() []RowID {
if setting != nil {
var allRowIDs []RowID
parts := []getRowIDs{setting.SimpleValue, setting.ValueWithMetadata, setting.TableValue}
for _, part := range parts {
allRowIDs = append(allRowIDs, part.GetRowIDs()...)
}
return allRowIDs
}
return []RowID{}
}
// getSetting gets the setting at the specified path.
// If the setting does not exist, nil is returned.
func (setting *documentSetting) getSetting(path documentSettingPath, index int) (*documentSetting, error) {
// abort, if the setting is found
if index == len(path) {
return setting, nil
}
if setting.SimpleValue != nil || setting.ValueWithMetadata != nil {
return nil, fmt.Errorf("Setting '%s' is a single value, but the path '%s' specifies a more nested setting", path[0:index], path)
}
if setting.TableValue != nil {
if path[index].row == nil {
return nil, fmt.Errorf("Setting '%s' is a table value, but the path '%s' does not address a specific row", path[0:index], path)
}
if index+1 == len(path) {
return nil, fmt.Errorf("Setting '%s' is a table value, but the path '%s' does not address a value within a row", path[0:index], path)
}
rowIndex := *path[index].row
if rowIndex >= len(setting.TableValue.Rows) {
return nil, nil
}
row := setting.TableValue.Rows[rowIndex]
for _, item := range row.Items {
if item.Name == *path[index+1].name {
return item.getSetting(path, index+2)
}
}
return nil, nil
}
panic("Unhandled setting type")
}
// createSettingPlaceholder creates all nodes along the specified path and a setting placeholder at the end.
func (setting *documentSetting) createSettingPlaceholder(path documentSettingPath, index int) (*documentSetting, error) {
// abort, if the setting is found
if index == len(path) {
return setting, nil
}
if setting.SimpleValue != nil || setting.ValueWithMetadata != nil {
return nil, fmt.Errorf("Setting '%s' is a single value, but the path '%s' specifies a more nested setting", path[0:index], path)
}
if setting.TableValue != nil {
if path[index].row == nil {
return nil, fmt.Errorf("Setting '%s' is a table value, but the path '%s' does not address a specific row", path[0:index], path)
}
if index+1 == len(path) {
return nil, fmt.Errorf("Setting '%s' is a table value, but the path '%s' does not address a value within a row", path[0:index], path)
}
// add missing table rows, if the index is out of bounds
rowIndex := *path[index].row
if rowIndex >= len(setting.TableValue.Rows) {
for i := rowIndex - len(setting.TableValue.Rows) + 1; i > 0; i-- {
setting.TableValue.Rows = append(setting.TableValue.Rows, &documentTableRow{Items: []*documentSetting{}})
}
}
// try to get existing setting
row := setting.TableValue.Rows[rowIndex]
for _, item := range row.Items {
if item.Name == *path[index+1].name {
return item.createSettingPlaceholder(path, index+2)
}
}
// setting does not exist, yet
// => create node on the way to it
if index+2 == len(path) {
// last setting on the path
newNode := &documentSetting{Name: *path[index+1].name}
row.Items = append(row.Items, newNode)
return newNode, nil
} else {
// a setting on the path to the specified setting, can be a table value only
newNode := &documentSetting{
Name: *path[index+1].name,
TableValue: &documentTableValue{
Rows: []*documentTableRow{
&documentTableRow{Items: []*documentSetting{}}}}}
row.Items = append(row.Items, newNode)
return newNode.createSettingPlaceholder(path, index+2)
}
}
panic("Unhandled setting type")
}
// removeSetting removes the setting at the specified path.
// If the setting does not exist, nil is returned (no error).
func (setting *documentSetting) removeSetting(path documentSettingPath, index int) error {
// abort, if the setting is found
if index == len(path) {
return nil
}
if setting.SimpleValue != nil || setting.ValueWithMetadata != nil {
return fmt.Errorf("Setting '%s' is a single value, but the path '%s' specifies a more nested setting", path[0:index], path)
}
if setting.TableValue != nil {
if path[index].row == nil {
return fmt.Errorf("Setting '%s' is a table value, but the path '%s' does not address a specific row", path[0:index], path)
}
if index+1 == len(path) {
return fmt.Errorf("Setting '%s' is a table value, but the path '%s' does not address a value within a row", path[0:index], path)
}
rowIndex := *path[index].row
if rowIndex >= len(setting.TableValue.Rows) {
return nil
}
row := setting.TableValue.Rows[rowIndex]
for i, item := range row.Items {
if item.Name == *path[index+1].name {
// dive deeper
err := item.removeSetting(path, index+2)
if err != nil {
return err
}
// remove the item, if the appropriate depth is reached
if index+2 == len(path) {
row.Items = append(row.Items[:i], row.Items[i+1:]...)
}
return nil
}
}
return nil
}
panic("Unhandled setting type")
}
// GetValue gets the value of the setting, if the setting is a simple value - with or without metadata.
// If the setting is a row reference, the reference is returned.
// If the setting has a table value, an error is returned.
func (setting *documentSetting) GetValue() (string, error) {
if setting.SimpleValue != nil {
return setting.SimpleValue.Value, nil
}
if setting.ValueWithMetadata != nil {
var value string
if setting.ValueWithMetadata.Data.TryGet("value", &value) {
return value, nil
}
return "", fmt.Errorf("The setting does not contain a value")
}
return "", fmt.Errorf("The setting is not a simple value")
}
// WriteDocumentPart writes a part of the ATV document to the specified writer.
func (setting *documentSetting) WriteDocumentPart(writer *strings.Builder, indent int) error {
// write the name of the setting
line := fmt.Sprintf("%s%s = ", spacer(indent), setting.Name)
_, err := writer.WriteString(line)
if err != nil {
return err
}
// write the setting value
parts := []documentWriter{setting.SimpleValue, setting.ValueWithMetadata, setting.TableValue}
for _, part := range parts {
if !isNil(part) {
err := part.WriteDocumentPart(writer, indent)
if err != nil {
return err
}
}
}
return nil
}