Documentation ¶
Overview ¶
Package q contains a list of Matchers used to compare struct fields with values
Index ¶
- Variables
- type FieldMatcher
- type Matcher
- func And(matchers ...Matcher) Matcher
- func Eq(field string, v interface{}) Matcher
- func EqF(field1, field2 string) Matcher
- func Gt(field string, v interface{}) Matcher
- func GtF(field1, field2 string) Matcher
- func Gte(field string, v interface{}) Matcher
- func GteF(field1, field2 string) Matcher
- func In(field string, v interface{}) Matcher
- func Lt(field string, v interface{}) Matcher
- func LtF(field1, field2 string) Matcher
- func Lte(field string, v interface{}) Matcher
- func LteF(field1, field2 string) Matcher
- func NewField2FieldMatcher(field1, field2 string, tok token.Token) Matcher
- func NewFieldMatcher(field string, fm FieldMatcher) Matcher
- func Not(matchers ...Matcher) Matcher
- func Or(matchers ...Matcher) Matcher
- func Re(field string, re string) Matcher
- func StrictEq(field string, v interface{}) Matcher
- func True() Matcher
- type ValueMatcher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnknownField = errors.New("unknown field")
ErrUnknownField is returned when an unknown field is passed.
Functions ¶
This section is empty.
Types ¶
type FieldMatcher ¶
FieldMatcher can be used in NewFieldMatcher as a simple way to create the most common Matcher: A Matcher that evaluates one field's value. For more complex scenarios, implement the Matcher interface directly.
type Matcher ¶
type Matcher interface { // Match is used to test the criteria against a structure. Match(interface{}) (bool, error) }
A Matcher is used to test against a record to see if it matches.
func In ¶ added in v0.7.0
In matcher, checks if the given field matches one of the value of the given slice. v must be a slice.
func NewField2FieldMatcher ¶
NewField2FieldMatcher creates a Matcher for a given field1 and field2.
func NewFieldMatcher ¶
func NewFieldMatcher(field string, fm FieldMatcher) Matcher
NewFieldMatcher creates a Matcher for a given field.
func Re ¶
Re creates a regexp matcher. It checks if the given field matches the given regexp. Note that this only supports fields of type string or []byte.
Example ¶
package main import ( "fmt" "log" "time" "os" "io/ioutil" "path/filepath" "strings" "github.com/asdine/storm" "github.com/asdine/storm/q" ) func main() { dir, db := prepareDB() defer os.RemoveAll(dir) defer db.Close() var users []User // Find all users with name that starts with the letter D. if err := db.Select(q.Re("Name", "^D")).Find(&users); err != nil { log.Println("error: Select failed:", err) return } // Donald and Dilbert fmt.Println("Found", len(users), "users.") } type User struct { ID int `storm:"id,increment"` Group string `storm:"index"` Email string `storm:"unique"` Name string Age int `storm:"index"` CreatedAt time.Time `storm:"index"` } func prepareDB() (string, *storm.DB) { dir, _ := ioutil.TempDir(os.TempDir(), "storm") db, _ := storm.Open(filepath.Join(dir, "storm.db")) for i, name := range []string{"John", "Norm", "Donald", "Eric", "Dilbert"} { email := strings.ToLower(name + "@provider.com") user := User{ Group: "staff", Email: email, Name: name, Age: 21 + i, CreatedAt: time.Now(), } err := db.Save(&user) if err != nil { log.Fatal(err) } } return dir, db }
Output: Found 2 users.