-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite_generic_list.go
More file actions
106 lines (92 loc) · 2.94 KB
/
composite_generic_list.go
File metadata and controls
106 lines (92 loc) · 2.94 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
97
98
99
100
101
102
103
104
105
106
package protopath
import (
"strconv"
"github.com/daishe/protoreflectextra"
"google.golang.org/protobuf/reflect/protoreflect"
)
// listIndex parses the provided name as a list index. It also reports if the provided name was parsed successfully. In case of parsing failure, it returns 0 and false.
func listIndex(name string) (int, bool) {
i, err := strconv.ParseInt(name, 10, 0)
if err != nil {
return 0, false
}
return int(i), true
}
// listIndexNormalized normalizes the provided list index, by ensuring it will have a non negative value assuming it points to an element in a list with the given length. It reports if the provided index was normalized successfully. In case of failure, it returns 0 and false.
func listIndexNormalized(len, index int) (int, bool) {
if index < -len || index >= len {
return 0, false
}
if index < 0 {
index = len + index
}
return index, true
}
func listIndexInListOrError(list protoreflectextra.List, name string) (int, error) {
if !list.IsMutable() {
return 0, &ErrNotFound{Kind: "index", Value: name}
}
index, ok := listIndex(name)
if !ok {
return 0, &ErrNotFound{Kind: "index", Value: name}
}
index, ok = listIndexNormalized(list.Len(), index)
if !ok {
return 0, &ErrNotFound{Kind: "index", Value: name}
}
return index, nil
}
func interfaceOfListItem(field protoreflect.FieldDescriptor, value protoreflect.Value) any {
if field.Kind() == protoreflect.EnumKind {
return protoreflectextra.NewEnum(field.Enum(), value.Enum())
}
return value.Interface()
}
type genericListComposite struct {
List protoreflectextra.List
}
func newGenericListComposite(li protoreflectextra.List) genericListComposite {
return genericListComposite{List: li}
}
func (c genericListComposite) IsReadOnly() bool {
return !c.List.IsMutable()
}
func (c genericListComposite) Self() any {
return c.List
}
func (c genericListComposite) Get(s Segment) (any, error) {
idx, err := listIndexInListOrError(c.List, s.Value())
if err != nil {
return nil, err
}
return interfaceOfListItem(c.List.Descriptor().ListElement(), c.List.Get(idx)), nil
}
func (c genericListComposite) Mutable(s Segment) (any, error) {
if !c.List.IsMutable() {
return nil, ErrMutationOfReadOnlyValue
}
return c.Get(s)
}
func (c genericListComposite) Access(s Segment) (any, error) {
idx, err := listIndexInListOrError(c.List, s.Value())
if err != nil {
return nil, err
}
if c.List.Descriptor().ListElement().Kind() != protoreflect.MessageKind {
return nil, newErrInSegment(s, ErrAccessToNonCompositeType)
}
return c.List.Get(idx).Message(), nil
}
func (c genericListComposite) AccessMutable(s Segment) (any, error) {
if !c.List.IsMutable() {
return nil, ErrMutationOfReadOnlyValue
}
idx, err := listIndexInListOrError(c.List, s.Value())
if err != nil {
return nil, err
}
if c.List.Descriptor().ListElement().Kind() != protoreflect.MessageKind {
return nil, newErrInSegment(s, ErrAccessToNonCompositeType)
}
return c.List.Get(idx).Message(), nil
}