chaisql / chai

Modern embedded SQL database
MIT License
1.54k stars 89 forks source link

StructScan not working when struct has slice of pointer types #341

Closed JustinJudd closed 3 years ago

JustinJudd commented 3 years ago

What version of Genji are you using?

$ genji version 
Genji v0.10.0
Genji CLI v0.10.0

Does this issue reproduce with the latest release?

Yes

What did you do?

package main

import (
        "fmt"
        "log"

        "github.com/genjidb/genji"
        "github.com/genjidb/genji/document"
)

type User struct {
        Name    string
        Reviews []*Review
}

type Review struct {
        Item   string
        Review string
        Rating int
}

func main() {

        db, err := genji.Open(":memory:")
        if err != nil {
                log.Fatal(err)
        }

        db.Exec("CREATE table users")

        u := User{
                Name: "Foo",
                Reviews: []*Review{
                        &Review{"Harry Potter", "Great Book", 9},
                },
        }

        db.Exec("INSERT INTO users VALUES ?", &u)

        var users []User
        res, err := db.Query("SELECT * FROM users")
        if err != nil {
                log.Fatal(err)
        }

        defer res.Close()
        err = res.Iterate(func(d document.Document) error {
                var u User
                err = document.StructScan(d, &u)
                if err != nil {
                        return err
                }
                users = append(users, u)
                return nil
        })
        if err != nil {
                log.Fatal(err)
        }

        fmt.Println("Users:", users)
}

What did you expect to see?

Users: [{Foo [0xc0000a4ab0]}]

What did you see instead?

panic: reflect.Set: value of type **main.Review is not assignable to type *main.Review

What Go version and environment are you using?

$ go version
go version go1.15.7 linux/amd64
go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/justin/.cache/go-build"
GOENV="/home/justin/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/justin/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/justin/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build523484142=/tmp/go-build -gno-record-gcc-switches"
asdine commented 3 years ago

Thanks @JustinJudd! I have written a fix, I will create a patch release today

asdine commented 3 years ago

Patch: https://github.com/genjidb/genji/releases/tag/v0.10.1

JustinJudd commented 3 years ago

Thanks for such a quick response!