blob: eb79a7ba94c096e5c6afd8b80bb25f5b37bc40a9 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301// 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
5package impl
6
7import (
8 "fmt"
9
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053010 "google.golang.org/protobuf/reflect/protoreflect"
Naveen Sampath04696f72022-06-13 15:19:14 +053011 "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.
18type weakFields WeakFields
19
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053020func (w weakFields) get(num protoreflect.FieldNumber) (protoreflect.ProtoMessage, bool) {
Naveen Sampath04696f72022-06-13 15:19:14 +053021 m, ok := w[int32(num)]
22 return m, ok
23}
24
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053025func (w *weakFields) set(num protoreflect.FieldNumber, m protoreflect.ProtoMessage) {
Naveen Sampath04696f72022-06-13 15:19:14 +053026 if *w == nil {
27 *w = make(weakFields)
28 }
29 (*w)[int32(num)] = m
30}
31
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053032func (w *weakFields) clear(num protoreflect.FieldNumber) {
Naveen Sampath04696f72022-06-13 15:19:14 +053033 delete(*w, int32(num))
34}
35
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053036func (Export) HasWeak(w WeakFields, num protoreflect.FieldNumber) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +053037 _, ok := w[int32(num)]
38 return ok
39}
40
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053041func (Export) ClearWeak(w *WeakFields, num protoreflect.FieldNumber) {
Naveen Sampath04696f72022-06-13 15:19:14 +053042 delete(*w, int32(num))
43}
44
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053045func (Export) GetWeak(w WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName) protoreflect.ProtoMessage {
Naveen Sampath04696f72022-06-13 15:19:14 +053046 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
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053056func (Export) SetWeak(w *WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName, m protoreflect.ProtoMessage) {
Naveen Sampath04696f72022-06-13 15:19:14 +053057 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}