Scott Baker | 4a35a70 | 2019-11-26 08:17:33 -0800 | [diff] [blame] | 1 | //+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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 4 | |
| 5 | package desc |
| 6 | |
| 7 | type jsonNameMap struct{} |
| 8 | type 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. |
| 13 | func (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 | |
| 29 | func (fd *FieldDescriptor) getDefaultValue() interface{} { |
| 30 | return fd.determineDefault() |
| 31 | } |