/
file_format.go
148 lines (134 loc) · 3.62 KB
/
file_format.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
package internal
import (
"fmt"
"path"
"strings"
"gopkg.in/yaml.v3"
)
type FileFormat interface {
ExtractAnnotations(all []string) ([]FileFormatAnnotation, error)
ReadValue(lines []string, lineNum int) (string, error)
WriteValue(lines []string, lineNum int, value string) error
}
type FileFormatAnnotation struct {
LineNum int
AnnotationRaw string
}
func GuessFileFormatFromExtension(file string) (FileFormat, error) {
ext := path.Ext(file)
switch ext {
case ".yml", ".yaml":
format := &YamlFileFormat{}
return format, nil
default:
return nil, fmt.Errorf("unsupported file extension %s", ext)
}
}
var _ FileFormat = (*YamlFileFormat)(nil)
type YamlFileFormat struct{}
func (f YamlFileFormat) ExtractAnnotations(lines []string) ([]FileFormatAnnotation, error) {
documentsLines := [][]string{{}}
for i := 0; i < len(lines); i++ {
if strings.HasPrefix(lines[i], "---") {
documentsLines = append(documentsLines, []string{})
} else {
documentsLines[len(documentsLines)-1] = append(documentsLines[len(documentsLines)-1], lines[i])
}
}
result := []FileFormatAnnotation{}
firstDocumentLine := 0
for _, documentLines := range documentsLines {
documentNode := &yaml.Node{}
if err := yaml.Unmarshal([]byte(strings.Join(documentLines, "\n")), documentNode); err != nil {
return result, err
}
if err := visitYaml(documentNode, func(node *yaml.Node) error {
if node.Tag == "!!str" && (node.Style == 0 || node.Style == yaml.SingleQuotedStyle || node.Style == yaml.DoubleQuotedStyle) && node.LineComment != "" {
result = append(result, FileFormatAnnotation{
LineNum: firstDocumentLine + node.Line,
AnnotationRaw: strings.TrimLeft(node.LineComment, "# "),
})
}
return nil
}); err != nil {
return result, err
}
firstDocumentLine = firstDocumentLine + len(documentLines) + 1
}
return result, nil
}
func (f YamlFileFormat) ReadValue(lines []string, lineNum int) (string, error) {
line := lines[lineNum-1]
_, rest := separateLeadingWhitspaces(line)
node := &yaml.Node{}
if err := yaml.Unmarshal([]byte(rest), node); err != nil {
return "", err
}
value := ""
if err := visitYaml(node, func(node *yaml.Node) error {
value = node.Value
return nil
}); err != nil {
return "", err
}
return value, nil
}
func (f YamlFileFormat) WriteValue(lines []string, lineNum int, value string) error {
line := lines[lineNum-1]
lws, rest := separateLeadingWhitspaces(line)
node := &yaml.Node{}
if err := yaml.Unmarshal([]byte(rest), node); err != nil {
return err
}
if err := visitYaml(node, func(node *yaml.Node) error {
node.Value = value
return nil
}); err != nil {
return err
}
output, err := yaml.Marshal(node)
if err != nil {
return err
}
lines[lineNum-1] = lws + strings.TrimSuffix(string(output), "\n")
return nil
}
func separateLeadingWhitspaces(str string) (string, string) {
for i := 0; i < len(str); i++ {
if str[i] != ' ' && str[i] != '\t' {
return str[0:i], str[i:]
}
}
return str, ""
}
func visitYaml(node *yaml.Node, fn func(node *yaml.Node) error) error {
switch node.Kind {
case yaml.DocumentNode:
for _, node := range node.Content {
if err := visitYaml(node, fn); err != nil {
return err
}
}
return nil
case yaml.SequenceNode:
for _, node := range node.Content {
if err := visitYaml(node, fn); err != nil {
return err
}
}
return nil
case yaml.MappingNode:
for i := 0; i < len(node.Content); i += 2 {
// keyNode := node.Content[i]
valueNode := node.Content[i+1]
if err := visitYaml(valueNode, fn); err != nil {
return err
}
}
return nil
case yaml.ScalarNode:
return fn(node)
default:
return nil
}
}