khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 1 | //go:build appengine || gopherjs || purego |
| 2 | // +build appengine gopherjs purego |
| 3 | |
| 4 | // NB: other environments where unsafe is unappropriate should use "purego" build tag |
| 5 | // https://github.com/golang/go/issues/23172 |
| 6 | |
| 7 | package desc |
| 8 | |
| 9 | type jsonNameMap struct{} |
| 10 | type 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. |
| 15 | func (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 |
| 18 | // and must do a linear scan of fields each time. |
| 19 | for _, f := range md.fields { |
| 20 | jn := f.GetJSONName() |
| 21 | if jn == jsonName { |
| 22 | return f |
| 23 | } |
| 24 | } |
| 25 | return nil |
| 26 | } |
| 27 | |
| 28 | func (fd *FieldDescriptor) getDefaultValue() interface{} { |
| 29 | return fd.determineDefault() |
| 30 | } |