-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite_structvalue.go
More file actions
67 lines (56 loc) · 1.73 KB
/
composite_structvalue.go
File metadata and controls
67 lines (56 loc) · 1.73 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
package protopath
import (
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/daishe/protopath/internal/genid"
)
type structComposite struct {
struct_ protoreflect.Message
fields protoreflect.Map
}
func newStructComposite(m protoreflect.Message) *structComposite {
desc := m.Descriptor()
if desc.FullName() != genid.Struct_message_fullname {
panic("protopath: not an instance of " + genid.Struct_message_fullname)
}
if !m.IsValid() {
ma := m.Get(desc.Fields().ByNumber(genid.Struct_Fields_field_number)).Map()
return &structComposite{struct_: m, fields: ma}
}
ma := m.Mutable(desc.Fields().ByNumber(genid.Struct_Fields_field_number)).Map()
return &structComposite{struct_: m, fields: ma}
}
func (c *structComposite) Struct() protoreflect.Message {
return c.struct_
}
func (c *structComposite) IsReadOnly() bool {
return !c.struct_.IsValid() || !c.fields.IsValid()
}
func (c *structComposite) Self() any {
return c.struct_
}
func (c *structComposite) Get(s Segment) (any, error) {
segmentValue := s.Value()
if !c.struct_.IsValid() || !c.fields.IsValid() {
return nil, &ErrNotFound{Kind: "key", Value: segmentValue}
}
v := c.fields.Get(protoreflect.ValueOfString(segmentValue).MapKey())
if !v.IsValid() {
return nil, &ErrNotFound{Kind: "key", Value: segmentValue}
}
return v.Message(), nil
}
func (c *structComposite) Mutable(s Segment) (any, error) {
if !c.struct_.IsValid() || !c.fields.IsValid() {
return nil, ErrMutationOfReadOnlyValue
}
return c.Get(s)
}
func (c *structComposite) Access(s Segment) (any, error) {
return c.Get(s)
}
func (c *structComposite) AccessMutable(s Segment) (any, error) {
if !c.struct_.IsValid() || !c.fields.IsValid() {
return nil, ErrMutationOfReadOnlyValue
}
return c.Get(s)
}