-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuilder.go
More file actions
96 lines (81 loc) · 1.87 KB
/
Copy pathbuilder.go
File metadata and controls
96 lines (81 loc) · 1.87 KB
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
package geoarrow
import (
"bytes"
"fmt"
"github.com/apache/arrow-go/v18/arrow/array"
json "github.com/goccy/go-json"
)
type valueBuilder[V GeometryValue, G GeometryType[V]] struct {
*array.ExtensionBuilder
}
var (
_ array.Builder = (*valueBuilder[WKBBytes, *WKBType])(nil)
_ array.Builder = (*valueBuilder[PointValue, *PointType])(nil)
)
func (b *valueBuilder[V, G]) Append(v V) {
b.AppendValue(v)
}
func (b *valueBuilder[V, G]) AppendValue(v V) {
geomType := b.Type().(G)
geomType.appendValueToBuilder(b.Builder, v)
}
func (b *valueBuilder[V, G]) AppendValues(v []V, valid []bool) {
if len(v) != len(valid) && len(valid) != 0 {
panic("len(v) != len(valid) && len(valid) != 0")
}
for i, val := range v {
if len(valid) > 0 && !valid[i] {
b.AppendNull()
} else {
b.AppendValue(val)
}
}
}
func (b *valueBuilder[V, G]) AppendNull() {
b.Builder.AppendNull()
}
func (b *valueBuilder[V, G]) AppendValueFromString(s string) error {
if s == array.NullValueStr {
b.AppendNull()
return nil
}
geomType := b.Type().(G)
v, err := geomType.valueFromString(s)
if err != nil {
return err
}
b.AppendValue(v)
return nil
}
func (b *valueBuilder[V, G]) UnmarshalOne(dec *json.Decoder) error {
geomType := b.Type().(G)
v, isNull, err := geomType.unmarshalJSONOne(dec)
if err != nil {
return err
}
if isNull {
b.AppendNull()
return nil
}
b.AppendValue(v)
return nil
}
func (b *valueBuilder[V, G]) Unmarshal(dec *json.Decoder) error {
for dec.More() {
if err := b.UnmarshalOne(dec); err != nil {
return err
}
}
return nil
}
func (b *valueBuilder[V, G]) UnmarshalJSON(data []byte) error {
dec := json.NewDecoder(bytes.NewReader(data))
t, err := dec.Token()
if err != nil {
return err
}
if delim, ok := t.(json.Delim); !ok || delim != '[' {
return fmt.Errorf("geoarrow builder must unpack from json array, found %s", delim)
}
return b.Unmarshal(dec)
}