Skip to content

Commit

Permalink
attributes: get rid of the special handling in the badVerb function a…
Browse files Browse the repository at this point in the history
…nd live with what `%#v` gives us
  • Loading branch information
searKing committed Sep 19, 2023
1 parent 32abddd commit 2e3e6ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 57 deletions.
50 changes: 1 addition & 49 deletions attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package attributes

import (
"fmt"
"reflect"
"strings"
)

Expand Down Expand Up @@ -128,54 +127,7 @@ func str(x any) (s string) {
} else if v, ok := x.(string); ok {
return v
}
value := reflect.ValueOf(x)
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
return fmt.Sprintf("<%p>", x)
default:
// This will call badVerb to print as "<%p>", but without leading "%!(" and tailing ")"
return badVerb(x, value)
}
}

const nilAngleString = "<nil>"

// badVerb is like fmt.Sprintf("%p", arg), but with
// leading "%!verb(" replaced by "<" and tailing ")" replaced by ">".
// If an invalid argument is given for a '%p', such as providing
// an int to %p, the generated string will contain a
// description of the problem, as in these examples:
//
// # our style
//
// Wrong type or unknown verb: <type=value>
// Printf("%p", 1): <int=1>
//
// # fmt style as `fmt.Sprintf("%p", arg)`
//
// Wrong type or unknown verb: %!verb(type=value)
// Printf("%p", 1): %!d(int=1)
//
// Adapted from the code in fmt/print.go.
func badVerb(arg any, value reflect.Value) string {
var buf strings.Builder
switch {
case arg != nil:
buf.WriteByte('<')
buf.WriteString(reflect.TypeOf(arg).String())
buf.WriteByte('=')
_, _ = fmt.Fprintf(&buf, "%v", arg)
buf.WriteByte('>')
case value.IsValid():
buf.WriteByte('<')
buf.WriteString(value.Type().String())
buf.WriteByte('=')
_, _ = fmt.Fprintf(&buf, "%v", 0)
buf.WriteByte('>')
default:
buf.WriteString(nilAngleString)
}
return buf.String()
return fmt.Sprintf("%#v", x)
}

// MarshalJSON helps implement the json.Marshaler interface, thereby rendering
Expand Down
16 changes: 8 additions & 8 deletions attributes/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ func ExampleAttributes_String() {
fmt.Println("a7:", a7.String())
fmt.Println("a8:", a8.String())
// Output:
// a1: {"<attributes_test.key={}>": "<nil>" }
// a2: {"<attributes_test.key={}>": "<nil>" }
// a3: {"<attributes_test.key={}>": "<0x0>" }
// a4: {"<attributes_test.key={}>": "<nil>" }
// a5: {"<attributes_test.key={}>": "<int=1>" }
// a6: {"<attributes_test.key={}>": "two" }
// a7: {"<attributes_test.key={}>": "<attributes_test.stringVal={two}>" }
// a8: {"<int=1>": "<bool=true>" }
// a1: {"attributes_test.key{}": "<nil>" }
// a2: {"attributes_test.key{}": "<nil>" }
// a3: {"attributes_test.key{}": "(*attributes_test.stringVal)(nil)" }
// a4: {"attributes_test.key{}": "<nil>" }
// a5: {"attributes_test.key{}": "1" }
// a6: {"attributes_test.key{}": "two" }
// a7: {"attributes_test.key{}": "attributes_test.stringVal{s:\"two\"}" }
// a8: {"1": "true" }
}

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

0 comments on commit 2e3e6ee

Please sign in to comment.