davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging
ISC License
5.98k stars 361 forks source link

Selective usage of String method #65

Open distributed opened 7 years ago

distributed commented 7 years ago

I have some Protobuf/gRPC generated types that I would like to pretty print using spew.

Some types, like enums, have String() methods generated that produce nice, readable output, for an example, see DevStatusReply_Status in the code below. For these types I prefer spew calling String() instead of formatting by itself.

Some types, structs like DevStatusReply_StatusDetails below , have a generated String() method that produces terses output. For these types I would rather have spew handle the printing by itself.

Using ConfigState.DisableMethods I can turn off and on whether spew calls the String() method, but I cannot discern between calling String() for the enums and spew pretty printing for the structs.

Is there a way I can control more finely which String()methods get called? Is there a way to work around this, except duplicating spew's code in my own program?

Code:

type DevStatusReply struct {
    Status      DevStatusReply_Status            `protobuf:"varint,4,opt,name=...`
    Details     *DevStatusReply_StatusDetails `protobuf:"bytes,8,opt,name=..."`
}

type DevStatusReply_Status int32
const (
    DevStatusReply_INVALID_STATUS DevStatusReply_Status = 0
    DevStatusReply_IDLE           DevStatusReply_Status = 1
    // and so on...
)

// prints nice string representation of enum
func (s DevStatusReply_Status) String() string { ... }

type DevStatusReply_StatusDetails struct {
   ...
}

// prints unwanted string representation of struct
func (x *DevStatusReply_StatusDetails) String() { ... }
jrick commented 7 years ago

Might want to report this to the gRPC developers so the protoc plugin can produce better output. It should be implementing GoStringer for a string representation of the Go syntax, not String.