Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address panic-ing quadtree.Matching(…) method when finding no closest node #73

Merged
merged 4 commits into from
Oct 12, 2021

Conversation

willsalz
Copy link
Contributor

@willsalz willsalz commented Sep 29, 2021

a fix here is not straight forward because the method signature does not
return error. It's possible that we could add a type orb.NilPointer
[ ugh naming ] that satisfies the orb.Poitner interface but could be
returned without breaking existing clients

…ching

this test case panics because in quadtree/quadtree.go does not check
if the `findVisitor.closest` value is nil before returning
`v.closest.Value` at the end of the method.

a fix here is not straight forward because the method signature does not
return `error`. It's possible that we could add a `type orb.NilPointer`
[ ugh naming ] that satisfies the orb.Poitner interface but could be
returned without breaking existing clients
just return a private, nilPointer struct that implements orb.Pointer by
returning an orb.Point{} directly. This change is technically breaking
in that it no longer reliably panics if users were relying on that
behavior. It is the simplest change I could think of that didn't change
the API.

Alternatively, we can introduce new FindV2 and MatchingV2 methods that
return (orb.Pointer, error) that do not panic that can survive until a
minor orb version bump with breaking API.
@willsalz willsalz marked this pull request as ready for review September 29, 2021 00:31
@willsalz willsalz changed the title add a panicing test case to quadtree/quadtree_test.go/TestQuadTreeMatching Address panic-ing quadtree.Matching(…) method when finding no closest node Sep 29, 2021
@@ -181,6 +181,14 @@ func (q *Quadtree) Find(p orb.Point) orb.Pointer {
return q.Matching(p, nil)
}

type nilPointer struct{}
Copy link
Contributor Author

@willsalz willsalz Sep 29, 2021

Choose a reason for hiding this comment

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

what an unfortunate name; consider emptyPointer?

@@ -204,6 +212,9 @@ func (q *Quadtree) Matching(p orb.Point, f FilterFunc) orb.Pointer {
q.bound.Min[1], q.bound.Max[1],
)

if v.closest == nil {
return nilPointer{}
Copy link
Owner

Choose a reason for hiding this comment

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

why can't this just return nil? The caller would have the do the if results != nil check, but at this point the code is panicing.

@@ -136,6 +136,12 @@ func TestQuadtreeMatching(t *testing.T) {
point: orb.Point{0.1, 0.1},
expected: orb.Point{1, 1},
},
{
name: "match none filter",
Copy link
Owner

Choose a reason for hiding this comment

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

This test will need to be updated. Maybe change the expected to a pointer expected *orb.Point and handle the case below.

@paulmach
Copy link
Owner

Thank you for taking the time to make this change.

@paulmach paulmach merged commit d1cf592 into paulmach:master Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants