Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,16 @@ func (a *Attributes) String() string {
}

func str(x any) (s string) {
if x == nil {
return "<nil>"
}
if v, ok := x.(fmt.Stringer); ok {
return fmt.Sprint(v)
} else if v, ok := x.(string); ok {
}
if v, ok := x.(string); ok {
return v
}
return fmt.Sprintf("<%p>", x)
return fmt.Sprintf("%T=%v", x, x)
}

// MarshalJSON helps implement the json.Marshaler interface, thereby rendering
Expand Down
20 changes: 12 additions & 8 deletions attributes/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func ExampleAttributes_WithValue() {

func ExampleAttributes_String() {
type key struct{}
type namedKey string
var typedNil *stringerVal
a1 := attributes.New(key{}, typedNil) // typed nil implements [fmt.Stringer]
a2 := attributes.New(key{}, (*stringerVal)(nil)) // typed nil implements [fmt.Stringer]
Expand All @@ -76,6 +77,7 @@ func ExampleAttributes_String() {
a6 := attributes.New(key{}, stringerVal{s: "two"})
a7 := attributes.New(key{}, stringVal{s: "two"})
a8 := attributes.New(1, true)
a9 := attributes.New(namedKey("my.custom.key"), "val")
fmt.Println("a1:", a1.String())
fmt.Println("a2:", a2.String())
fmt.Println("a3:", a3.String())
Expand All @@ -84,15 +86,17 @@ func ExampleAttributes_String() {
fmt.Println("a6:", a6.String())
fmt.Println("a7:", a7.String())
fmt.Println("a8:", a8.String())
fmt.Println("a9:", a9.String())
// Output:
// a1: {"<%!p(attributes_test.key={})>": "<nil>" }
// a2: {"<%!p(attributes_test.key={})>": "<nil>" }
// a3: {"<%!p(attributes_test.key={})>": "<0x0>" }
// a4: {"<%!p(attributes_test.key={})>": "<%!p(<nil>)>" }
// a5: {"<%!p(attributes_test.key={})>": "<%!p(int=1)>" }
// a6: {"<%!p(attributes_test.key={})>": "two" }
// a7: {"<%!p(attributes_test.key={})>": "<%!p(attributes_test.stringVal={two})>" }
// a8: {"<%!p(int=1)>": "<%!p(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={}": "int=1" }
// a6: {"attributes_test.key={}": "two" }
// a7: {"attributes_test.key={}": "attributes_test.stringVal={two}" }
// a8: {"int=1": "bool=true" }
// a9: {"attributes_test.namedKey=my.custom.key": "val" }
}

// Test that two attributes with different content are not Equal.
Expand Down
Loading