-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtype.go
293 lines (253 loc) · 6.55 KB
/
dtype.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package zarr
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// Dtype is the set of all zarr data types
// Simple data types as a string following the NumPy array protocol type string
// (typestr) format. The format consists of 3 parts:
// * One character describing the byteorder of the data:
// "<": little-endian; ">": big-endian; "|": not-relevant)
// * One character code giving the basic type of the array:
// * "b": Boolean (integer type where all values are only True or False)
// * "i": integer;
// * "u": unsigned integer
// * "f": floating point
// * "c": complex floating point
// * "m": timedelta;
// * "M": datetime
// * "S": string (fixed-length sequence of char)
// * "U": unicode (fixed-length sequence of Py_UNICODE)
// * "V": other (void * – each item is a fixed-size chunk of memory))
// * An integer specifying the number of bytes the type uses.
//
// The byte order is optional in some circumstances, within the zarr format
// byte order MUST be specified
type Dtype struct {
ByteOrder ByteOrder
BasicType BasicType
ByteSize int
Units string
}
var (
_ json.Unmarshaler = (*Dtype)(nil)
_ json.Marshaler = (*Dtype)(nil)
)
func ParseDtype(s string) (dt Dtype, err error) {
// bug in python implementation uses HTML escape sequences when serializaing JSON
s = strings.Replace(s, "<", "<", 1)
s = strings.Replace(s, ">", ">", 1)
if len(s) < 3 {
return dt, fmt.Errorf("invalid Dtype string. %q is too short", s)
}
boByte, s := s[0], s[1:]
dt.ByteOrder, err = ParseByteOrder(rune(boByte))
if err != nil {
return dt, err
}
typeByte, s := s[0], s[1:]
dt.BasicType, err = ParseBasicType(rune(typeByte))
if err != nil {
return dt, err
}
var sizeStr, unitStr string
for i, b := range s {
if b == '[' {
unitStr = s[i:]
break
}
sizeStr += string(b)
}
size, err := strconv.ParseInt(sizeStr, 10, 0)
if err != nil {
return dt, err
}
dt.ByteSize = int(size)
// TODO(b5): validate unit string
dt.Units = unitStr
return dt, nil
}
func (dt Dtype) String() string {
s := fmt.Sprintf("%s%s%d", string(dt.ByteOrder), string(dt.BasicType), dt.ByteSize)
if dt.Units != "" {
s += dt.Units
}
return s
}
func (dt Dtype) MarshalJSON() ([]byte, error) {
return []byte(`"` + dt.String() + `"`), nil
}
func (dt *Dtype) UnmarshalJSON(d []byte) error {
var s string
if err := json.Unmarshal(d, &s); err != nil {
return err
}
t, err := ParseDtype(s)
if err != nil {
return err
}
*dt = t
return nil
}
type ByteOrder rune
func ParseByteOrder(r rune) (ByteOrder, error) {
o := ByteOrder(r)
if _, ok := byteOrders[o]; !ok {
return o, fmt.Errorf("unsupported byte order format: %q", r)
}
return o, nil
}
const (
BONotRelevant ByteOrder = '|'
BOLittleEndian ByteOrder = '<'
BOBigEndian ByteOrder = '>'
)
var byteOrders = map[ByteOrder]struct{}{
BONotRelevant: {},
BOLittleEndian: {},
BOBigEndian: {},
}
type BasicType rune
func ParseBasicType(r rune) (BasicType, error) {
t := BasicType(r)
if _, ok := supportedBasicTypes[t]; !ok {
return t, fmt.Errorf("unsupported byte order format: %q", r)
}
return t, nil
}
func (bt BasicType) Human() string {
return supportedBasicTypes[bt]
}
const (
BTBoolean BasicType = 'b'
BTInteger BasicType = 'i'
BTUnsigned BasicType = 'u'
BTFloatingPoint BasicType = 'f'
BTComplex BasicType = 'c'
BTTimedelta BasicType = 'm'
BTDatetime BasicType = 'M'
BTString BasicType = 'S'
BTUnicode BasicType = 'U'
BTOther BasicType = 'V'
)
// TODO(b5): human names need to be matched to the python implementation?
var supportedBasicTypes = map[BasicType]string{
BTBoolean: "bool",
BTInteger: "int",
BTUnsigned: "uint",
BTFloatingPoint: "float64",
BTComplex: "complex",
BTTimedelta: "timeDelta",
BTDatetime: "dateTime",
BTString: "string",
BTUnicode: "unicode",
BTOther: "other",
}
type StructuredType struct {
Fieldname string
Dtype Dtype
Shape interface{}
Children []StructuredType
}
var (
_ json.Unmarshaler = (*Dtype)(nil)
_ json.Marshaler = (*Dtype)(nil)
)
func ParseStructuredType(d interface{}) (StructuredType, error) {
switch v := d.(type) {
case string:
// string is a Dtype literal
dt, err := ParseDtype(v)
if err != nil {
return StructuredType{}, err
}
return StructuredType{Dtype: dt}, nil
case []interface{}:
return parseStructuredTypeSlice(v)
default:
return StructuredType{}, fmt.Errorf("unexpected type %T", d)
}
}
func parseStructuredTypeSlice(d []interface{}) (StructuredType, error) {
if len(d) == 1 {
childSlice, ok := d[0].([]interface{})
if !ok {
return StructuredType{}, fmt.Errorf("expected single element array to contain an array of structure types")
}
parent := StructuredType{}
for i, el := range childSlice {
ch, err := ParseStructuredType(el)
if err != nil {
return StructuredType{}, fmt.Errorf("element %d: %w", i, err)
}
parent.Children = append(parent.Children, ch)
}
// return StructuredType{}, fmt.Errorf("invalid structured Dtype: %q is too short", string(d))
return parent, nil
} else if len(d) < 2 {
return StructuredType{}, fmt.Errorf("invalid structured Dtype: %q is too short", len(d))
}
t := StructuredType{}
fieldName, ok := d[0].(string)
if !ok {
return StructuredType{}, fmt.Errorf("invalid structured Dtype: field name must be a string. got %T", d[0])
}
t.Fieldname = fieldName
switch x := d[1].(type) {
case string:
dtype, err := ParseDtype(x)
if err != nil {
return StructuredType{}, err
}
t.Dtype = dtype
case []interface{}:
ch, err := ParseStructuredType(x)
if err != nil {
return StructuredType{}, err
}
t.Children = append(t.Children, ch)
default:
return t, fmt.Errorf("invalid structured Dtype: want either string or Structured Type. got %T", d[1])
}
// TODO (b5): shape parsing
if len(d) > 2 {
t.Shape = d[2]
}
return t, nil
}
func (st *StructuredType) IsBasic() bool {
return st.Fieldname == "" && st.Shape == nil
}
func (st *StructuredType) Human() string {
if st.IsBasic() {
return st.Dtype.BasicType.Human()
}
return "struct"
}
func (st *StructuredType) MarshalJSON() ([]byte, error) {
if st.IsBasic() {
return st.Dtype.MarshalJSON()
}
d := []interface{}{
st.Fieldname,
st.Dtype,
}
if st.Shape != nil {
d = append(d, st.Shape)
}
return json.Marshal(d)
}
func (st *StructuredType) UnmarshalJSON(d []byte) error {
var v interface{}
if err := json.Unmarshal(d, &v); err != nil {
return err
}
t, err := ParseStructuredType(v)
if err != nil {
return err
}
*st = t
return nil
}