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 ¶
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"
"path/filepath"
"strings"
"github.com/vbargl/rainstorm/v6"
"github.com/vbargl/rainstorm/v6/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 `rainstorm:"id,increment"`
Group string `rainstorm:"index"`
Email string `rainstorm:"unique"`
Name string
Age int `rainstorm:"index"`
CreatedAt time.Time `rainstorm:"index"`
}
func prepareDB() (string, *rainstorm.DB) {
dir, _ := os.MkdirTemp(os.TempDir(), "rainstorm")
db, _ := rainstorm.Open(filepath.Join(dir, "rainstorm.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.