-
Notifications
You must be signed in to change notification settings - Fork 0
/
yml.go
146 lines (123 loc) · 3.2 KB
/
yml.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
// SPDX-FileCopyrightText: 2023 6543 <[email protected]>
// SPDX-License-Identifier: MIT
package yaml2json
import (
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"gopkg.in/yaml.v3"
)
const maxDepth uint8 = 100
var (
ErrBrokenMappingNode = errors.New("broken mapping node")
ErrUnsupportedNode = errors.New("unsupported yaml node")
ErrMaxDepth = errors.New("max depth reached")
)
// Convert YAML bytes to JSON bytes
func Convert(data []byte) ([]byte, error) {
m := &yaml.Node{}
if err := yaml.Unmarshal(data, m); err != nil {
return nil, err
}
return ConvertNode(m)
}
// ConvertNode convert a gopkg.in/yaml.v3 Node to JSON bytes
func ConvertNode(m *yaml.Node) ([]byte, error) {
n, err := resolveMerges(m)
if err != nil {
return nil, err
}
d, err := toJSON(n, 0)
if err != nil {
return nil, err
}
return json.Marshal(d)
}
// StreamConvert convert YAML byte stream to JSON byte stream
func StreamConvert(r io.Reader, w io.Writer) error {
decoder := yaml.NewDecoder(r)
encoder := json.NewEncoder(w)
m := &yaml.Node{}
if err := decoder.Decode(m); err != nil {
return err
}
n, err := resolveMerges(m)
if err != nil {
return err
}
d, err := toJSON(n, 0)
if err != nil {
return err
}
return encoder.Encode(d)
}
// resolveMerges force yaml decoder to resolve map merges
func resolveMerges(m *yaml.Node) (*yaml.Node, error) {
i := new(interface{})
if err := m.Decode(i); err != nil {
return nil, err
}
n := new(yaml.Node)
return n, n.Encode(i)
}
// toJSON convert gopkg.in/yaml.v3 nodes to object that can be serialized as json
// fmt.Sprint() with default formatting is used to convert the key to a string key.
func toJSON(node *yaml.Node, depth uint8) (interface{}, error) {
// prevent loop by hardcoded limit
if depth == maxDepth {
return nil, ErrMaxDepth
}
switch node.Kind {
case yaml.DocumentNode:
return toJSON(node.Content[0], depth+1)
case yaml.SequenceNode:
val := make([]interface{}, len(node.Content))
var err error
for i := range node.Content {
if val[i], err = toJSON(node.Content[i], depth+1); err != nil {
return nil, err
}
}
return val, nil
case yaml.MappingNode:
if (len(node.Content) % 2) != 0 {
return nil, ErrBrokenMappingNode
}
val := make(map[string]interface{}, len(node.Content)%2)
for i := len(node.Content); i > 1; i = i - 2 {
k, err := toJSON(node.Content[i-2], depth+1)
if err != nil {
return nil, err
}
if val[fmt.Sprint(k)], err = toJSON(node.Content[i-1], depth+1); err != nil {
return nil, err
}
}
return val, nil
case yaml.ScalarNode:
switch node.Tag {
case nullTag:
return nil, nil
case boolTag:
return strconv.ParseBool(node.Value)
case intTag:
return strconv.ParseInt(node.Value, 10, 64)
case floatTag:
return strconv.ParseFloat(node.Value, 64)
}
return node.Value, nil
case yaml.AliasNode:
return toJSON(node.Alias, depth+1)
}
return nil, fmt.Errorf("%w: '%v'", ErrUnsupportedNode, node.Kind)
}
// Source: https://github.com/go-yaml/yaml/blob/3e3283e801afc229479d5fc68aa41df1137b8394/resolve.go#L70-L81
const (
nullTag = "!!null"
boolTag = "!!bool"
intTag = "!!int"
floatTag = "!!float"
// mergeTag = "!!merge" // TODO: do we have to parse this?
)