Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 1 | // Copyright 2017, The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame^] | 3 | // license that can be found in the LICENSE file. |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 4 | |
| 5 | // +build !purego |
| 6 | |
| 7 | package cmp |
| 8 | |
| 9 | import ( |
| 10 | "reflect" |
| 11 | "unsafe" |
| 12 | ) |
| 13 | |
| 14 | const supportExporters = true |
| 15 | |
| 16 | // retrieveUnexportedField uses unsafe to forcibly retrieve any field from |
| 17 | // a struct such that the value has read-write permissions. |
| 18 | // |
| 19 | // The parent struct, v, must be addressable, while f must be a StructField |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame^] | 20 | // describing the field to retrieve. If addr is false, |
| 21 | // then the returned value will be shallowed copied to be non-addressable. |
| 22 | func retrieveUnexportedField(v reflect.Value, f reflect.StructField, addr bool) reflect.Value { |
| 23 | ve := reflect.NewAt(f.Type, unsafe.Pointer(uintptr(unsafe.Pointer(v.UnsafeAddr()))+f.Offset)).Elem() |
| 24 | if !addr { |
| 25 | // A field is addressable if and only if the struct is addressable. |
| 26 | // If the original parent value was not addressable, shallow copy the |
| 27 | // value to make it non-addressable to avoid leaking an implementation |
| 28 | // detail of how forcibly exporting a field works. |
| 29 | if ve.Kind() == reflect.Interface && ve.IsNil() { |
| 30 | return reflect.Zero(f.Type) |
| 31 | } |
| 32 | return reflect.ValueOf(ve.Interface()).Convert(f.Type) |
| 33 | } |
| 34 | return ve |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 35 | } |