func goToHash(value interface{}, nullable bool) unsafe.Pointer { switch v := value.(type) { case map[string]interface{}: if v == nil { if nullable { return nil } } else { size := len(v) hash := C.cfish_Hash_new(C.size_t(size)) for key, val := range v { newVal := GoToClownfish(val, nil, true) keySize := len(key) keyStr := C.CString(key) cfKey := C.cfish_Str_new_steal_utf8(keyStr, C.size_t(keySize)) defer C.cfish_dec_refcount(unsafe.Pointer(cfKey)) C.CFISH_Hash_Store(hash, cfKey, (*C.cfish_Obj)(newVal)) } return unsafe.Pointer(hash) } case Obj: certifyCF(v, C.CFISH_HASH, nullable) return unsafe.Pointer(C.cfish_incref(unsafe.Pointer(v.TOPTR()))) } mess := fmt.Sprintf("Can't convert %T to clownfish.Hash", value) panic(NewErr(mess)) }
func (obj *IndexerIMP) AddDoc(doc interface{}) error { self := ((*C.lucy_Indexer)(unsafe.Pointer(obj.TOPTR()))) stockDoc := C.LUCY_Indexer_Get_Stock_Doc(self) docFields := (*C.cfish_Hash)(C.LUCY_Doc_Get_Fields(stockDoc)) C.CFISH_Hash_Clear(docFields) // TODO: Support map as doc in addition to struct as doc. // Get reflection value and type for the supplied struct. var docValue reflect.Value if reflect.ValueOf(doc).Kind() == reflect.Ptr { temp := reflect.ValueOf(doc).Elem() if temp.Kind() == reflect.Struct { docValue = temp } } if docValue == (reflect.Value{}) { mess := fmt.Sprintf("Doc not struct pointer: %v", reflect.TypeOf(doc)) return clownfish.NewErr(mess) } docType := docValue.Type() for i := 0; i < docValue.NumField(); i++ { field := docType.Field(i).Name value := docValue.Field(i).String() fieldC := obj.findFieldC(field) valueC := clownfish.NewString(value) C.CFISH_Hash_Store(docFields, (*C.cfish_String)(unsafe.Pointer(fieldC)), C.cfish_inc_refcount(unsafe.Pointer(valueC.TOPTR()))) } // TODO create an additional method AddDocWithBoost which allows the // client to supply `boost`. boost := 1.0 err := clownfish.TrapErr(func() { C.LUCY_Indexer_Add_Doc(self, stockDoc, C.float(boost)) }) return err }
//export GOLUCY_Doc_Store func GOLUCY_Doc_Store(d *C.lucy_Doc, field *C.cfish_String, value *C.cfish_Obj) { ivars := C.lucy_Doc_IVARS(d) hash := (*C.cfish_Hash)(ivars.fields) C.CFISH_Hash_Store(hash, field, C.cfish_inc_refcount(unsafe.Pointer(value))) }