256
|
1 |
// Copyright 2019 The Go Authors. All rights reserved. |
|
2 |
// Use of this source code is governed by a BSD-style |
|
3 |
// license that can be found in the LICENSE file. |
|
4 |
|
|
5 |
package impl |
|
6 |
|
|
7 |
import ( |
|
8 |
"fmt" |
|
9 |
|
260
|
10 |
"google.golang.org/protobuf/reflect/protoreflect" |
256
|
11 |
"google.golang.org/protobuf/reflect/protoregistry" |
|
12 |
) |
|
13 |
|
|
14 |
// weakFields adds methods to the exported WeakFields type for internal use. |
|
15 |
// |
|
16 |
// The exported type is an alias to an unnamed type, so methods can't be |
|
17 |
// defined directly on it. |
|
18 |
type weakFields WeakFields |
|
19 |
|
260
|
20 |
func (w weakFields) get(num protoreflect.FieldNumber) (protoreflect.ProtoMessage, bool) { |
256
|
21 |
m, ok := w[int32(num)] |
|
22 |
return m, ok |
|
23 |
} |
|
24 |
|
260
|
25 |
func (w *weakFields) set(num protoreflect.FieldNumber, m protoreflect.ProtoMessage) { |
256
|
26 |
if *w == nil { |
|
27 |
*w = make(weakFields) |
|
28 |
} |
|
29 |
(*w)[int32(num)] = m |
|
30 |
} |
|
31 |
|
260
|
32 |
func (w *weakFields) clear(num protoreflect.FieldNumber) { |
256
|
33 |
delete(*w, int32(num)) |
|
34 |
} |
|
35 |
|
260
|
36 |
func (Export) HasWeak(w WeakFields, num protoreflect.FieldNumber) bool { |
256
|
37 |
_, ok := w[int32(num)] |
|
38 |
return ok |
|
39 |
} |
|
40 |
|
260
|
41 |
func (Export) ClearWeak(w *WeakFields, num protoreflect.FieldNumber) { |
256
|
42 |
delete(*w, int32(num)) |
|
43 |
} |
|
44 |
|
260
|
45 |
func (Export) GetWeak(w WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName) protoreflect.ProtoMessage { |
256
|
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 |
|
260
|
56 |
func (Export) SetWeak(w *WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName, m protoreflect.ProtoMessage) { |
256
|
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 |
} |