-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml2cbor.go
157 lines (136 loc) · 2.92 KB
/
yaml2cbor.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
// Copyright 2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"github.com/fxamacker/cbor/v2"
"github.com/setrofim/yaml"
)
var em, emErr = cbor.EncOptions{
IndefLength: cbor.IndefLengthForbidden,
TimeTag: cbor.EncTagRequired,
}.EncMode()
func retag(v interface{}) (interface{}, bool) {
m, ok := v.(map[string]interface{})
if !ok {
return v, false
}
if len(m) != 2 {
return v, false
}
tag, ok := m["tag"]
if !ok {
return v, false
}
value, ok := m["value"]
if !ok {
return v, false
}
return cbor.Tag{
Number: uint64(tag.(int)),
Content: value,
}, true
}
func retagRecursively(v interface{}) interface{} {
out, retagged := retag(v)
if retagged {
tag := out.(cbor.Tag)
tag.Content = retagRecursively(tag.Content)
return tag
}
switch t := v.(type) {
case map[interface{}]interface{}:
updated := make(map[interface{}]interface{})
for key, val := range t {
updated[key] = retagRecursively(val)
}
return updated
case map[string]interface{}:
updated := make(map[string]interface{})
for key, val := range t {
updated[key] = retagRecursively(val)
}
return updated
case []interface{}:
var updated []interface{}
for _, val := range t {
updated = append(updated, retagRecursively(val))
}
return updated
default:
return v
}
}
func encodeCBOR(v interface{}) (interface{}, bool, error) {
m, ok := v.(map[string]interface{})
if !ok {
return v, false, nil
}
if len(m) != 1 {
return v, false, nil
}
toEncode, ok := m["encodedCBOR"]
if !ok {
return v, false, nil
}
out, err := em.Marshal(toEncode)
return out, true, err
}
func encodeCBORRecursively(v interface{}) (interface{}, error) {
out, encoded, err := encodeCBOR(v)
if err != nil {
return nil, err
}
if encoded {
return out, nil
}
switch t := v.(type) {
case map[interface{}]interface{}:
updated := make(map[interface{}]interface{})
for key, val := range t {
updated[key], err = encodeCBORRecursively(val)
if err != nil {
return nil, err
}
}
return updated, nil
case map[string]interface{}:
updated := make(map[string]interface{})
for key, val := range t {
updated[key], err = encodeCBORRecursively(val)
if err != nil {
return nil, err
}
}
return updated, nil
case []interface{}:
var updated []interface{}
for _, val := range t {
encodedVal, err := encodeCBORRecursively(val)
if err != nil {
return nil, err
}
updated = append(updated, encodedVal)
}
return updated, nil
default:
return v, nil
}
}
func yaml2cbor(data []byte) ([]byte, error) {
m := make(map[interface{}]interface{})
err := yaml.Unmarshal([]byte(data), &m)
if err != nil {
return nil, err
}
retagged := retagRecursively(m)
out, err := encodeCBORRecursively(retagged)
if err != nil {
return nil, err
}
return em.Marshal(out)
}
func init() {
if emErr != nil {
panic(emErr)
}
}