-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Resolves #17 - Add support for Mongo * Update README.md Co-authored-by: dazadep <62723515+dazadep@users.noreply.github.com> --------- Co-authored-by: dazadep <62723515+dazadep@users.noreply.github.com>
- Loading branch information
1 parent
c3639d7
commit fe2d29e
Showing
6 changed files
with
553 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package epsearchast_v3_mongo | ||
|
||
import ( | ||
epsearchast_v3 "github.com/elasticpath/epcc-search-ast-helper/external/epsearchast/v3" | ||
"regexp" | ||
"strings" | ||
) | ||
import "go.mongodb.org/mongo-driver/bson" | ||
|
||
type DefaultMongoQueryBuilder struct{} | ||
|
||
var _ epsearchast_v3.SemanticReducer[bson.D] = (*DefaultMongoQueryBuilder)(nil) | ||
|
||
func (d DefaultMongoQueryBuilder) PostVisitAnd(rs []*bson.D) (*bson.D, error) { | ||
// https://www.mongodb.com/docs/manual/reference/operator/query/and/ | ||
return &bson.D{ | ||
{"$and", | ||
rs, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (d DefaultMongoQueryBuilder) VisitIn(args ...string) (*bson.D, error) { | ||
// https://www.mongodb.com/docs/manual/reference/operator/query/in/ | ||
return &bson.D{{args[0], bson.D{{"$in", args[1:]}}}}, nil | ||
} | ||
|
||
func (d DefaultMongoQueryBuilder) VisitEq(first, second string) (*bson.D, error) { | ||
// https://www.mongodb.com/docs/manual/reference/operator/query/eq/#std-label-eq-usage-examples | ||
// This is equivalent to { key: value } but makes for easier tests. | ||
return &bson.D{{first, bson.D{{"$eq", second}}}}, nil | ||
} | ||
|
||
func (d DefaultMongoQueryBuilder) VisitLe(first, second string) (*bson.D, error) { | ||
// https://www.mongodb.com/docs/manual/reference/operator/query/lte/ | ||
return &bson.D{{first, bson.D{{"$lte", second}}}}, nil | ||
} | ||
|
||
func (d DefaultMongoQueryBuilder) VisitLt(first, second string) (*bson.D, error) { | ||
// https://www.mongodb.com/docs/manual/reference/operator/query/lt/ | ||
return &bson.D{{first, bson.D{{"$lt", second}}}}, nil | ||
} | ||
|
||
func (d DefaultMongoQueryBuilder) VisitGe(first, second string) (*bson.D, error) { | ||
// https://www.mongodb.com/docs/manual/reference/operator/query/gte/ | ||
return &bson.D{{first, bson.D{{"$gte", second}}}}, nil | ||
} | ||
|
||
func (d DefaultMongoQueryBuilder) VisitGt(first, second string) (*bson.D, error) { | ||
// https://www.mongodb.com/docs/manual/reference/operator/query/gt/ | ||
return &bson.D{{first, bson.D{{"$gt", second}}}}, nil | ||
} | ||
|
||
func (d DefaultMongoQueryBuilder) VisitLike(first, second string) (*bson.D, error) { | ||
return &bson.D{{first, bson.D{{"$regex", d.ProcessLikeWildcards(second)}}}}, nil | ||
} | ||
|
||
func (d DefaultMongoQueryBuilder) ProcessLikeWildcards(valString string) string { | ||
if valString == "*" { | ||
return "^.*$" | ||
} | ||
|
||
var startsWithStar = strings.HasPrefix(valString, "*") | ||
var endsWithStar = strings.HasSuffix(valString, "*") | ||
if startsWithStar { | ||
valString = valString[1:] | ||
} | ||
if endsWithStar { | ||
valString = valString[:len(valString)-1] | ||
} | ||
|
||
valString = regexp.QuoteMeta(valString) | ||
|
||
if startsWithStar { | ||
valString = ".*" + valString | ||
} | ||
if endsWithStar { | ||
valString += ".*" | ||
} | ||
return "^" + valString + "$" | ||
} |
Oops, something went wrong.