Skip to content

Commit 2e3e6ee

Browse files
committed
attributes: get rid of the special handling in the badVerb function and live with what %#v gives us
1 parent 32abddd commit 2e3e6ee

File tree

2 files changed

+9
-57
lines changed

2 files changed

+9
-57
lines changed

attributes/attributes.go

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ package attributes
2727

2828
import (
2929
"fmt"
30-
"reflect"
3130
"strings"
3231
)
3332

@@ -128,54 +127,7 @@ func str(x any) (s string) {
128127
} else if v, ok := x.(string); ok {
129128
return v
130129
}
131-
value := reflect.ValueOf(x)
132-
switch value.Kind() {
133-
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
134-
return fmt.Sprintf("<%p>", x)
135-
default:
136-
// This will call badVerb to print as "<%p>", but without leading "%!(" and tailing ")"
137-
return badVerb(x, value)
138-
}
139-
}
140-
141-
const nilAngleString = "<nil>"
142-
143-
// badVerb is like fmt.Sprintf("%p", arg), but with
144-
// leading "%!verb(" replaced by "<" and tailing ")" replaced by ">".
145-
// If an invalid argument is given for a '%p', such as providing
146-
// an int to %p, the generated string will contain a
147-
// description of the problem, as in these examples:
148-
//
149-
// # our style
150-
//
151-
// Wrong type or unknown verb: <type=value>
152-
// Printf("%p", 1): <int=1>
153-
//
154-
// # fmt style as `fmt.Sprintf("%p", arg)`
155-
//
156-
// Wrong type or unknown verb: %!verb(type=value)
157-
// Printf("%p", 1): %!d(int=1)
158-
//
159-
// Adapted from the code in fmt/print.go.
160-
func badVerb(arg any, value reflect.Value) string {
161-
var buf strings.Builder
162-
switch {
163-
case arg != nil:
164-
buf.WriteByte('<')
165-
buf.WriteString(reflect.TypeOf(arg).String())
166-
buf.WriteByte('=')
167-
_, _ = fmt.Fprintf(&buf, "%v", arg)
168-
buf.WriteByte('>')
169-
case value.IsValid():
170-
buf.WriteByte('<')
171-
buf.WriteString(value.Type().String())
172-
buf.WriteByte('=')
173-
_, _ = fmt.Fprintf(&buf, "%v", 0)
174-
buf.WriteByte('>')
175-
default:
176-
buf.WriteString(nilAngleString)
177-
}
178-
return buf.String()
130+
return fmt.Sprintf("%#v", x)
179131
}
180132

181133
// MarshalJSON helps implement the json.Marshaler interface, thereby rendering

attributes/attributes_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ func ExampleAttributes_String() {
8585
fmt.Println("a7:", a7.String())
8686
fmt.Println("a8:", a8.String())
8787
// Output:
88-
// a1: {"<attributes_test.key={}>": "<nil>" }
89-
// a2: {"<attributes_test.key={}>": "<nil>" }
90-
// a3: {"<attributes_test.key={}>": "<0x0>" }
91-
// a4: {"<attributes_test.key={}>": "<nil>" }
92-
// a5: {"<attributes_test.key={}>": "<int=1>" }
93-
// a6: {"<attributes_test.key={}>": "two" }
94-
// a7: {"<attributes_test.key={}>": "<attributes_test.stringVal={two}>" }
95-
// a8: {"<int=1>": "<bool=true>" }
88+
// a1: {"attributes_test.key{}": "<nil>" }
89+
// a2: {"attributes_test.key{}": "<nil>" }
90+
// a3: {"attributes_test.key{}": "(*attributes_test.stringVal)(nil)" }
91+
// a4: {"attributes_test.key{}": "<nil>" }
92+
// a5: {"attributes_test.key{}": "1" }
93+
// a6: {"attributes_test.key{}": "two" }
94+
// a7: {"attributes_test.key{}": "attributes_test.stringVal{s:\"two\"}" }
95+
// a8: {"1": "true" }
9696
}
9797

9898
// Test that two attributes with the same content are Equal.

0 commit comments

Comments
 (0)