-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.go
171 lines (142 loc) · 3.82 KB
/
common.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 swid
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"reflect"
"strconv"
)
type codeDictionary map[uint64]string
type stringDictionary map[string]uint64
func stringifyCode(v *interface{}, dict codeDictionary, codeName string) error {
switch t := (*v).(type) {
case string:
*v = t
return nil
case uint64:
if s, ok := dict[t]; ok {
*v = s
} else if codeName != "" {
*v = fmt.Sprintf("%s(%d)", codeName, t)
}
return nil
default:
return fmt.Errorf("unhandled type: %T", t)
}
}
func codifyString(v *interface{}, dict stringDictionary) error {
switch t := (*v).(type) {
case string:
// try mapping to code and replace if successful
if ui, ok := dict[t]; ok {
*v = ui
}
return nil
case uint64:
return nil
case float64:
// check that the JSON number is integer (i.e., no fraction / exponent)
// if so, convert and replace
if t == float64(uint64(t)) {
*v = uint64(t)
return nil
}
return fmt.Errorf("number %s is not uint64", strconv.FormatFloat(t, 'f', -1, 64))
default:
return fmt.Errorf("unhandled type: %T", t)
}
}
func isStringOrCode(v interface{}, codeName string) error {
switch t := v.(type) {
case uint64:
return nil
case string:
return nil
default:
return fmt.Errorf("%s MUST be uint64 or string; got %T", codeName, t)
}
}
func codeStringer(code interface{}, dict codeDictionary, codeName string) string {
v := code
if err := stringifyCode(&v, dict, codeName); err != nil {
return ""
}
return v.(string)
}
func codeToCBOR(code interface{}, dict stringDictionary) ([]byte, error) {
v := code
// always try to minimize bandwidth
if err := codifyString(&v, dict); err != nil {
return nil, err
}
return em.Marshal(v)
}
func codeToJSON(code interface{}, dict codeDictionary) ([]byte, error) {
v := code // make a copy we can clobber
// always try to maximize expressiveness
// however, avoid encoding unknown codes
if err := stringifyCode(&v, dict, ""); err != nil {
return nil, err
}
return json.Marshal(v)
}
func codeToXMLAttr(attrName xml.Name, code interface{}, dict codeDictionary) (xml.Attr, error) {
v := code // make a copy we can clobber
// always try to maximize expressiveness
// however, avoid encoding unknown codes
if err := stringifyCode(&v, dict, ""); err != nil {
return xml.Attr{}, err
}
return xml.Attr{Name: attrName, Value: v.(string)}, nil
}
type encoder func([]byte, interface{}) error
func xToCode(enc encoder, from []byte, dict stringDictionary, to *interface{}) error {
if err := enc(from, to); err != nil {
return err
}
// try to make internal representation as homogeneous as possible
if err := codifyString(to, dict); err != nil {
return err
}
return nil
}
func cborToCode(from []byte, dict stringDictionary, to *interface{}) error {
return xToCode(dm.Unmarshal, from, dict, to)
}
func jsonToCode(from []byte, dict stringDictionary, to *interface{}) error {
return xToCode(json.Unmarshal, from, dict, to)
}
func xmlAttrToCode(from xml.Attr, dict stringDictionary, to *interface{}) error {
*to = from.Value
if err := codifyString(to, dict); err != nil {
return err
}
return nil
}
func arrayToCBOR(a reflect.Value) ([]byte, error) {
switch a.Kind() {
case reflect.Array, reflect.Slice:
default:
return nil, errors.New("expecting array or slice")
}
switch a.Len() {
case 0:
return nil, errors.New("array MUST NOT be 0-length")
case 1:
return em.Marshal(a.Index(0).Interface())
default:
return em.Marshal(
// this slight contortion is done to handle conversion from e.g.,
// Processes to []Process. it is needed to steer the (e.g.,)
// Processes marshaler to the array of Process marshaler -- if we
// don't do that, we'd re-enter this same function and be trapped
// in here until the stack blows up.
a.Convert(
reflect.SliceOf(
a.Index(0).Type(),
),
).Interface(),
)
}
}