Skip to content
Merged
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
6 changes: 5 additions & 1 deletion pointers/ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NullIsZero[T comparable](v *T) T {
return *v
}

func Eq[T comparable](a, b *T) bool {
func Equal[T comparable](a, b *T) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would you think of adding smth like:

       if aa, ok := a.(interface{ Equal(b T) bool }); ok {
               return aa.Equal(*b)
       }

before the final return?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't compile for me:

pointers/ptr.go:35:15: invalid operation: a (variable of type *T) is not an interface

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bummer :(. tho i do have a dirty trick for you. you can add a var v any = a and do the type assertion on v.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, thank you

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your syntax is much better 👍🏼

can i ask you to add a test to assert this? you could use some *time.Time for example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

if a == b {
return true
}
Expand All @@ -32,5 +32,9 @@ func Eq[T comparable](a, b *T) bool {
return false
}

if aa, ok := any(a).(interface{ Equal(b T) bool }); ok {
return aa.Equal(*b)
}

return *a == *b
}
31 changes: 19 additions & 12 deletions pointers/ptr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pointers

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -42,32 +43,38 @@ type testStruct struct {

func TestEq(t *testing.T) {
n := 5
assert.True(t, Eq(&n, &n))
assert.True(t, Eq(Ptr(5), &n))
assert.True(t, Eq(Ptr(5), Ptr(5)))
assert.False(t, Eq(&n, Ptr(6)))
assert.False(t, Eq(Ptr(5), Ptr(6)))
assert.True(t, Equal(&n, &n))
assert.True(t, Equal(Ptr(5), &n))
assert.True(t, Equal(Ptr(5), Ptr(5)))
assert.False(t, Equal(&n, Ptr(6)))
assert.False(t, Equal(Ptr(5), Ptr(6)))

str := "foo"
assert.True(t, Eq(&str, &str))
assert.True(t, Eq(&str, Ptr("foo")))
assert.False(t, Eq(&str, Ptr("bar")))
assert.True(t, Equal(&str, &str))
assert.True(t, Equal(&str, Ptr("foo")))
assert.False(t, Equal(&str, Ptr("bar")))

s := testStruct{
number: 1,
string: "hello",
}
assert.True(t, Eq(&s, &s))
assert.True(t, Eq(&s, &testStruct{
assert.True(t, Equal(&s, &s))
assert.True(t, Equal(&s, &testStruct{
number: 1,
string: "hello",
}))
assert.False(t, Eq(&s, &testStruct{
assert.False(t, Equal(&s, &testStruct{
number: 2,
string: "hello",
}))
assert.False(t, Eq(&s, &testStruct{
assert.False(t, Equal(&s, &testStruct{
number: 1,
string: "world",
}))

d1 := Ptr(time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC))
d2 := Ptr(time.Date(2000, 2, 1, 20, 30, 0, 0, time.FixedZone("z2", int((8*time.Hour).Seconds()))))
assert.False(t, *d1 == *d2) // nolint
assert.True(t, Equal(d1, d2))
assert.False(t, Equal(d1, Ptr(d2.Add(time.Hour))))
}
Loading