Trap errors in Go bindings for Matcher_Next. Use the same design pattern as Hits_Next: trap errors in a Go struct member and expect the user to check after iteration finishes.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/8208210b Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/8208210b Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/8208210b Branch: refs/heads/master Commit: 8208210b3f5877b2056fecd4fa19f4de94ccdd6e Parents: 6612082 Author: Marvin Humphrey <[email protected]> Authored: Mon Nov 16 18:33:40 2015 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Fri Nov 20 19:00:31 2015 -0800 ---------------------------------------------------------------------- go/build.go | 6 ++++++ go/lucy/search.go | 21 +++++++++++++++++++++ go/lucy/search_test.go | 3 +++ 3 files changed, 30 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/8208210b/go/build.go ---------------------------------------------------------------------- diff --git a/go/build.go b/go/build.go index 8db2109..e1fef50 100644 --- a/go/build.go +++ b/go/build.go @@ -250,6 +250,12 @@ func specClasses(parcel *cfc.Parcel) { orQueryBinding.SetSuppressCtor(true) orQueryBinding.Register() + matcherBinding := cfc.NewGoClass(parcel, "Lucy::Search::Matcher") + matcherBinding.SpecMethod("Next", "Next() int32") + matcherBinding.SpecMethod("", "Error() error") + matcherBinding.SetSuppressStruct(true) + matcherBinding.Register() + andMatcherBinding := cfc.NewGoClass(parcel, "Lucy::Search::ANDMatcher") andMatcherBinding.SetSuppressCtor(true) andMatcherBinding.Register() http://git-wip-us.apache.org/repos/asf/lucy/blob/8208210b/go/lucy/search.go ---------------------------------------------------------------------- diff --git a/go/lucy/search.go b/go/lucy/search.go index 2db169b..e202b9d 100644 --- a/go/lucy/search.go +++ b/go/lucy/search.go @@ -60,6 +60,11 @@ type HitsIMP struct { err error } +type MatcherIMP struct { + clownfish.ObjIMP + err error +} + func OpenIndexSearcher(index interface{}) (obj IndexSearcher, err error) { indexC := (*C.cfish_Obj)(clownfish.GoToClownfish(index, unsafe.Pointer(C.CFISH_OBJ), false)) defer C.cfish_decref(unsafe.Pointer(indexC)) @@ -293,6 +298,22 @@ func NewORQuery(children []Query) ORQuery { return WRAPORQuery(unsafe.Pointer(cfObj)) } +func (m *MatcherIMP) Next() int32 { + var retval int32 + m.err = clownfish.TrapErr(func() { + self := (*C.lucy_Matcher)(clownfish.Unwrap(m, "m")) + retval = int32(C.LUCY_Matcher_Next(self)) + }) + if m.err != nil { + return 0 + } + return retval +} + +func (m *MatcherIMP) Error() error { + return m.err +} + func NewANDMatcher(children []Matcher, sim Similarity) ANDMatcher { simC := (*C.lucy_Similarity)(clownfish.UnwrapNullable(sim)) vec := clownfish.NewVector(len(children)) http://git-wip-us.apache.org/repos/asf/lucy/blob/8208210b/go/lucy/search_test.go ---------------------------------------------------------------------- diff --git a/go/lucy/search_test.go b/go/lucy/search_test.go index 1127f72..3d4fc50 100644 --- a/go/lucy/search_test.go +++ b/go/lucy/search_test.go @@ -273,6 +273,9 @@ func checkMatcher(t *testing.T, matcher Matcher, supportsScore bool) { if got := matcher.Next(); got != 0 { t.Error("Next (iteration finished): %d", got) } + if got := matcher.Error(); got != nil { + t.Error("Error after iteration finished: %v", got) + } } func TestMockMatcherBasics(t *testing.T) {
