riyaz-ali / sqlite

Golang library to build sqlite extensions :rocket:
https://go.riyazali.net/sqlite
Other
164 stars 5 forks source link

Register targets must be pointers #15

Closed anacrolix closed 1 year ago

anacrolix commented 2 years ago

I don't think it's documented, but the things you register must be pointer types, or at least I think it's that way (due to mattn/pointer). Let me know if I have this wrong.

riyaz-ali commented 1 year ago

Apologies for being 10 months late to this issue 😅

For future reference, it turns out that is not the case. mattn/go-pointer works just fine even with non-pointer types (I guess because it stores the interface{} structure and not the actual object but I don't have enough expertise to comment on this).

What's more important to realise is that the normal caveats of value vs. pointer receiver applies.

Here's a modified version of the slightly Upper function snippet from func_scalar_test.go:

type Upper struct{ c int }

func (m Upper) Args() int           { return 1 }
func (m Upper) Deterministic() bool { return true }
func (m Upper) Apply(ctx *Context, values ...Value) {
    _, _ = fmt.Fprintf(os.Stderr, "c: %d\n", m.c)
    m.c += 1 // updates m.c in the copy 
    ctx.ResultText(strings.ToUpper(values[0].Text()))
}