q

package
Version: v2.1.2+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2018 License: MIT Imports: 8 Imported by: 149

Documentation

Overview

Package q contains a list of Matchers used to compare struct fields with values

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnknownField = errors.New("unknown field")

ErrUnknownField is returned when an unknown field is passed.

Functions

This section is empty.

Types

type FieldMatcher

type FieldMatcher interface {
	MatchField(v interface{}) (bool, error)
}

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 And

func And(matchers ...Matcher) Matcher

And matcher, checks if all of the given matchers matches the record

func Eq

func Eq(field string, v interface{}) Matcher

Eq matcher, checks if the given field is equal to the given value

func EqF

func EqF(field1, field2 string) Matcher

EqF matcher, checks if the given field is equal to the given field

func Gt

func Gt(field string, v interface{}) Matcher

Gt matcher, checks if the given field is greater than the given value

func GtF

func GtF(field1, field2 string) Matcher

GtF matcher, checks if the given field is greater than the given field

func Gte

func Gte(field string, v interface{}) Matcher

Gte matcher, checks if the given field is greater than or equal to the given value

func GteF

func GteF(field1, field2 string) Matcher

GteF matcher, checks if the given field is greater than or equal to the given field

func In added in v0.7.0

func In(field string, v interface{}) Matcher

In matcher, checks if the given field matches one of the value of the given slice. v must be a slice.

func Lt

func Lt(field string, v interface{}) Matcher

Lt matcher, checks if the given field is lesser than the given value

func LtF

func LtF(field1, field2 string) Matcher

LtF matcher, checks if the given field is lesser than the given field

func Lte

func Lte(field string, v interface{}) Matcher

Lte matcher, checks if the given field is lesser than or equal to the given value

func LteF

func LteF(field1, field2 string) Matcher

LteF matcher, checks if the given field is lesser than or equal to the given field

func NewField2FieldMatcher

func NewField2FieldMatcher(field1, field2 string, tok token.Token) Matcher

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 Not added in v0.7.0

func Not(matchers ...Matcher) Matcher

Not matcher, checks if all of the given matchers return false

func Or

func Or(matchers ...Matcher) Matcher

Or matcher, checks if at least one of the given matchers matches the record

func Re

func Re(field string, re string) Matcher

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.

func StrictEq

func StrictEq(field string, v interface{}) Matcher

StrictEq matcher, checks if the given field is deeply equal to the given value

func True

func True() Matcher

True matcher, always returns true

type ValueMatcher

type ValueMatcher interface {
	// MatchValue tests if the given reflect.Value matches.
	// It is useful when the reflect.Value of an object already exists.
	MatchValue(*reflect.Value) (bool, error)
}

A ValueMatcher is used to test against a reflect.Value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL