-
Notifications
You must be signed in to change notification settings - Fork 504
/
struct.go
54 lines (45 loc) · 1.08 KB
/
struct.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
package values
import (
"fmt"
"reflect"
"github.com/apache/arrow/go/v16/arrow"
"github.com/apache/arrow/go/v16/arrow/array"
)
func structValue(arr *array.Struct) (any, error) {
arr = sanitizeNested(arr).(*array.Struct)
fields := arr.DataType().(*arrow.StructType).Fields()
columns := make(map[string][]any, len(fields))
for i, field := range fields {
data, err := FromArray(arr.Field(i))
if err != nil {
return nil, err
}
columns[field.Name], err = toSlice(data)
if err != nil {
return nil, err
}
}
rows := make([]map[string]any, arr.Len())
for i := 0; i < arr.Len(); i++ {
row := make(map[string]any, len(fields))
for _, field := range fields {
row[field.Name] = columns[field.Name][i]
}
rows[i] = row
}
return rows, nil
}
func toSlice(data any) ([]any, error) {
val := reflect.ValueOf(data)
if val.Kind() != reflect.Slice {
return nil, fmt.Errorf("expected slice, got %T", data)
}
res := make([]any, val.Len())
for i := 0; i < len(res); i++ {
elem := val.Index(i)
if elem.CanInterface() {
res[i] = elem.Interface()
}
}
return res, nil
}