Skip to content

Commit

Permalink
add when not present predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
tzhao-viant committed May 21, 2024
1 parent 7c62613 commit f7e3ba8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
require (
firebase.google.com/go/v4 v4.14.0
github.com/viant/structology v0.5.6-0.20231127181208-736f8ad06193
github.com/viant/tagly v0.2.1-0.20240417022938-8b3b65de980b
github.com/viant/tagly v0.2.1-0.20240521205717-55de744e893c
github.com/viant/xdatly v0.3.1-0.20240503144018-bce51af60365
github.com/viant/xdatly/extension v0.0.0-20231013204918-ecf3c2edf259
github.com/viant/xdatly/handler v0.0.0-20240503144018-bce51af60365
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,8 @@ github.com/viant/structology v0.5.6-0.20231127181208-736f8ad06193 h1:wKs4BXSTV0V
github.com/viant/structology v0.5.6-0.20231127181208-736f8ad06193/go.mod h1:oGgckfRCArsyzFpRqem+U0EEyzxjoNx3o21MXO43uh0=
github.com/viant/structql v0.4.2-0.20240406183616-cff48e18d922 h1:w/h5njGTM1Pxw2WHeYsS391btY+bXr6XK3IdBFecenU=
github.com/viant/structql v0.4.2-0.20240406183616-cff48e18d922/go.mod h1:1ko0M6tIUv2nafHo94cnUGG65GmAXYmkEcgeUU/+kCM=
github.com/viant/tagly v0.2.1-0.20240417022938-8b3b65de980b h1:/gjKZOnfZXzIRJSkfOIBP3OmCZSG3D5VimfXG7k/wkg=
github.com/viant/tagly v0.2.1-0.20240417022938-8b3b65de980b/go.mod h1:vV8QgJkhug+X+qyKds8av0fhjD+4u7IhNtowL1KGQ5A=
github.com/viant/tagly v0.2.1-0.20240521205717-55de744e893c h1:j16wc8v05VpZXLaBDqbLZOAjSyN/XrmzjqgcPm6K2FI=
github.com/viant/tagly v0.2.1-0.20240521205717-55de744e893c/go.mod h1:vV8QgJkhug+X+qyKds8av0fhjD+4u7IhNtowL1KGQ5A=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/viant/toolbox v0.34.5/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/viant/toolbox v0.36.0 h1:UYKY3WokI/hn4IBDz8fLe8mYt9YQiJf/q+GCRt7HpUY=
Expand Down
1 change: 1 addition & 0 deletions view/extension/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func InitRegistry() {
PredicateCriteriaIn: NewInCriteriaPredicate(),
PredicateCriteriaNotIn: NewNotInCriteriaPredicate(),
PredicateWhenPresent: NewWhenPresent(),
PredicateWhenNotPresent: NewWhenNotPresent(),
},
},
Docs: docs.New(),
Expand Down
18 changes: 18 additions & 0 deletions view/extension/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
PredicateBetween = "between"
PredicateDuration = "duration"
PredicateWhenPresent = "when_present"
PredicateWhenNotPresent = "when_not_present"
)

type (
Expand Down Expand Up @@ -427,6 +428,10 @@ func NewWhenPresent() *Predicate {
return newWhenPredicate(PredicateWhenPresent)
}

func NewWhenNotPresent() *Predicate {
return NewWhenNotPredicate(PredicateWhenNotPresent)
}

func newExistsPredicate(name string, withCriteria bool, negated bool) *Predicate {
args := []*predicate.NamedArgument{
{
Expand Down Expand Up @@ -519,3 +524,16 @@ func newWhenPredicate(name string) *Predicate {
},
}
}

func NewWhenNotPredicate(name string) *Predicate {
condition := `#if(!$HasFilterValue) ${Criterion} #end`
return &Predicate{
Template: &predicate.Template{
Name: name,
Source: " " + condition,
Args: []*predicate.NamedArgument{
{Name: "Criterion", Position: 0},
},
},
}
}
22 changes: 17 additions & 5 deletions view/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ type (
handler codec.PredicateHandler
}

predicateEvaluator struct {
PredicateEvaluator struct {
ctx *expand.Variable
evaluator *expand.Evaluator
valueState *expand.NamedVariable
hasValueState *expand.NamedVariable
}
)

func (e *predicateEvaluator) Compute(ctx context.Context, value interface{}) (*codec.Criteria, error) {
func (e *PredicateEvaluator) Compute(ctx context.Context, value interface{}) (*codec.Criteria, error) {
cuxtomCtx, ok := ctx.Value(expand.PredicateCtx).(*expand.Context)
if !ok {
panic("not found custom ctx")
Expand All @@ -68,12 +68,24 @@ func (e *predicateEvaluator) Compute(ctx context.Context, value interface{}) (*c
return criteria, nil
}

func (e *predicateEvaluator) Evaluate(ctx *expand.Context, state *structology.State, value interface{}) (*expand.State, error) {
func (e *PredicateEvaluator) Evaluate(ctx *expand.Context, state *structology.State, value interface{}) (*expand.State, error) {

hasValue := value != nil
if value != nil {
switch actual := value.(type) {
case []int:
hasValue = actual != nil
case *int:
hasValue = actual != nil
case *string:
hasValue = actual != nil
}
}
return e.evaluator.Evaluate(ctx,
expand.WithParameterState(state),
expand.WithNamedVariables(
e.valueState.New(value),
e.hasValueState.New(value != nil),
e.hasValueState.New(hasValue),
),
expand.WithCustomContext(e.ctx),
)
Expand Down Expand Up @@ -133,7 +145,7 @@ func (p *predicateEvaluatorProvider) new(predicateConfig *extension.PredicateCon
Value: dst,
}

return &predicateEvaluator{
return &PredicateEvaluator{
ctx: customCtx,
evaluator: p.evaluator,
valueState: p.state,
Expand Down

0 comments on commit f7e3ba8

Please sign in to comment.