-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
148 lines (131 loc) · 4.35 KB
/
yaml.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 config
import (
"bytes"
"sort"
"strings"
"github.com/mitchellh/go-wordwrap"
"gitlab.com/tozd/go/errors"
"gopkg.in/yaml.v3"
)
const (
maxCommentWidth = 80
yamlIndent = 2
)
// formatDescriptions formats descriptions to be used a comment block before a
// sequence of objects in YAML. The comment block describes fields of those
// objects.
func formatDescriptions(descriptions map[string]string) string {
keys := []string{}
for key := range descriptions {
keys = append(keys, key)
}
sort.Strings(keys)
output := ""
for _, key := range keys {
description := key + ": " + descriptions[key] + "\n"
output += wordwrap.WrapString(description, maxCommentWidth)
}
return output
}
// toConfigurationYAML returns configuration as YAML.
//
// YAML contains configuration comments.
func toConfigurationYAML(configuration *Configuration) ([]byte, errors.E) {
var node yaml.Node
err := (&node).Encode(configuration)
if err != nil {
return nil, errors.WithMessage(err, "cannot encode configuration")
}
return toYAML(&node)
}
// setYAMLComments modifies YAML node by moving comments in children nodes which have
// "comment:" prefix in object field names to corresponding data fields (and their nodes).
func setYAMLComments(node *yaml.Node) {
if node.Kind == yaml.SequenceNode {
// We first extract all comments.
comments := map[int]string{}
contentsToDelete := []int{}
for i := 0; i < len(node.Content); i++ {
key := node.Content[i].Value
if strings.HasPrefix(key, "comment:") {
contentsToDelete = append(contentsToDelete, i)
// We set it at the index it will be after we delete comments from Content.
// If there are multiple comments one immediately after the other,
// then only the last one is kept.
comments[i+1-len(contentsToDelete)] = strings.TrimPrefix(key, "comment:")
}
}
// We iterate in the reverse order.
for i := len(contentsToDelete) - 1; i >= 0; i-- {
k := contentsToDelete[i]
// Remove one content node after the other.
node.Content = append(node.Content[:k], node.Content[k+1:]...)
}
// Finally set comments.
for i := 0; i < len(node.Content); i++ {
comment, ok := comments[i]
// Only if there is a comment and another comment is not already set.
if ok && comment != "" && node.Content[i].HeadComment == "" {
node.Content[i].HeadComment = wordwrap.WrapString(comment, maxCommentWidth)
}
// And recurse at the same time.
setYAMLComments(node.Content[i])
}
} else if node.Kind == yaml.MappingNode {
// We first extract all comments.
comments := map[string]string{}
contentsToDelete := []int{}
for i := 0; i < len(node.Content); i += 2 {
key := node.Content[i].Value
if strings.HasPrefix(key, "comment:") {
contentsToDelete = append(contentsToDelete, i, i+1)
comments[strings.TrimPrefix(key, "comment:")] = node.Content[i+1].Value
}
}
// We iterate in the reverse order.
for i := len(contentsToDelete) - 1; i >= 0; i-- {
k := contentsToDelete[i]
// Remove one content node after the other.
node.Content = append(node.Content[:k], node.Content[k+1:]...)
}
// Finally set comments.
for i := 0; i < len(node.Content); i += 2 {
key := node.Content[i].Value
comment, ok := comments[key]
// Only if there is a comment and another comment is not already set.
if ok && comment != "" && node.Content[i].HeadComment == "" {
node.Content[i].HeadComment = wordwrap.WrapString(comment, maxCommentWidth)
}
// And recurse at the same time.
setYAMLComments(node.Content[i+1])
}
// Set comment for the node itself.
comment, ok := comments[""]
// Only if there is a comment and another comment is not already set.
if ok && comment != "" && node.HeadComment == "" {
node.HeadComment = wordwrap.WrapString(comment, maxCommentWidth)
}
} else {
for _, content := range node.Content {
setYAMLComments(content)
}
}
}
// toYAML converts YAML node to bytes.
//
// Comments in the YAML node are converted as well.
func toYAML(node *yaml.Node) ([]byte, errors.E) {
setYAMLComments(node)
buffer := bytes.Buffer{}
encoder := yaml.NewEncoder(&buffer)
encoder.SetIndent(yamlIndent)
err := encoder.Encode(node)
if err != nil {
return nil, errors.WithMessage(err, "cannot marshal configuration")
}
err = encoder.Close()
if err != nil {
return nil, errors.WithMessage(err, "cannot marshal configuration")
}
return buffer.Bytes(), nil
}