Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame^] | 1 | //+build appengine |
| 2 | // TODO: other build tags for environments where unsafe package is inappropriate |
| 3 | |
| 4 | package desc |
| 5 | |
| 6 | type jsonNameMap struct{} |
| 7 | type memoizedDefault struct{} |
| 8 | |
| 9 | // FindFieldByJSONName finds the field with the given JSON field name. If no such |
| 10 | // field exists then nil is returned. Only regular fields are returned, not |
| 11 | // extensions. |
| 12 | func (md *MessageDescriptor) FindFieldByJSONName(jsonName string) *FieldDescriptor { |
| 13 | // NB: With allowed use of unsafe, we use it to atomically define an index |
| 14 | // via atomic.LoadPointer/atomic.StorePointer. Without it, we skip the index |
| 15 | // and do an linear scan of fields each time. |
| 16 | for _, f := range md.fields { |
| 17 | jn := f.proto.GetJsonName() |
| 18 | if jn == "" { |
| 19 | jn = f.proto.GetName() |
| 20 | } |
| 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 | } |