The name indent better describes the function. This also will help
differentiate the function from planned functions that are intended to
perform padding for the purposes of aligning fields, types, and values.
In order to help future proof itself, spew handles unrecognized reflect
values by passing them on to the standard fmt library. Since spew handles
all current reflect values in the language, this condition has to be
manually tested with a bit of hackery by using unsafe to change the kind
to a nonexistent value.
As of this commit, there is now 100% test coverage.
It was previously possible for an invalid reflect value to lead to a panic
in certain obscure cases. Rather than adding multiple checks for the
invalid reflect value, handle invalid reflect values immediately.
This commit adds tests for a struct which has an embedded struct pointer
and a field that is a pointer to the same object. This ensures the cycle
detection is properly reset between fields.
This commit adds tests for the DisableMethods and DisablePointerMethods
options. All combinations of method options and Stringer interface
invocation for both pointer receivers and non-pointer receivers are
tested.
If a type implements a Stringer or error interface with a pointer receiver
and the value being formatted is addressable, the interface should be
invoked even when the DisablePointerMethods option is set.
DisablePointerMethods is only intended to prevent the potentially unsafe
action of stepping around type-safety restriction to invoke a Stringer or
error interface with a pointer to an unaddressable value.
Previously, the tests did not include maps with more than a single entry
since the iteration order is randomized and the tests only accepted a
single valid expected value. This commit modifies the tests to accept
multiple valid expected values and adds tests for a multi-entry map to
both Dump and Formatter.
Rather than stuffing a ConfigState instance into a separate SpewState,
just add the functionality directly to the ConfigState. This provides
simpler syntax for the consumer.
One side effect of this change is that, unlike a zero value SpewState, a
zero value ConfigState doesn't provide default values which means the
Indent field is set to provide no indentation. The consumer is now
expected to set the indent to their desired value when declaring an
instance of ConfigState.
Alternatively, the consumer can call a new function, NewDefaultConfig,
which returns a ConfigState with default values, including a default
indentation of a single space.
For example, to change the indent to a tab, the previous syntax was:
ss := new(spew.SpewState) // or var ss spew.SpewState
scs := ss.Config()
scs.Indent = "\t"
scs.Dump(whatever)
The new syntax is simply:
scs := spew.ConfigState{Indent: "\t"}
scs.Dump(whatever)
This commit implements feature request #3. In particular, it allows the
formatter to respond to %#v and %#+v. The # flag (%#v) adds type
information to the output and the combination of the # and + flags (%#+v)
adds both type information and pointer information. This allows the
consumer a choice between displaying types, pointer information, or both.
This commit adds a new type, SpewState, which can be used to create
instances with unique configuration options. The methods of SpewState are
equivalent to the top-level functions. Full documentation and examples
are included.
This paves the way to support individual configuration options through a
separate type while still providing the simple global config and package
level methods.