-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathwriter.go
144 lines (131 loc) · 3.29 KB
/
writer.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
// Copyright 2017 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jsonwriter
import (
"bytes"
"errors"
"fmt"
"strconv"
"gopkg.in/yaml.v3"
)
const (
indentation = " "
null = "null"
)
type writer struct {
b bytes.Buffer
}
func (w *writer) bytes() []byte {
return w.b.Bytes()
}
func (w *writer) writeString(s string) {
w.b.Write([]byte(s))
}
func (w *writer) writeMap(node *yaml.Node, indent string) {
if node.Kind == yaml.DocumentNode {
w.writeMap(node.Content[0], indent)
return
}
if node.Kind != yaml.MappingNode {
w.writeString(fmt.Sprintf("invalid node for map: %+v", node))
return
}
w.writeString("{\n")
innerIndent := indent + indentation
for i := 0; i < len(node.Content); i += 2 {
// first print the key
key := node.Content[i].Value
w.writeString(fmt.Sprintf("%s\"%+v\": ", innerIndent, key))
// then the value
value := node.Content[i+1]
switch value.Kind {
case yaml.MappingNode:
w.writeMap(value, innerIndent)
case yaml.SequenceNode:
w.writeSequence(value, innerIndent)
case yaml.ScalarNode:
w.writeScalar(value, innerIndent)
}
if i < len(node.Content)-2 {
w.writeString(",")
}
w.writeString("\n")
}
w.writeString(indent)
w.writeString("}")
}
func (w *writer) writeScalar(node *yaml.Node, indent string) {
if node.Kind != yaml.ScalarNode {
w.writeString(fmt.Sprintf("invalid node for scalar: %+v", node))
return
}
switch node.Tag {
case "!!str":
w.writeString(strconv.Quote(node.Value))
case "!!int":
w.writeString(node.Value)
case "!!float":
w.writeString(node.Value)
case "!!bool":
w.writeString(node.Value)
case "!!null":
w.writeString(null)
}
}
func (w *writer) writeSequence(node *yaml.Node, indent string) {
if node.Kind != yaml.SequenceNode {
w.writeString(fmt.Sprintf("invalid node for sequence: %+v", node))
return
}
w.writeString("[\n")
innerIndent := indent + indentation
for i, value := range node.Content {
w.writeString(innerIndent)
switch value.Kind {
case yaml.MappingNode:
w.writeMap(value, innerIndent)
case yaml.SequenceNode:
w.writeSequence(value, innerIndent)
case yaml.ScalarNode:
w.writeScalar(value, innerIndent)
}
if i < len(node.Content)-1 {
w.writeString(",")
}
w.writeString("\n")
}
w.writeString(indent)
w.writeString("]")
}
// Marshal writes a yaml.Node as JSON
func Marshal(in *yaml.Node) (out []byte, err error) {
var w writer
switch in.Kind {
case yaml.DocumentNode:
w.writeMap(in.Content[0], "")
w.writeString("\n")
case yaml.MappingNode:
w.writeMap(in, "")
w.writeString("\n")
case yaml.SequenceNode:
w.writeSequence(in, "")
w.writeString("\n")
case yaml.ScalarNode:
w.writeScalar(in, "")
w.writeString("\n")
default:
return nil, errors.New("invalid type passed to Marshal")
}
return w.bytes(), err
}