5 package impl |
5 package impl |
6 |
6 |
7 import ( |
7 import ( |
8 "fmt" |
8 "fmt" |
9 |
9 |
10 pref "google.golang.org/protobuf/reflect/protoreflect" |
10 "google.golang.org/protobuf/reflect/protoreflect" |
11 "google.golang.org/protobuf/reflect/protoregistry" |
11 "google.golang.org/protobuf/reflect/protoregistry" |
12 ) |
12 ) |
13 |
13 |
14 // weakFields adds methods to the exported WeakFields type for internal use. |
14 // weakFields adds methods to the exported WeakFields type for internal use. |
15 // |
15 // |
16 // The exported type is an alias to an unnamed type, so methods can't be |
16 // The exported type is an alias to an unnamed type, so methods can't be |
17 // defined directly on it. |
17 // defined directly on it. |
18 type weakFields WeakFields |
18 type weakFields WeakFields |
19 |
19 |
20 func (w weakFields) get(num pref.FieldNumber) (pref.ProtoMessage, bool) { |
20 func (w weakFields) get(num protoreflect.FieldNumber) (protoreflect.ProtoMessage, bool) { |
21 m, ok := w[int32(num)] |
21 m, ok := w[int32(num)] |
22 return m, ok |
22 return m, ok |
23 } |
23 } |
24 |
24 |
25 func (w *weakFields) set(num pref.FieldNumber, m pref.ProtoMessage) { |
25 func (w *weakFields) set(num protoreflect.FieldNumber, m protoreflect.ProtoMessage) { |
26 if *w == nil { |
26 if *w == nil { |
27 *w = make(weakFields) |
27 *w = make(weakFields) |
28 } |
28 } |
29 (*w)[int32(num)] = m |
29 (*w)[int32(num)] = m |
30 } |
30 } |
31 |
31 |
32 func (w *weakFields) clear(num pref.FieldNumber) { |
32 func (w *weakFields) clear(num protoreflect.FieldNumber) { |
33 delete(*w, int32(num)) |
33 delete(*w, int32(num)) |
34 } |
34 } |
35 |
35 |
36 func (Export) HasWeak(w WeakFields, num pref.FieldNumber) bool { |
36 func (Export) HasWeak(w WeakFields, num protoreflect.FieldNumber) bool { |
37 _, ok := w[int32(num)] |
37 _, ok := w[int32(num)] |
38 return ok |
38 return ok |
39 } |
39 } |
40 |
40 |
41 func (Export) ClearWeak(w *WeakFields, num pref.FieldNumber) { |
41 func (Export) ClearWeak(w *WeakFields, num protoreflect.FieldNumber) { |
42 delete(*w, int32(num)) |
42 delete(*w, int32(num)) |
43 } |
43 } |
44 |
44 |
45 func (Export) GetWeak(w WeakFields, num pref.FieldNumber, name pref.FullName) pref.ProtoMessage { |
45 func (Export) GetWeak(w WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName) protoreflect.ProtoMessage { |
46 if m, ok := w[int32(num)]; ok { |
46 if m, ok := w[int32(num)]; ok { |
47 return m |
47 return m |
48 } |
48 } |
49 mt, _ := protoregistry.GlobalTypes.FindMessageByName(name) |
49 mt, _ := protoregistry.GlobalTypes.FindMessageByName(name) |
50 if mt == nil { |
50 if mt == nil { |
51 panic(fmt.Sprintf("message %v for weak field is not linked in", name)) |
51 panic(fmt.Sprintf("message %v for weak field is not linked in", name)) |
52 } |
52 } |
53 return mt.Zero().Interface() |
53 return mt.Zero().Interface() |
54 } |
54 } |
55 |
55 |
56 func (Export) SetWeak(w *WeakFields, num pref.FieldNumber, name pref.FullName, m pref.ProtoMessage) { |
56 func (Export) SetWeak(w *WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName, m protoreflect.ProtoMessage) { |
57 if m != nil { |
57 if m != nil { |
58 mt, _ := protoregistry.GlobalTypes.FindMessageByName(name) |
58 mt, _ := protoregistry.GlobalTypes.FindMessageByName(name) |
59 if mt == nil { |
59 if mt == nil { |
60 panic(fmt.Sprintf("message %v for weak field is not linked in", name)) |
60 panic(fmt.Sprintf("message %v for weak field is not linked in", name)) |
61 } |
61 } |