From 694158b6f67a5d80f027bf7901e452ceff58fdfe Mon Sep 17 00:00:00 2001 From: Vishal Pawar Date: Fri, 26 Jun 2020 18:12:11 +0530 Subject: [PATCH 1/3] Support added for conditional filtering --- activity/filter/activity.go | 19 ++++++++++------- activity/filter/activity_test.go | 36 ++++++++++++++++++++++++++++++++ activity/filter/descriptor.json | 6 +++++- activity/filter/filters.go | 9 +++++++- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/activity/filter/activity.go b/activity/filter/activity.go index 3eafbdf..89ce5e4 100755 --- a/activity/filter/activity.go +++ b/activity/filter/activity.go @@ -8,19 +8,21 @@ import ( ) const ( - ivValue = "value" - ovFiltered = "filtered" - ovValue = "value" + ivValue = "value" + ivCondition = "condition" + ovFiltered = "filtered" + ovValue = "value" ) //we can generate json from this! - we could also create a "validate-able" object from this type Settings struct { - Type string `md:"type,allowed(non-zero)"` + Type string `md:"type,allowed(non-zero,conditional)"` ProceedOnlyOnEmit bool } type Input struct { - Value interface{} `md:"value"` + Value interface{} `md:"value"` + Condition bool `md:"condition"` } type Output struct { @@ -45,6 +47,8 @@ func New(ctx activity.InitContext) (activity.Activity, error) { if s.Type == "non-zero" { act.filter = &NonZeroFilter{} + } else if s.Type == "conditional" { + act.filter = &ConditionalFilter{} } else { return nil, fmt.Errorf("unsupported filter: '%s'", s.Type) } @@ -72,8 +76,9 @@ func (a *Activity) Eval(ctx activity.Context) (done bool, err error) { proceedOnlyOnEmit := a.proceedOnlyOnEmit in := ctx.GetInput(ivValue) + condition := ctx.GetInput(ivCondition).(bool) - filteredOut := filter.FilterOut(in) + filteredOut := filter.FilterOut(in, condition) done = !(proceedOnlyOnEmit && filteredOut) @@ -90,5 +95,5 @@ func (a *Activity) Eval(ctx activity.Context) (done bool, err error) { } type Filter interface { - FilterOut(val interface{}) bool + FilterOut(val interface{}, condition bool) bool } diff --git a/activity/filter/activity_test.go b/activity/filter/activity_test.go index 4ce0555..a6cd34e 100755 --- a/activity/filter/activity_test.go +++ b/activity/filter/activity_test.go @@ -49,6 +49,42 @@ func TestEval(t *testing.T) { assert.Equal(t, 0, value) } +func TestEvalWithCondition(t *testing.T) { + + settings := &Settings{Type: "conditional", ProceedOnlyOnEmit: true} + //mf := mapper.NewFactory(resolve.GetBasicResolver()) + iCtx := test.NewActivityInitContext(settings, nil) + + act, err := New(iCtx) + assert.Nil(t, err) + + tc := test.NewActivityContext(act.Metadata()) + + tc.SetInput(ivValue, 1) + tc.SetInput(ivCondition, true) + done, err := act.Eval(tc) + assert.Nil(t, err) + assert.False(t, done) + + filtered := tc.GetOutput(ovFiltered).(bool) + value := tc.GetOutput(ovValue) + + assert.True(t, filtered) + assert.Equal(t, 1, value) + + tc.SetInput(ivValue, 1) + tc.SetInput(ivCondition, false) + done, err = act.Eval(tc) + assert.Nil(t, err) + assert.True(t, done) + + filtered = tc.GetOutput(ovFiltered).(bool) + value = tc.GetOutput(ovValue) + + assert.False(t, filtered) + assert.Equal(t, 1, value) +} + func TestPOOEFalse(t *testing.T) { settings := &Settings{Type: "non-zero", ProceedOnlyOnEmit: false} diff --git a/activity/filter/descriptor.json b/activity/filter/descriptor.json index 4141010..0263b1b 100755 --- a/activity/filter/descriptor.json +++ b/activity/filter/descriptor.json @@ -10,7 +10,7 @@ "name": "type", "type": "string", "required": true, - "allowed" : ["non-zero"] + "allowed" : ["non-zero", "conditional"] }, { "name": "proceedOnlyOnEmit", @@ -21,6 +21,10 @@ { "name": "value", "type": "any" + }, + { + "name": "condition", + "type": "boolean" } ], "output": [ diff --git a/activity/filter/filters.go b/activity/filter/filters.go index e35b8f5..c74e569 100644 --- a/activity/filter/filters.go +++ b/activity/filter/filters.go @@ -3,7 +3,10 @@ package filter type NonZeroFilter struct { } -func (*NonZeroFilter) FilterOut(val interface{}) bool { +type ConditionalFilter struct { +} + +func (*NonZeroFilter) FilterOut(val interface{}, condition bool) bool { return !IsNonZero(val) } @@ -33,3 +36,7 @@ func IsNonZero(val interface{}) bool { //todo handle unsupported type return true } + +func (*ConditionalFilter) FilterOut(val interface{}, condition bool) bool { + return condition +} From d0aff2aee50689f6d517415bb9183875300828ea Mon Sep 17 00:00:00 2001 From: Vishal Pawar Date: Fri, 26 Jun 2020 18:29:34 +0530 Subject: [PATCH 2/3] Doc update --- activity/filter/README.md | 9 +++++++-- activity/filter/go.mod | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/activity/filter/README.md b/activity/filter/README.md index 3c0b4ea..2df9d6a 100755 --- a/activity/filter/README.md +++ b/activity/filter/README.md @@ -21,7 +21,7 @@ flogo install github.com/project-flogo/stream/activity/filter "name": "type", "type": "string", "required": true, - "allowed" : ["non-zero"] + "allowed" : ["non-zero","conditional"] }, { "name": "proceedOnlyOnEmit", @@ -32,6 +32,10 @@ flogo install github.com/project-flogo/stream/activity/filter { "name": "value", "type": "any" + }, + { + "name": "condition", + "type": "boolean" } ], "output": [ @@ -51,7 +55,7 @@ flogo install github.com/project-flogo/stream/activity/filter #### Settings: | Setting | Required | Description | |:------------|:---------|:------------| -| type | true | The type of filter to apply (ex. non-zero) +| type | true | The type of filter to apply (ex. non-zero, conditional) | proceedOnlyOnEmit | false | Indicates that the next activity should proceed, true by default _note_ : if using this activity in a flow, proceedOnlyOnEmit should be set to false @@ -59,6 +63,7 @@ _note_ : if using this activity in a flow, proceedOnlyOnEmit should be set to fa | Name | Description | |:------------|:---------| | value | The input value +| condition| The filter condition when filter type is 'conditional' #### Output: | Name | Description | diff --git a/activity/filter/go.mod b/activity/filter/go.mod index 46ff5a7..2dcaffd 100644 --- a/activity/filter/go.mod +++ b/activity/filter/go.mod @@ -1,4 +1,4 @@ -module github.com/project-flogo/stream/activity/filter +module github.com/vpawar-ai/stream/activity/filter require ( github.com/project-flogo/core v0.10.1 From 03fd72e2410221af680df2138c8be98d01927159 Mon Sep 17 00:00:00 2001 From: Vishal Pawar Date: Fri, 26 Jun 2020 18:58:28 +0530 Subject: [PATCH 3/3] Reverting go.mod --- activity/filter/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activity/filter/go.mod b/activity/filter/go.mod index 2dcaffd..46ff5a7 100644 --- a/activity/filter/go.mod +++ b/activity/filter/go.mod @@ -1,4 +1,4 @@ -module github.com/vpawar-ai/stream/activity/filter +module github.com/project-flogo/stream/activity/filter require ( github.com/project-flogo/core v0.10.1