...
1
2
3
4
5 package impl
6
7 import (
8 "fmt"
9
10 "google.golang.org/protobuf/reflect/protoreflect"
11 "google.golang.org/protobuf/reflect/protoregistry"
12 )
13
14
15
16
17
18 type weakFields WeakFields
19
20 func (w weakFields) get(num protoreflect.FieldNumber) (protoreflect.ProtoMessage, bool) {
21 m, ok := w[int32(num)]
22 return m, ok
23 }
24
25 func (w *weakFields) set(num protoreflect.FieldNumber, m protoreflect.ProtoMessage) {
26 if *w == nil {
27 *w = make(weakFields)
28 }
29 (*w)[int32(num)] = m
30 }
31
32 func (w *weakFields) clear(num protoreflect.FieldNumber) {
33 delete(*w, int32(num))
34 }
35
36 func (Export) HasWeak(w WeakFields, num protoreflect.FieldNumber) bool {
37 _, ok := w[int32(num)]
38 return ok
39 }
40
41 func (Export) ClearWeak(w *WeakFields, num protoreflect.FieldNumber) {
42 delete(*w, int32(num))
43 }
44
45 func (Export) GetWeak(w WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName) protoreflect.ProtoMessage {
46 if m, ok := w[int32(num)]; ok {
47 return m
48 }
49 mt, _ := protoregistry.GlobalTypes.FindMessageByName(name)
50 if mt == nil {
51 panic(fmt.Sprintf("message %v for weak field is not linked in", name))
52 }
53 return mt.Zero().Interface()
54 }
55
56 func (Export) SetWeak(w *WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName, m protoreflect.ProtoMessage) {
57 if m != nil {
58 mt, _ := protoregistry.GlobalTypes.FindMessageByName(name)
59 if mt == nil {
60 panic(fmt.Sprintf("message %v for weak field is not linked in", name))
61 }
62 if mt != m.ProtoReflect().Type() {
63 panic(fmt.Sprintf("invalid message type for weak field: got %T, want %T", m, mt.Zero().Interface()))
64 }
65 }
66 if m == nil || !m.ProtoReflect().IsValid() {
67 delete(*w, int32(num))
68 return
69 }
70 if *w == nil {
71 *w = make(weakFields)
72 }
73 (*w)[int32(num)] = m
74 }
75
View as plain text