-
Notifications
You must be signed in to change notification settings - Fork 0
/
documentValueWithMetadata.go
79 lines (63 loc) · 1.79 KB
/
documentValueWithMetadata.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
package atv
import (
"fmt"
"strings"
"github.com/alecthomas/participle/lexer"
)
// documentValueWithMetadata represents a value with some metadata attached to it in an ATV document.
type documentValueWithMetadata struct {
Pos lexer.Position
Data dictionary `"{" @@* "}"`
}
// Dupe returns a copy of the value.
func (value *documentValueWithMetadata) Dupe() *documentValueWithMetadata {
if value == nil {
return nil
}
return &documentValueWithMetadata{
Data: value.Data,
}
}
// GetRowReferences returns all row references recursively.
func (value *documentValueWithMetadata) GetRowReferences() []RowRef {
if value != nil {
var rowref string
if value.Data.TryGet("rowref", &rowref) {
return []RowRef{RowRef(rowref)}
}
}
return []RowRef{}
}
// GetRowIDs returns all row ids recursively.
func (value *documentValueWithMetadata) GetRowIDs() []RowID {
return []RowID{}
}
// WriteDocumentPart writes a part of the ATV document to the specified writer.
func (value *documentValueWithMetadata) WriteDocumentPart(writer *strings.Builder, indent int) error {
// write opening brace of the complex type
_, err := writer.WriteString("{\n")
if err != nil {
return err
}
// write key-value-pairs forming the value
for _, item := range value.Data {
line := fmt.Sprintf("%s%s = \"%s\"\n", spacer(indent+1), item.Key, item.Value)
_, err := writer.WriteString(line)
if err != nil {
return err
}
}
// write closing brace of the complex type
line := fmt.Sprintf("%s}\n", spacer(indent))
_, err = writer.WriteString(line)
if err != nil {
return err
}
return err
}
// String returns the complex value as a string.
func (value *documentValueWithMetadata) String() string {
builder := strings.Builder{}
value.WriteDocumentPart(&builder, 0)
return strings.TrimSpace(builder.String())
}