Repository navigation
Expand file tree
/
Copy pathordered_encode.go
More file actions
126 lines (117 loc) · 3.95 KB
/
Copy pathordered_encode.go
File metadata and controls
126 lines (117 loc) · 3.95 KB
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
package yamledit
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
gyaml "github.com/goccy/go-yaml"
"gopkg.in/yaml.v3"
)
// orderedToYAMLNode converts an interface{} value into a yaml.v3 Node while preserving
// ordering for gyaml.MapSlice (and recursively for nested MapSlices).
func orderedToYAMLNode(v interface{}) *yaml.Node {
switch t := v.(type) {
case nil:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!null", Value: "null"}
case bool:
if t {
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!bool", Value: "true"}
}
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!bool", Value: "false"}
case int:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.Itoa(t)}
case int64:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.FormatInt(t, 10)}
case uint:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.FormatUint(uint64(t), 10)}
case uint8:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.FormatUint(uint64(t), 10)}
case uint16:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.FormatUint(uint64(t), 10)}
case uint32:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.FormatUint(uint64(t), 10)}
case uint64:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.FormatUint(t, 10)}
case float64:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!float", Value: formatYAMLFloat(t)}
case json.Number:
if !isValidJSONNumber(t) {
// json.Number is publicly constructible and may contain arbitrary text.
// Preserve that text as data instead of treating it as trusted YAML syntax.
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: string(t)}
}
tag := "!!int"
if strings.ContainsAny(string(t), ".eE") {
tag = "!!float"
}
node := &yaml.Node{Kind: yaml.ScalarNode, Tag: tag, Value: string(t)}
if !yamlLexemeResolvesAsTag(string(t), tag) {
node.Style |= yaml.TaggedStyle
}
return node
case string:
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: t}
case []interface{}:
seq := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
for _, e := range t {
seq.Content = append(seq.Content, orderedToYAMLNode(e))
}
return seq
case gyaml.MapSlice:
mp := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
for _, it := range t {
ks := fmt.Sprint(it.Key)
mp.Content = append(mp.Content,
&yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: ks},
orderedToYAMLNode(it.Value),
)
}
return mp
// Unordered maps: keep stable ordering (sorted) to avoid nondeterminism.
// (Not ideal, but better than random. Your primary churn bug is MapSlice -> map conversion,
// so this case should be rare in the fallback path.)
case map[string]interface{}:
keys := make([]string, 0, len(t))
for k := range t {
keys = append(keys, k)
}
sort.Strings(keys)
mp := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
for _, k := range keys {
mp.Content = append(mp.Content,
&yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: k},
orderedToYAMLNode(t[k]),
)
}
return mp
default:
// Best-effort scalar string
return &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: fmt.Sprint(t)}
}
}
// encodeNodeLines encodes a yaml node to lines, stripping any leading '---' document marker.
func encodeNodeLines(n *yaml.Node, indent int) ([]string, bool) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(indent)
if err := enc.Encode(n); err != nil {
_ = enc.Close()
return nil, false
}
if err := enc.Close(); err != nil {
return nil, false
}
// Remove only the encoder's document-terminating newline. Additional
// trailing newlines can be significant content in a |+ block scalar.
s := strings.TrimSuffix(buf.String(), "\n")
if s == "" {
return []string{}, true
}
lines := strings.Split(s, "\n")
if len(lines) > 0 && strings.TrimSpace(lines[0]) == "---" {
lines = lines[1:]
}
return lines, true
}