-
Notifications
You must be signed in to change notification settings - Fork 110
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
Conversation
…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.
quadtree/quadtree.go
Outdated
@@ -181,6 +181,14 @@ func (q *Quadtree) Find(p orb.Point) orb.Pointer { | |||
return q.Matching(p, nil) | |||
} | |||
|
|||
type nilPointer struct{} |
There was a problem hiding this comment.
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
?
quadtree/quadtree.go
Outdated
@@ -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{} |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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.
Thank you for taking the time to make this change. |
a fix here is not straight forward because the method signature does not
return
error
. It's possible that we could add atype orb.NilPointer
[ ugh naming ] that satisfies the orb.Poitner interface but could be
returned without breaking existing clients