Add Go wrappers for Clownfish basic types.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/56636641 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/56636641 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/56636641 Branch: refs/heads/master Commit: 56636641d9447c779da5ed661f3f60e2af32aae6 Parents: 3d11dea Author: Marvin Humphrey <[email protected]> Authored: Wed Nov 5 10:58:27 2014 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Sun Mar 15 19:01:11 2015 -0700 ---------------------------------------------------------------------- runtime/go/clownfish/clownfish.go | 73 +++++++++++++++++++++++++++-- runtime/go/clownfish/clownfish_test.go | 6 ++- 2 files changed, 74 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/56636641/runtime/go/clownfish/clownfish.go ---------------------------------------------------------------------- diff --git a/runtime/go/clownfish/clownfish.go b/runtime/go/clownfish/clownfish.go index 7859709..0165223 100644 --- a/runtime/go/clownfish/clownfish.go +++ b/runtime/go/clownfish/clownfish.go @@ -33,13 +33,78 @@ package clownfish */ import "C" +import "runtime" +import "unsafe" func init() { C.cfish_bootstrap_parcel() } -// Temporary test-only routine. -func DoStuff() { - hash := C.cfish_Hash_new(C.uint32_t(0)) - C.cfish_dec_refcount(unsafe.Pointer(hash)) +type Obj interface { + ToPtr() unsafe.Pointer +} + +type Err struct { + ref *C.cfish_Err +} + +type String struct { + ref *C.cfish_String +} + +type ByteBuf struct { + ref *C.cfish_ByteBuf +} + +type Hash struct { + ref *C.cfish_Hash +} + +type VArray struct { + ref *C.cfish_VArray +} + +type Class struct { + ref *C.cfish_Class +} + +type Method struct { + ref *C.cfish_Method +} + +type LockFreeRegistry struct { + ref *C.cfish_LockFreeRegistry +} + +func NewString(goString string) String { + str := C.CString(goString) + len := C.size_t(len(goString)) + obj := String{ + C.cfish_Str_new_steal_utf8(str, len), + } + runtime.SetFinalizer(&obj, (*String).callDecRef) + return obj +} + +func (obj *String) callDecRef() { + C.cfish_dec_refcount(unsafe.Pointer(obj.ref)) + obj.ref = nil +} + +func (obj *String) ToPtr() unsafe.Pointer { + return unsafe.Pointer(obj.ref) +} + +func CFStringToGo(ptr unsafe.Pointer) string { + cfString := (*C.cfish_String)(ptr) + if cfString == nil { + return "" + } + if !C.CFISH_Str_Is_A(cfString, C.CFISH_STRING) { + cfString := C.CFISH_Str_To_String(cfString) + defer C.cfish_dec_refcount(unsafe.Pointer(cfString)) + } + data := C.CFISH_Str_Get_Ptr8(cfString) + size := C.int(C.CFISH_Str_Get_Size(cfString)) + return C.GoStringN(data, size) } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/56636641/runtime/go/clownfish/clownfish_test.go ---------------------------------------------------------------------- diff --git a/runtime/go/clownfish/clownfish_test.go b/runtime/go/clownfish/clownfish_test.go index 3872b92..1709dfc 100644 --- a/runtime/go/clownfish/clownfish_test.go +++ b/runtime/go/clownfish/clownfish_test.go @@ -20,5 +20,9 @@ import "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish" import "testing" func TestStuff(t *testing.T) { - clownfish.DoStuff() + cfString := clownfish.NewString("foo") + goString := clownfish.CFStringToGo(cfString.ToPtr()) + if goString != "foo" { + t.Error("Round-tripping strings failed") + } }
