summaryrefslogtreecommitdiff
path: root/spew/dump.go
diff options
context:
space:
mode:
authorDave Collins <[email protected]>2015-06-17 22:34:22 -0500
committerDave Collins <[email protected]>2015-06-19 15:29:34 -0500
commit2df174808ee097f90d259e432cc04442cf60be21 (patch)
tree137a52652745f1dc47abb8ab3d379cb556e6e632 /spew/dump.go
parentf9f629a1d082960e5d1747f33c2b378612615fea (diff)
Add support for limited mode without unsafe pkg.
This commit adds support for compiling spew without the unsafe package. When compiled without the unsafe package, some of the more advanced features such as invoking stringers on pointers from non-pointer variables and unexported struct fields are not available. By default, spew will be compiled in the limited mode for Google App Engine since the unsafe package is not available there. Additionally, spew can be compiled without the unsafe package manually by specifying the "disableunsafe" build tag. Finally, a new package-level constant named "UnsafeDisabled" has been exposed which can be used to programmatically determine if spew was compiled with access to the unsafe package.
Diffstat (limited to 'spew/dump.go')
-rw-r--r--spew/dump.go31
1 files changed, 18 insertions, 13 deletions
diff --git a/spew/dump.go b/spew/dump.go
index 5783145..36a2b6c 100644
--- a/spew/dump.go
+++ b/spew/dump.go
@@ -181,25 +181,30 @@ func (d *dumpState) dumpSlice(v reflect.Value) {
// Try to use existing uint8 slices and fall back to converting
// and copying if that fails.
case vt.Kind() == reflect.Uint8:
- // We need an addressable interface to convert the type back
- // into a byte slice. However, the reflect package won't give
- // us an interface on certain things like unexported struct
- // fields in order to enforce visibility rules. We use unsafe
- // to bypass these restrictions since this package does not
+ // TODO(davec): Fix up the disableUnsafe bits...
+
+ // We need an addressable interface to convert the type
+ // to a byte slice. However, the reflect package won't
+ // give us an interface on certain things like
+ // unexported struct fields in order to enforce
+ // visibility rules. We use unsafe, when available, to
+ // bypass these restrictions since this package does not
// mutate the values.
vs := v
if !vs.CanInterface() || !vs.CanAddr() {
vs = unsafeReflectValue(vs)
}
- vs = vs.Slice(0, numEntries)
+ if !UnsafeDisabled {
+ vs = vs.Slice(0, numEntries)
- // Use the existing uint8 slice if it can be type
- // asserted.
- iface := vs.Interface()
- if slice, ok := iface.([]uint8); ok {
- buf = slice
- doHexDump = true
- break
+ // Use the existing uint8 slice if it can be
+ // type asserted.
+ iface := vs.Interface()
+ if slice, ok := iface.([]uint8); ok {
+ buf = slice
+ doHexDump = true
+ break
+ }
}
// The underlying data needs to be converted if it can't