blob: 25d619a288247530824659afe5ef30be21349f5c [file] [log] [blame]
Joey Armstrong903c69d2024-02-01 19:46:39 -05001//go:build appengine || gopherjs || purego
2// +build appengine gopherjs purego
3
Scott Baker4a35a702019-11-26 08:17:33 -08004// NB: other environments where unsafe is unappropriate should use "purego" build tag
5// https://github.com/golang/go/issues/23172
Zack Williamse940c7a2019-08-21 14:25:39 -07006
7package desc
8
9type jsonNameMap struct{}
10type memoizedDefault struct{}
11
12// FindFieldByJSONName finds the field with the given JSON field name. If no such
13// field exists then nil is returned. Only regular fields are returned, not
14// extensions.
15func (md *MessageDescriptor) FindFieldByJSONName(jsonName string) *FieldDescriptor {
16 // NB: With allowed use of unsafe, we use it to atomically define an index
17 // via atomic.LoadPointer/atomic.StorePointer. Without it, we skip the index
Joey Armstrong903c69d2024-02-01 19:46:39 -050018 // and must do a linear scan of fields each time.
Zack Williamse940c7a2019-08-21 14:25:39 -070019 for _, f := range md.fields {
Joey Armstrong903c69d2024-02-01 19:46:39 -050020 jn := f.GetJSONName()
Zack Williamse940c7a2019-08-21 14:25:39 -070021 if jn == jsonName {
22 return f
23 }
24 }
25 return nil
26}
27
28func (fd *FieldDescriptor) getDefaultValue() interface{} {
29 return fd.determineDefault()
30}