Tune and test Go bindings for Doc.

Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/91dd3894
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/91dd3894
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/91dd3894

Branch: refs/heads/master
Commit: 91dd3894b16cc3f5140c2ac398d193a59f4b19a0
Parents: d5063a5
Author: Marvin Humphrey <[email protected]>
Authored: Mon Nov 16 17:15:05 2015 -0800
Committer: Marvin Humphrey <[email protected]>
Committed: Fri Nov 20 19:00:31 2015 -0800

----------------------------------------------------------------------
 go/build.go              |  6 ++++
 go/lucy/document.go      | 21 ++++++++++++
 go/lucy/document_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++
 go/lucy/lucy.go          |  2 +-
 4 files changed, 102 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/91dd3894/go/build.go
----------------------------------------------------------------------
diff --git a/go/build.go b/go/build.go
index 3b6fc68..8db2109 100644
--- a/go/build.go
+++ b/go/build.go
@@ -159,6 +159,12 @@ func specClasses(parcel *cfc.Parcel) {
        polyAnalyzerBinding.SetSuppressCtor(true)
        polyAnalyzerBinding.Register()
 
+       docBinding := cfc.NewGoClass(parcel, "Lucy::Document::Doc")
+       docBinding.SpecMethod("", "GetFields() map[string]interface{}")
+       docBinding.SpecMethod("", "SetFields(map[string]interface{})")
+       docBinding.SpecMethod("Field_Names", "FieldNames() []string")
+       docBinding.Register()
+
        indexerBinding := cfc.NewGoClass(parcel, "Lucy::Index::Indexer")
        indexerBinding.SpecMethod("", "Close() error")
        indexerBinding.SpecMethod("Add_Doc", "AddDoc(doc interface{}) error")

http://git-wip-us.apache.org/repos/asf/lucy/blob/91dd3894/go/lucy/document.go
----------------------------------------------------------------------
diff --git a/go/lucy/document.go b/go/lucy/document.go
index 1221810..1d8c289 100644
--- a/go/lucy/document.go
+++ b/go/lucy/document.go
@@ -48,3 +48,24 @@ func fetchDocFields(d *C.lucy_Doc) map[string]interface{} {
        }
        return fieldsGo
 }
+
+func (d *DocIMP) GetFields() map[string]interface{} {
+       self := (*C.lucy_Doc)(clownfish.Unwrap(d, "d"))
+       return fetchDocFields(self)
+}
+
+func (d *DocIMP) SetFields(fields map[string]interface{}) {
+       self := (*C.lucy_Doc)(clownfish.Unwrap(d, "d"))
+       ivars := C.lucy_Doc_IVARS(self)
+       oldID := uintptr(unsafe.Pointer(ivars.fields))
+       newFieldsID := registry.store(fields)
+       ivars.fields = unsafe.Pointer(newFieldsID)
+       registry.delete(oldID)
+}
+
+func (d *DocIMP) FieldNames() []string {
+       self := (*C.lucy_Doc)(unsafe.Pointer(d.TOPTR()))
+       fieldsCF := C.LUCY_Doc_Field_Names(self)
+       defer C.cfish_decref(unsafe.Pointer(fieldsCF))
+       return vecToStringSlice(fieldsCF)
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/91dd3894/go/lucy/document_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/document_test.go b/go/lucy/document_test.go
new file mode 100644
index 0000000..2cf6313
--- /dev/null
+++ b/go/lucy/document_test.go
@@ -0,0 +1,74 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package lucy
+
+import "testing"
+import "reflect"
+import 
"git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
+
+func TestDocMisc(t *testing.T) {
+       doc := NewDoc(1)
+       if got := doc.GetDocID(); got != 1 {
+               t.Errorf("GetDocID: %d", got)
+       }
+       doc.SetDocID(42)
+       if got := doc.GetDocID(); got != 42 {
+               t.Errorf("Set/GetDocID: %d", got)
+       }
+       fields := map[string]interface{}{"content": "foo"}
+       doc.SetFields(fields)
+       if got, ok := doc.Extract("content").(string); !ok || got != "foo" {
+               t.Errorf("Extract: %v", got)
+       }
+       doc.Store("content", "bar")
+       retrievedFields := doc.GetFields()
+       if got, ok := retrievedFields["content"].(string); !ok || got != "bar" {
+               t.Errorf("Store/GetFields: %v", got)
+       }
+       if got := doc.GetSize(); got != 1 {
+               t.Errorf("GetSize: %d", got)
+       }
+       if got := doc.FieldNames(); !reflect.DeepEqual(got, 
[]string{"content"}) {
+               t.Errorf("FieldNames: %v", got)
+       }
+       checkDocSerialize(t, doc)
+       checkdocDumpLoad(t, doc)
+}
+
+func checkDocSerialize(t *testing.T, doc Doc) {
+       folder := NewRAMFolder("")
+       outStream, _ := folder.OpenOut("foo")
+       doc.serialize(outStream)
+       outStream.Close()
+       inStream, _ := folder.OpenIn("foo")
+       dupe := clownfish.GetClass(doc).MakeObj().(Doc).deserialize(inStream)
+       if !doc.Equals(dupe) {
+               t.Errorf("Unsuccessful serialization round trip -- expected 
'%v', got '%v'",
+                                doc.ToString(), dupe.ToString())
+       }
+}
+
+func checkdocDumpLoad(t *testing.T, doc Doc) {
+       t.Skip("Dump/Load are TODO")
+       return
+       dupe := clownfish.GetClass(doc).MakeObj().(Doc)
+       dupe = dupe.load(doc.dump()).(Doc)
+       if !doc.Equals(dupe) {
+               t.Errorf("Unsuccessful dump/load round trip -- expected '%v', 
got '%v'",
+                                doc.ToString(), dupe.ToString())
+       }
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/91dd3894/go/lucy/lucy.go
----------------------------------------------------------------------
diff --git a/go/lucy/lucy.go b/go/lucy/lucy.go
index 585a192..03307bb 100644
--- a/go/lucy/lucy.go
+++ b/go/lucy/lucy.go
@@ -273,7 +273,7 @@ func GOLUCY_Doc_init(d *C.lucy_Doc, fields unsafe.Pointer, 
docID C.int32_t) *C.l
 
 //export GOLUCY_Doc_Set_Fields
 func GOLUCY_Doc_Set_Fields(d *C.lucy_Doc, fields unsafe.Pointer) {
-       panic(clownfish.NewErr("Set_Fields unsupported in Go bindings"))
+       panic(clownfish.NewErr("Set_Fields unsupported from C-space in Go"))
 }
 
 //export GOLUCY_Doc_Get_Size

Reply via email to