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.
10
"google.golang.org/protobuf/reflect/protoreflect"
11
"google.golang.org/protobuf/reflect/protoregistry"
14
// weakFields adds methods to the exported WeakFields type for internal use.
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
20
func (w weakFields) get(num protoreflect.FieldNumber) (protoreflect.ProtoMessage, bool) {
21
m, ok := w[int32(num)]
25
func (w *weakFields) set(num protoreflect.FieldNumber, m protoreflect.ProtoMessage) {
32
func (w *weakFields) clear(num protoreflect.FieldNumber) {
33
delete(*w, int32(num))
36
func (Export) HasWeak(w WeakFields, num protoreflect.FieldNumber) bool {
37
_, ok := w[int32(num)]
41
func (Export) ClearWeak(w *WeakFields, num protoreflect.FieldNumber) {
42
delete(*w, int32(num))
45
func (Export) GetWeak(w WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName) protoreflect.ProtoMessage {
46
if m, ok := w[int32(num)]; ok {
49
mt, _ := protoregistry.GlobalTypes.FindMessageByName(name)
51
panic(fmt.Sprintf("message %v for weak field is not linked in", name))
53
return mt.Zero().Interface()
56
func (Export) SetWeak(w *WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName, m protoreflect.ProtoMessage) {
58
mt, _ := protoregistry.GlobalTypes.FindMessageByName(name)
60
panic(fmt.Sprintf("message %v for weak field is not linked in", name))
62
if mt != m.ProtoReflect().Type() {
63
panic(fmt.Sprintf("invalid message type for weak field: got %T, want %T", m, mt.Zero().Interface()))
66
if m == nil || !m.ProtoReflect().IsValid() {
67
delete(*w, int32(num))