summaryrefslogtreecommitdiffstats
path: root/vendor/google.golang.org/protobuf/internal/impl/weak.go
blob: 575c988ccb2fe79df8989facaec106d16b0fbe02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 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 (
	"reflect"

	pref "google.golang.org/protobuf/reflect/protoreflect"
)

// 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

func (w *weakFields) get(num pref.FieldNumber) (_ pref.ProtoMessage, ok bool) {
	if *w == nil {
		return nil, false
	}
	m, ok := (*w)[int32(num)]
	if !ok {
		return nil, false
	}
	// As a legacy quirk, consider a typed nil to be unset.
	//
	// TODO: Consider fixing the generated set methods to clear the field
	// when provided with a typed nil.
	if v := reflect.ValueOf(m); v.Kind() == reflect.Ptr && v.IsNil() {
		return nil, false
	}
	return Export{}.ProtoMessageV2Of(m), true
}

func (w *weakFields) set(num pref.FieldNumber, m pref.ProtoMessage) {
	if *w == nil {
		*w = make(weakFields)
	}
	(*w)[int32(num)] = Export{}.ProtoMessageV1Of(m)
}

func (w *weakFields) clear(num pref.FieldNumber) {
	delete(*w, int32(num))
}