MCPcopy Create free account
hub / github.com/coder/slog / formatValue

Function formatValue

internal/entryhuman/entry.go:59–92  ·  view source on GitHub ↗
(v interface{})

Source from the content-addressed store, hash-verified

57}
58
59func formatValue(v interface{}) (string, error) {
60 if vr, ok := v.(driver.Valuer); ok {
61 var err error
62 v, err = vr.Value()
63 if err != nil {
64 return "", xerrors.Errorf("error calling Value: %w", err)
65 }
66 }
67 if v == nil {
68 return "<nil>", nil
69 }
70 switch v.(type) {
71 case error, fmt.Stringer:
72 return quote(fmt.Sprintf("%+v", v)), nil
73 }
74 typ := reflect.TypeOf(v)
75 switch typ.Kind() {
76 case reflect.Struct, reflect.Map:
77 byt, err := json.Marshal(v)
78 if err != nil {
79 // don't panic
80 return "", xerrors.Errorf("error marshalling value: %w", err)
81 }
82 return string(byt), nil
83 case reflect.Slice:
84 // Byte slices are optimistically readable.
85 if typ.Elem().Kind() == reflect.Uint8 {
86 return fmt.Sprintf("%q", v), nil
87 }
88 fallthrough
89 default:
90 return quote(fmt.Sprintf("%+v", v)), nil
91 }
92}
93
94const tab = " "
95

Callers 1

FmtMethod · 0.85

Calls 2

quoteFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected