MCPcopy
hub / github.com/uber-go/zap / encodeStringer

Function encodeStringer

zapcore/field.go:214–233  ·  view source on GitHub ↗
(key string, stringer interface{}, enc ObjectEncoder)

Source from the content-addressed store, hash-verified

212}
213
214func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (retErr error) {
215 // Try to capture panics (from nil references or otherwise) when calling
216 // the String() method, similar to https://golang.org/src/fmt/print.go#L540
217 defer func() {
218 if err := recover(); err != nil {
219 // If it's a nil pointer, just say "<nil>". The likeliest causes are a
220 // Stringer that fails to guard against nil or a nil pointer for a
221 // value receiver, and in either case, "<nil>" is a nice result.
222 if v := reflect.ValueOf(stringer); v.Kind() == reflect.Ptr && v.IsNil() {
223 enc.AddString(key, "<nil>")
224 return
225 }
226
227 retErr = fmt.Errorf("PANIC=%v", err)
228 }
229 }()
230
231 enc.AddString(key, stringer.(fmt.Stringer).String())
232 return nil
233}

Callers 1

AddToMethod · 0.85

Calls 3

AddStringMethod · 0.65
ErrorfMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected