summaryrefslogtreecommitdiff
path: root/vendor/github.com/jhump/protoreflect/desc/descriptor_no_unsafe.go
blob: 25d619a288247530824659afe5ef30be21349f5c (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
//go:build appengine || gopherjs || purego
// +build appengine gopherjs purego

// NB: other environments where unsafe is unappropriate should use "purego" build tag
// https://github.com/golang/go/issues/23172

package desc

type jsonNameMap struct{}
type memoizedDefault struct{}

// FindFieldByJSONName finds the field with the given JSON field name. If no such
// field exists then nil is returned. Only regular fields are returned, not
// extensions.
func (md *MessageDescriptor) FindFieldByJSONName(jsonName string) *FieldDescriptor {
	// NB: With allowed use of unsafe, we use it to atomically define an index
	// via atomic.LoadPointer/atomic.StorePointer. Without it, we skip the index
	// and must do a linear scan of fields each time.
	for _, f := range md.fields {
		jn := f.GetJSONName()
		if jn == jsonName {
			return f
		}
	}
	return nil
}

func (fd *FieldDescriptor) getDefaultValue() interface{} {
	return fd.determineDefault()
}