MCPcopy
hub / github.com/google/go-cmp / retrieveUnexportedField

Function retrieveUnexportedField

cmp/export.go:18–31  ·  view source on GitHub ↗

retrieveUnexportedField uses unsafe to forcibly retrieve any field from a struct such that the value has read-write permissions. The parent struct, v, must be addressable, while f must be a StructField describing the field to retrieve. If addr is false, then the returned value will be shallowed cop

(v reflect.Value, f reflect.StructField, addr bool)

Source from the content-addressed store, hash-verified

16// describing the field to retrieve. If addr is false,
17// then the returned value will be shallowed copied to be non-addressable.
18func retrieveUnexportedField(v reflect.Value, f reflect.StructField, addr bool) reflect.Value {
19 ve := reflect.NewAt(f.Type, unsafe.Pointer(uintptr(unsafe.Pointer(v.UnsafeAddr()))+f.Offset)).Elem()
20 if !addr {
21 // A field is addressable if and only if the struct is addressable.
22 // If the original parent value was not addressable, shallow copy the
23 // value to make it non-addressable to avoid leaking an implementation
24 // detail of how forcibly exporting a field works.
25 if ve.Kind() == reflect.Interface && ve.IsNil() {
26 return reflect.Zero(f.Type)
27 }
28 return reflect.ValueOf(ve.Interface()).Convert(f.Type)
29 }
30 return ve
31}

Callers 2

ValuesMethod · 0.85
FormatValueMethod · 0.85

Calls 1

IsNilMethod · 0.80

Tested by

no test coverage detected