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