Documentation
¶
Index ¶
Examples ¶
Constants ¶
const ( KeywordEq string = "$eq" KeywordNe string = "$ne" // Array KeywordIn string = "$in" KeywordNin string = "$nin" KeywordAll string = "$all" KeywordElemMatch string = "$elemMatch" KeywordAllElemMatch string = "$allElemMatch" // Logic KeywordOr string = "$or" KeywordAnd string = "$and" KeywordNor string = "$nor" KeywordNand string = "$nand" // Comparison KeywordGt string = "$gt" KeywordGte string = "$gte" KeywordLt string = "$lt" KeywordLte string = "$lte" // Element KeywordExists string = "$exists" KeywordSize string = "$size" KeywordType string = "$type" // Evaluation KeywordMod string = "$mod" KeywordRegex string = "$regex" KeywordOptions string = "$options" KeywordText string = "$text" KeywordSearch string = "$search" KeywordComment string = "$comment" KeywordMax string = "$max" KeywordMin string = "$min" KeywordMaxDistance string = "$maxDistance" // Bitwise KeywordBitwiseAnd string = "$bitwiseAnd" KeywordBitwiseOr string = "$bitwiseOr" KeywordBitwiseXor string = "$bitwiseXor" KeywordBitwiseNot string = "$bitwiseNot" // Geospatial KeywordGeoIntersects string = "$geoIntersects" KeywordGeoWithin string = "$geoWithin" KeywordNear string = "$near" KeywordWithin string = "$within" // Projection KeywordProjection string = "$projection" // Update KeywordUnset string = "$unset" KeywordSet string = "$set" KeywordSetOnInsert string = "$setOnInsert" // Array KeywordArrayFilters string = "$arrayFilters" // Text KeywordTextScore string = "$textScore" KeywordCurrentDate string = "$currentDate" )
Variables ¶
var ( // Set is updater for $set query Set = getGlobalOperationWithKeywordConstructor(KeywordSet) // SetOnInsert is updater for $setOnInsert query SetOnInsert = getGlobalOperationWithKeywordConstructor(KeywordSetOnInsert) // Unset is updater for $unset query Unset = getGlobalOperationWithKeywordConstructor(KeywordUnset) )
var ( // Or is updater for $or query Or = getLogicalOperationWithKeywordConstructor(KeywordOr) // And is updater for $and query And = getLogicalOperationWithKeywordConstructor(KeywordAnd) // Nor is updater for $nor query Nor = getLogicalOperationWithKeywordConstructor(KeywordNor) // Nand is updater for $nand query Nand = getLogicalOperationWithKeywordConstructor(KeywordNand) )
Functions ¶
Types ¶
type Checker ¶
Checker is an interface that defines how to check a filter has to be eliminated or not.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/ahmetcanozcan/fet" ) func main() { filter := fet.Build( fet.Field("username").Eq("dave", fet.IfNotEmpty), fet.Field("cardId").Eq("", fet.IfNotEmpty), fet.Field("car").Eq(nil, fet.IfNotNil), ) b, _ := json.Marshal(filter) fmt.Println(string(b)) }
Output: {"username":{"$eq":"dave"}}
var IfNotEmpty Checker = CheckerFunc(func(key string, value interface{}) bool {
return value != ""
})
IfNotEmpty checks if the given value is not a empty empty string.
var IfNotNil Checker = CheckerFunc(func(key string, value interface{}) bool { return value != nil })
IfNotNil checks if the given value is not nil.
var IfNotZero Checker = CheckerFunc(func(key string, value interface{}) bool {
return value != 0
})
IfNotZero checks if the given value is not zero.
type CheckerFunc ¶ added in v0.3.0
func (CheckerFunc) Check ¶ added in v0.3.0
func (cf CheckerFunc) Check(key string, value interface{}) bool
type M ¶
type M map[string]interface{}
M is representation of a MongoDB query, it is fet equivalent of `bson.M`.
func Apply ¶
Apply applies the given updaters to the given filter.
Example:
filter := fet.Apply( m, fet.Field("firstname").Eq("dave"), fet.Field("lastname").Eq("bowman"), )
func Build ¶
Build creates a filter and apply the given updaters to it.
Example:
filter := fet.Build( fet.Field("firstname").Eq("dave"), fet.Field("lastname").Eq("bowman"), )
Example ¶
package main import ( "encoding/json" "fmt" "github.com/ahmetcanozcan/fet" ) func main() { filter := fet.Build( fet.Field("name").Eq("dave"), fet.Field("age").Is(10), ) b, _ := json.Marshal(filter) fmt.Println(string(b)) }
Output: {"age":10,"name":{"$eq":"dave"}}
func BuildSet ¶
BuildSet combines Build and Set. is equivalent to:
fet.Build(fet.Set(updaters...))
Example ¶
package main import ( "encoding/json" "fmt" "github.com/ahmetcanozcan/fet" ) func main() { query := fet.BuildSet( fet.Field("age").Is(18), ) b, _ := json.Marshal(query) fmt.Println(string(b)) }
Output: {"$set":{"age":18}}
type StructUpdater ¶ added in v0.3.0
type StructUpdater struct {
// contains filtered or unexported fields
}
StructUpdater is and updater for struct
func Struct ¶ added in v0.3.0
func Struct(s interface{}) *StructUpdater
Struct constructs a StructUpdater for the given struct. if given value is nil or not a struct, panic.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/ahmetcanozcan/fet" ) func main() { type User struct { Name string `bson:"name"` Age int Phone string } u := User{ Name: "Dave Bowman", Age: 18, Phone: "", } filter := fet.Struct(u). Pick("Name"). Pick("Phone", fet.IfNotEmpty). Build() b, _ := json.Marshal(filter) fmt.Println(string(b)) }
Output: {"name":"Dave Bowman"}
func (*StructUpdater) Build ¶ added in v0.4.0
func (s *StructUpdater) Build() M
func (*StructUpdater) Pick ¶ added in v0.3.0
func (s *StructUpdater) Pick(name string, checkers ...Checker) *StructUpdater
Pick picks the given field from struct with the given checkers. if the struct field has `bson` tag, otherwise use fieldname Example:
fet.Struct(user).Pick("firstname").Pick("lastname")
func (*StructUpdater) PickAll ¶ added in v0.3.0
func (s *StructUpdater) PickAll(checkers ...Checker) *StructUpdater
PickAll picks all the fields from the given struct. use given checkers to all fields.
func (*StructUpdater) Update ¶ added in v0.3.0
func (s *StructUpdater) Update(m M)