| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | // Copyright 2019 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package impl | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 18:30:40 +01:00
										 |  |  | 	"google.golang.org/protobuf/reflect/protoreflect" | 
					
						
							| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | 	"google.golang.org/protobuf/reflect/protoregistry" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // weakFields adds methods to the exported WeakFields type for internal use. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The exported type is an alias to an unnamed type, so methods can't be | 
					
						
							|  |  |  | // defined directly on it. | 
					
						
							|  |  |  | type weakFields WeakFields | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 18:30:40 +01:00
										 |  |  | func (w weakFields) get(num protoreflect.FieldNumber) (protoreflect.ProtoMessage, bool) { | 
					
						
							| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | 	m, ok := w[int32(num)] | 
					
						
							|  |  |  | 	return m, ok | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 18:30:40 +01:00
										 |  |  | func (w *weakFields) set(num protoreflect.FieldNumber, m protoreflect.ProtoMessage) { | 
					
						
							| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | 	if *w == nil { | 
					
						
							|  |  |  | 		*w = make(weakFields) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	(*w)[int32(num)] = m | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 18:30:40 +01:00
										 |  |  | func (w *weakFields) clear(num protoreflect.FieldNumber) { | 
					
						
							| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | 	delete(*w, int32(num)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 18:30:40 +01:00
										 |  |  | func (Export) HasWeak(w WeakFields, num protoreflect.FieldNumber) bool { | 
					
						
							| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | 	_, ok := w[int32(num)] | 
					
						
							|  |  |  | 	return ok | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 18:30:40 +01:00
										 |  |  | func (Export) ClearWeak(w *WeakFields, num protoreflect.FieldNumber) { | 
					
						
							| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | 	delete(*w, int32(num)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 18:30:40 +01:00
										 |  |  | func (Export) GetWeak(w WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName) protoreflect.ProtoMessage { | 
					
						
							| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | 	if m, ok := w[int32(num)]; ok { | 
					
						
							|  |  |  | 		return m | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	mt, _ := protoregistry.GlobalTypes.FindMessageByName(name) | 
					
						
							|  |  |  | 	if mt == nil { | 
					
						
							|  |  |  | 		panic(fmt.Sprintf("message %v for weak field is not linked in", name)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return mt.Zero().Interface() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 18:30:40 +01:00
										 |  |  | func (Export) SetWeak(w *WeakFields, num protoreflect.FieldNumber, name protoreflect.FullName, m protoreflect.ProtoMessage) { | 
					
						
							| 
									
										
										
										
											2021-08-12 21:03:24 +02:00
										 |  |  | 	if m != nil { | 
					
						
							|  |  |  | 		mt, _ := protoregistry.GlobalTypes.FindMessageByName(name) | 
					
						
							|  |  |  | 		if mt == nil { | 
					
						
							|  |  |  | 			panic(fmt.Sprintf("message %v for weak field is not linked in", name)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if mt != m.ProtoReflect().Type() { | 
					
						
							|  |  |  | 			panic(fmt.Sprintf("invalid message type for weak field: got %T, want %T", m, mt.Zero().Interface())) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if m == nil || !m.ProtoReflect().IsValid() { | 
					
						
							|  |  |  | 		delete(*w, int32(num)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if *w == nil { | 
					
						
							|  |  |  | 		*w = make(weakFields) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	(*w)[int32(num)] = m | 
					
						
							|  |  |  | } |