Scott Baker | 105df15 | 2020-04-13 15:55:14 -0700 | [diff] [blame^] | 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 | "reflect" |
| 9 | |
| 10 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 11 | ) |
| 12 | |
| 13 | // weakFields adds methods to the exported WeakFields type for internal use. |
| 14 | // |
| 15 | // The exported type is an alias to an unnamed type, so methods can't be |
| 16 | // defined directly on it. |
| 17 | type weakFields WeakFields |
| 18 | |
| 19 | func (w *weakFields) get(num pref.FieldNumber) (_ pref.ProtoMessage, ok bool) { |
| 20 | if *w == nil { |
| 21 | return nil, false |
| 22 | } |
| 23 | m, ok := (*w)[int32(num)] |
| 24 | if !ok { |
| 25 | return nil, false |
| 26 | } |
| 27 | // As a legacy quirk, consider a typed nil to be unset. |
| 28 | // |
| 29 | // TODO: Consider fixing the generated set methods to clear the field |
| 30 | // when provided with a typed nil. |
| 31 | if v := reflect.ValueOf(m); v.Kind() == reflect.Ptr && v.IsNil() { |
| 32 | return nil, false |
| 33 | } |
| 34 | return Export{}.ProtoMessageV2Of(m), true |
| 35 | } |
| 36 | |
| 37 | func (w *weakFields) set(num pref.FieldNumber, m pref.ProtoMessage) { |
| 38 | if *w == nil { |
| 39 | *w = make(weakFields) |
| 40 | } |
| 41 | (*w)[int32(num)] = Export{}.ProtoMessageV1Of(m) |
| 42 | } |
| 43 | |
| 44 | func (w *weakFields) clear(num pref.FieldNumber) { |
| 45 | delete(*w, int32(num)) |
| 46 | } |