asdine / storm

Simple and powerful toolkit for BoltDB
MIT License
2.07k stars 139 forks source link

Having trouble getting q.In to work #249

Open u89012 opened 5 years ago

u89012 commented 5 years ago

Hello everyone! Love the package! But stuck with a weird problem -- can't get q.In to work, perhaps I'm doing something wrong, or perhaps I've hit a bug in the library, can someone please help?

package main

import (
    "fmt"
    "log"

    "github.com/asdine/storm"
    "github.com/asdine/storm/q"
)

type Foo struct {
    Id    int64    `storm:"id,increment"`
    Title string   `storm:"index"`
    Tags  []string `storm:"index"`
}

func main() {
    if db, e := storm.Open("db/test.db"); e != nil {
        log.Fatal("Couldn't open database")
    } else {
        defer db.Close()

        db.Save(&Foo{Title: "Lorem", Tags: []string{"foo", "bar"}})
        db.Save(&Foo{Title: "Ipsum", Tags: []string{"foo", "bax"}})
        db.Save(&Foo{Title: "Doler", Tags: []string{"quux", "baz"}})

        {
            var a []Foo
            if e := db.Select().Find(&a); e != nil {
                panic(e)
            } else {
                fmt.Println(a)
            }
        }

        {
            var a Foo
            if e := db.One("Title", "Lorem", &a); e != nil {
                panic(e)
            }
        }

        { 
            var a []Foo
            if e := db.Select(q.In("Tags", []string{"quux"})).Find(&a); e != nil {
                panic(e) // yikes
            }
        }
    }
}
mwmahlberg commented 5 years ago

As far as I understood it, q.In is for comparing wether a single value field (not a slice) matches one of the given values in the slice.