forked from parquet-go/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn_buffer_proto_purego.go
More file actions
84 lines (73 loc) · 2.04 KB
/
Copy pathcolumn_buffer_proto_purego.go
File metadata and controls
84 lines (73 loc) · 2.04 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
//go:build purego
package parquet
import (
"fmt"
"iter"
"reflect"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
func writeProtoAnyToGroup(msg *anypb.Any, columns []ColumnBuffer, levels columnLevels, writers []fieldWriter, node Node, value *reflect.Value) bool {
if msg == nil {
for i := range writers {
w := &writers[i]
w.writeValue(columns, levels, reflect.Value{})
}
return true
}
typeURL := msg.GetTypeUrl()
const prefix = "type.googleapis.com/"
if !strings.HasPrefix(typeURL, prefix) {
panic(fmt.Sprintf("invalid type_url %q: expected %q prefix", typeURL, prefix))
}
path := typeURL[len(prefix):]
_ = navigateToNestedGroup(node, path)
unmarshaled, err := msg.UnmarshalNew()
if err != nil {
panic(fmt.Sprintf("failed to unmarshal Any: %v", err))
}
*value = reflect.ValueOf(makeNestedMap(path, unmarshaled))
return false
}
func writeProtoMessageToGroup(msg proto.Message, columns []ColumnBuffer, levels columnLevels, writers []fieldWriter) {
msgValue := reflect.ValueOf(msg)
if msgValue.Kind() == reflect.Ptr {
msgValue = msgValue.Elem()
}
for i := range writers {
w := &writers[i]
fieldValue := findFieldByProtoName(msgValue, w.fieldName)
w.writeValue(columns, levels, fieldValue)
}
}
func findFieldByProtoName(structValue reflect.Value, protoName string) reflect.Value {
structType := structValue.Type()
for i := range structType.NumField() {
f := structType.Field(i)
if tag := f.Tag.Get("protobuf"); tag != "" && tag != "-" {
if name := parseProtoNameFromTag(tag); name == protoName {
return structValue.Field(i)
}
}
}
return reflect.Value{}
}
func parseProtoNameFromTag(tag string) string {
for name, value := range parseProtoStructTag(tag) {
if name == "name" {
return value
}
}
return ""
}
func parseProtoStructTag(tag string) iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
for part := range strings.SplitSeq(tag, ",") {
name, value, _ := strings.Cut(part, "=")
if !yield(name, value) {
return
}
}
}
}