github.com/hoveychen/protoreflect@v1.4.7-0.20221103114119-0b4b3385ec76/desc/descriptor_no_unsafe.go (about)

     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
     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  }