/
marshaler.go
171 lines (152 loc) · 3.91 KB
/
marshaler.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package htmlyaml
import (
"bytes"
"errors"
"io"
"sort"
"strconv"
"strings"
)
// Marshaler converts YAML stored as Go `any` object represented into HTML.
// Rendering is customized by providing renderers for specific YAML elements.
// This facilitates CSS styling, CSS animations, and JavaScript events.
// YAML element renderers receive JSON Path and value of element.
// Should be used only for types: bool, float64, string, []any, map[string]any, nil.
// You can get allowed input easily with yaml.Unmarshal or json.Unmarshal to any.
// Since HTML automatically removes whitespace, to make indentation YAML conformant,
// spaces are wrapped in their own div element.
// Safe for repeated use.
// Not safe for concurrent use.
type Marshaler struct {
Null func(key string) string
Bool func(key string, v bool) string
String func(key string, v string) string
Number func(key string, v float64, s string) string
ArrayDash string
ArrayEmpty string
MapColon string
MapEmpty string
MapKey func(key string, v string) string
Row func(s string, padding int) string
*rowWriter
depth int
isParentList bool
isRoot bool
key string
err []error
}
// Marshaler converts YAML stored as Go `any` object represented into HTML.
func (s *Marshaler) Marshal(v any) []byte {
b := bytes.Buffer{}
s.MarshalTo(&b, v)
return b.Bytes()
}
// MarshalTo converts YAML stored as Go `any` object represented into HTML.
func (s *Marshaler) MarshalTo(w io.Writer, v any) error {
s.isRoot = true
s.depth = 0
s.key = "$"
s.rowWriter = &rowWriter{
b: strings.Builder{},
w: w,
row: s.Row,
}
s.marshal(v)
s.flush(s.depth)
s.err = append(s.err, s.rowWriter.err...)
return errors.Join(s.err...)
}
// go-yaml decodes YAML numbers into different numeric Go types,
// as opposed to encoding/json decoding always to float64.
func (s *Marshaler) marshal(v any) {
if v == nil {
s.write(s.Null(s.key))
}
switch q := v.(type) {
case bool:
s.write(s.Bool(s.key, q))
case string:
s.write(s.String(s.key, tryEscapeString(q)))
case int:
s.write(s.Number(s.key, float64(q), strconv.Itoa(q)))
case int64:
s.write(s.Number(s.key, float64(q), strconv.Itoa(int(q))))
case uint:
s.write(s.Number(s.key, float64(q), strconv.Itoa(int(q))))
case uint64:
s.write(s.Number(s.key, float64(q), strconv.Itoa(int(q))))
case float32:
s.write(s.Number(s.key, float64(q), strconv.FormatFloat(float64(q), 'f', -1, 32)))
case float64:
s.write(s.Number(s.key, q, strconv.FormatFloat(q, 'f', -1, 64)))
case map[string]any:
s.encodeMap(q)
return
case []any:
s.encodeArray(q)
return
default:
s.err = append(s.err, errors.New("skip unsupported type at key("+s.key+")"))
}
s.flush(s.depth)
}
func (s *Marshaler) encodeArray(v []any) {
if len(v) == 0 {
s.write(s.ArrayEmpty)
s.flush(s.depth)
return
}
// write array
k, d, pl := s.key, s.depth, s.isParentList
defer func() { s.key, s.depth, s.isParentList = k, d, pl }()
s.flush(s.depth)
if s.isRoot {
s.isRoot = false
s.depth = 0
} else {
s.depth = d + 1
}
s.isParentList = true
for i, q := range v {
s.key = k + "[" + strconv.Itoa(i) + "]"
s.write(s.ArrayDash)
s.marshal(q)
}
}
func (s *Marshaler) encodeMap(v map[string]any) {
if len(v) == 0 {
s.write(s.MapEmpty)
s.flush(s.depth)
return
}
// extract and sort the keys
type kv struct {
k string
v any
}
sv := make([]kv, 0, len(v))
for k, v := range v {
sv = append(sv, kv{k: k, v: v})
}
sort.Slice(sv, func(i, j int) bool { return sv[i].k < sv[j].k })
// write map
k, d, r := s.key, s.depth, s.isRoot
defer func() { s.key, s.depth = k, d }()
if !s.isParentList {
s.flush(s.depth)
}
if s.isRoot {
s.isRoot = false
}
for i, kv := range sv {
if !r && ((s.isParentList && i > 0) || !s.isParentList) {
s.depth = d + 1
}
// key
s.key = k + "." + kv.k
s.write(s.MapKey(s.key, kv.k))
s.write(s.MapColon)
// value
s.marshal(kv.v)
}
}