diff --git a/README.md b/README.md index ffd4ca96..17880a59 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Core collection of Flogo contributions. This repository consists of activities, ### Activities * [actreply](activity/actreply): Action Reply * [actreturn](activity/actreturn): Action Return +* [appdata](activity/actreturn): App Shared Data * [channel](activity/channel): Send internal engine messages * [counter](activity/counter): Simple Counter * [error](activity/error): Throw error diff --git a/activity/actreply/README.md b/activity/actreply/README.md index 2186653c..5a2235ac 100755 --- a/activity/actreply/README.md +++ b/activity/actreply/README.md @@ -8,9 +8,6 @@ This activity allows you to reply to a trigger invocation and map output values. ## Installation -### Flogo Web -This activity comes out of the box with the Flogo Web UI - ### Flogo CLI ```bash flogo install github.com/project-flogo/contrib/activity/actreply diff --git a/activity/actreturn/README.md b/activity/actreturn/README.md index feee721c..a6187b84 100755 --- a/activity/actreturn/README.md +++ b/activity/actreturn/README.md @@ -8,9 +8,6 @@ This activity allows you to reply to a trigger invocation and map output values. ## Installation -### Flogo Web -This activity comes out of the box with the Flogo Web UI - ### Flogo CLI ```bash flogo install github.com/project-flogo/contrib/activity/actreturn diff --git a/activity/appdata/README.md b/activity/appdata/README.md new file mode 100755 index 00000000..cc945ab3 --- /dev/null +++ b/activity/appdata/README.md @@ -0,0 +1,69 @@ +# AppData +This activity allows you to set and get global App attributes\. + +## Installation + +### Flogo CLI +```bash +flogo install github.com/project-flogo/contrib/activity/appdata +``` + +## Configuration + +### Settings: +| Name | Type | Description +|:--- | :--- | :--- +| name | string | The name of the shared attribute - **REQUIRED** +| op | string | The operation (get or set), 'get' is the default +| type | string | The data type of the shared value, default is 'any' + +### Input: +| Name | Type | Description +|:--- | :--- | :--- +| value | object | The value of the shared attribute + + +### Output: +| Name | Type | Description +|:--- | :--- | :--- +| value | object | The value of the shared attribute + + +## Examples + +### Get +Get the value of the 'myAttr' attribute: + +```json +{ + "id": "get_app_attr", + "name": "Get App Attr", + "activity": { + "ref": "github.com/project-flogo/contrib/activity/appdata", + "settings": { + "attribute": "myAttr", + "operation": "get" + } + } +} +``` + +### Set +Update the value of the 'myAttr' attribute to _bar_: + +```json +{ + "id": "set_app_attr", + "name": "Set App Attr", + "activity": { + "ref": "github.com/project-flogo/contrib/activity/appdata", + "settings": { + "attribute": "myAttr", + "operation": "set" + }, + "input": { + "value": "bar" + } + } +} +``` diff --git a/activity/appdata/activity.go b/activity/appdata/activity.go new file mode 100755 index 00000000..00b93670 --- /dev/null +++ b/activity/appdata/activity.go @@ -0,0 +1,109 @@ +package appdata + +import ( + "github.com/project-flogo/core/activity" + "github.com/project-flogo/core/app" + "github.com/project-flogo/core/data" + "github.com/project-flogo/core/data/coerce" + "github.com/project-flogo/core/data/metadata" +) + +const ( + opGet int = 0 + opSet int = 1 + + ivValue = "value" + ovValue = "value" +) + +type Settings struct { + Name string `md:"name,required"` // The name of the shared attribute + Op string `md:"op,allowed(get,set)"` // The operation (get or set), 'get' is the default + Type string `md:"type"` // The data type of the shared value, default is 'any' +} + +type Input struct { + Value interface{} `md:"value"` // The value of the shared attribute +} + +type Output struct { + Value interface{} `md:"value"` // The value of the shared attribute +} + +func init() { + _ = activity.Register(&Activity{}, New) +} + +var activityMd = activity.ToMetadata(&Settings{}, &Output{}) + +// Activity is a Counter Activity implementation +type Activity struct { + op int + dt data.Type + attrName string +} + +func New(ctx activity.InitContext) (activity.Activity, error) { + s := &Settings{} + err := metadata.MapToStruct(ctx.Settings(), s, true) + if err != nil { + return nil, err + } + + act := &Activity{attrName: s.Name} + + if s.Op == "set" { + act.op = opSet + } + + if s.Type != "" { + t, err := data.ToTypeEnum(s.Type) + if err != nil { + return nil, err + } + act.dt = t + } + + return act, nil +} + +// Metadata implements activity.Activity.Metadata +func (a *Activity) Metadata() *activity.Metadata { + return activityMd +} + +// Eval implements activity.Activity.Eval +func (a *Activity) Eval(ctx activity.Context) (done bool, err error) { + + switch a.op { + case opGet: + + val, exists := app.GetValue(a.attrName) + if exists && a.dt > 1 { + val, err = coerce.ToType(val, a.dt) + if err != nil { + return false, err + } + } + err = ctx.SetOutput(ovValue, val) + if err != nil { + return false, err + } + case opSet: + val := ctx.GetInput(ivValue) + + if a.dt > 1 { + val, err = coerce.ToType(val, a.dt) + if err != nil { + return false, err + } + } + + err = app.SetValue(a.attrName, val) + if err != nil { + return false, err + } + } + + return true, nil +} diff --git a/activity/appdata/activity_test.go b/activity/appdata/activity_test.go new file mode 100755 index 00000000..56861f35 --- /dev/null +++ b/activity/appdata/activity_test.go @@ -0,0 +1,72 @@ +package appdata + +import ( + "github.com/project-flogo/core/activity" + "github.com/project-flogo/core/app" + "github.com/project-flogo/core/support/test" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRegister(t *testing.T) { + + ref := activity.GetRef(&Activity{}) + act := activity.Get(ref) + + assert.NotNil(t, act) +} + +func TestSet(t *testing.T) { + + defer func() { + if r := recover(); r != nil { + t.Failed() + t.Errorf("panic during execution: %v", r) + } + }() + + settings := &Settings{Name: "test", Op: "set"} + iCtx := test.NewActivityInitContext(settings, nil) + + act, err := New(iCtx) + assert.Nil(t, err) + + tc := test.NewActivityContext(act.Metadata()) + tc.SetInput("value", "foo") + + _, err = act.Eval(tc) + assert.Nil(t, err) + + appValue, _ := app.GetValue("test") + assert.Equal(t, "foo", appValue) + +} + +func TestGet(t *testing.T) { + + defer func() { + if r := recover(); r != nil { + t.Failed() + t.Errorf("panic during execution: %v", r) + } + }() + + err := app.SetValue("test", "bar") + assert.Nil(t, err) + + settings := &Settings{Name: "test", Op: "get"} + iCtx := test.NewActivityInitContext(settings, nil) + + act, err := New(iCtx) + assert.Nil(t, err) + + tc := test.NewActivityContext(act.Metadata()) + tc.SetInput("value", "bar") + + _, err = act.Eval(tc) + assert.Nil(t, err) + + appValue, _ := app.GetValue("test") + assert.Equal(t, "bar", appValue) +} diff --git a/activity/appdata/descriptor.json b/activity/appdata/descriptor.json new file mode 100755 index 00000000..ea04812a --- /dev/null +++ b/activity/appdata/descriptor.json @@ -0,0 +1,42 @@ +{ + "name": "flogo-appdata", + "type": "flogo:activity", + "version": "0.0.1", + "title": "Use global App attribute", + "description": "Use global App attribute", + "homepage": "https://github.com/project-flogo/contrib/tree/master/activity/appdata", + "settings":[ + { + "name": "name", + "type": "string", + "required": true, + "description": "The name of the shared attribute" + }, + { + "name": "op", + "type": "string", + "allowed" : ["get", "set"], + "description": "The operation (get or set), 'get' is the default" + }, + { + "name": "type", + "type": "string", + "allowed" : [ "any","string", "int", "float64", "boolean", "object", "array", "params"], + "description": "The data type of the shared value, default is 'any'" + } + ], + "input": [ + { + "name": "value", + "type": "object", + "description": "The value of the shared attribute" + } + ], + "output": [ + { + "name": "value", + "type": "object", + "description": "The value of the shared attribute" + } + ] +} \ No newline at end of file diff --git a/activity/appdata/go.mod b/activity/appdata/go.mod new file mode 100644 index 00000000..9e383d81 --- /dev/null +++ b/activity/appdata/go.mod @@ -0,0 +1,6 @@ +module github.com/project-flogo/contrib/activity/appdata + +require ( + github.com/project-flogo/core v0.9.0-beta.3 + github.com/stretchr/testify v1.3.0 +) diff --git a/activity/appdata/go.sum b/activity/appdata/go.sum new file mode 100644 index 00000000..fb696537 --- /dev/null +++ b/activity/appdata/go.sum @@ -0,0 +1,19 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/project-flogo/core v0.9.0-beta.3 h1:RXBJBMLkMnEkI9MC/8xhGtLkk4XnMmug5Cvx4Of7yJo= +github.com/project-flogo/core v0.9.0-beta.3/go.mod h1:dzmBbQfNNC0g0KClKYQxxGJLe53MHafg75Vhmw2TW8U= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs= +go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o= +go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= diff --git a/activity/channel/activity.go b/activity/channel/activity.go index 968f4e20..b93503e8 100644 --- a/activity/channel/activity.go +++ b/activity/channel/activity.go @@ -28,7 +28,10 @@ func (a *Activity) Metadata() *activity.Metadata { func (a *Activity) Eval(ctx activity.Context) (done bool, err error) { input := &Input{} - ctx.GetInputObject(input) + err = ctx.GetInputObject(input) + if err != nil { + return false, err + } if len(input.Channel) == 0 { return false, fmt.Errorf("channel name must be specified") @@ -42,7 +45,7 @@ func (a *Activity) Eval(ctx activity.Context) (done bool, err error) { blocking := true - //should we allow + //todo should we allow publish new wait? if blocking { ch.Publish(input.Data) } else { diff --git a/activity/counter/README.md b/activity/counter/README.md index 2bccd95e..c891a699 100755 --- a/activity/counter/README.md +++ b/activity/counter/README.md @@ -8,9 +8,6 @@ This activity allows you to use a global counter. ## Installation -### Flogo Web -This activity comes out of the box with the Flogo Web UI - ### Flogo CLI ```bash flogo install github.com/project-flogo/contrib/activity/counter diff --git a/activity/counter/activity.go b/activity/counter/activity.go index 6de4b9d9..76c37fed 100755 --- a/activity/counter/activity.go +++ b/activity/counter/activity.go @@ -25,7 +25,7 @@ type Output struct { } func init() { - activity.Register(&Activity{}, New) + _ = activity.Register(&Activity{}, New) } var activityMd = activity.ToMetadata(&Settings{}, &Output{}) diff --git a/activity/error/README.md b/activity/error/README.md index b13c930d..cfd1430b 100644 --- a/activity/error/README.md +++ b/activity/error/README.md @@ -9,9 +9,6 @@ This activity allows you to cause an explicit error in the flow (throw an error) ## Installation -### Flogo Web -This activity comes out of the box with the Flogo Web UI - ### Flogo CLI ```bash flogo install github.com/project-flogo/contrib/activity/error diff --git a/activity/jsexec/metadata.go b/activity/jsexec/metadata.go index 76b596d7..06fc643d 100755 --- a/activity/jsexec/metadata.go +++ b/activity/jsexec/metadata.go @@ -15,19 +15,19 @@ type Input struct { } // FromMap converts the values from a map into the struct Input -func (r *Input) FromMap(values map[string]interface{}) error { +func (i *Input) FromMap(values map[string]interface{}) error { parameters, err := coerce.ToObject(values["parameters"]) if err != nil { return err } - r.Parameters = parameters + i.Parameters = parameters return nil } // ToMap converts the struct Input into a map -func (r *Input) ToMap() map[string]interface{} { +func (i *Input) ToMap() map[string]interface{} { return map[string]interface{}{ - "parameters": r.Parameters, + "parameters": i.Parameters, } } diff --git a/activity/log/README.md b/activity/log/README.md index b90a37cf..5368cb29 100644 --- a/activity/log/README.md +++ b/activity/log/README.md @@ -8,9 +8,6 @@ This activity allows you to write log messages. ## Installation -### Flogo Web -This activity comes out of the box with the Flogo Web UI - ### Flogo CLI ```bash flogo install github.com/project-flogo/contrib/activity/log diff --git a/activity/mapper/README.md b/activity/mapper/README.md index 01dc682b..9ff2f848 100755 --- a/activity/mapper/README.md +++ b/activity/mapper/README.md @@ -8,9 +8,6 @@ This activity allows you to map values to the working attribute set of an action ## Installation -### Flogo Web -This activity comes out of the box with the Flogo Web UI - ### Flogo CLI ```bash flogo install github.com/project-flogo/contrib/activity/mapper diff --git a/activity/noop/README.md b/activity/noop/README.md index 734146f9..8bd3b11d 100644 --- a/activity/noop/README.md +++ b/activity/noop/README.md @@ -8,9 +8,6 @@ This activity is a simple No-Op that can be used for testing. ## Installation -### Flogo Web -This activity comes out of the box with the Flogo Web UI - ### Flogo CLI ```bash flogo install github.com/project-flogo/contrib/activity/noop diff --git a/activity/rest/README.md b/activity/rest/README.md index b9d83cec..1f1f72dc 100644 --- a/activity/rest/README.md +++ b/activity/rest/README.md @@ -8,9 +8,6 @@ This activity allows you to invoke a REST service. ## Installation -### Flogo Web -This activity comes out of the box with the Flogo Web UI - ### Flogo CLI ```bash flogo install github.com/project-flogo/contrib/activity/rest diff --git a/activity/rest/metadata.go b/activity/rest/metadata.go index 031fe924..1c39051a 100644 --- a/activity/rest/metadata.go +++ b/activity/rest/metadata.go @@ -24,55 +24,55 @@ type Input struct { Content interface{} `md:"content"` // The message content to send. This is only used in POST, PUT, and PATCH } -func (o *Input) ToMap() map[string]interface{} { +func (i *Input) ToMap() map[string]interface{} { return map[string]interface{}{ - "pathParams": o.PathParams, - "queryParams": o.QueryParams, - "headers": o.Headers, - "content": o.Content, + "pathParams": i.PathParams, + "queryParams": i.QueryParams, + "headers": i.Headers, + "content": i.Content, } } -func (o *Input) FromMap(values map[string]interface{}) error { +func (i *Input) FromMap(values map[string]interface{}) error { var err error - o.PathParams, err = coerce.ToParams(values["pathParams"]) + i.PathParams, err = coerce.ToParams(values["pathParams"]) if err != nil { return err } - o.QueryParams, err = coerce.ToParams(values["queryParams"]) + i.QueryParams, err = coerce.ToParams(values["queryParams"]) if err != nil { return err } - o.Headers, err = coerce.ToParams(values["headers"]) + i.Headers, err = coerce.ToParams(values["headers"]) if err != nil { return err } - o.Content = values["content"] + i.Content = values["content"] return nil } type Output struct { - Status int `md:"status"` // The HTTP status code - Data interface{} `md:"result"` // The HTTP response data + Status int `md:"status"` // The HTTP status code + Data interface{} `md:"data"` // The HTTP response data } -func (r *Output) ToMap() map[string]interface{} { +func (o *Output) ToMap() map[string]interface{} { return map[string]interface{}{ - "status": r.Status, - "data": r.Data, + "status": o.Status, + "data": o.Data, } } -func (r *Output) FromMap(values map[string]interface{}) error { +func (o *Output) FromMap(values map[string]interface{}) error { var err error - r.Status, err = coerce.ToInt(values["status"]) + o.Status, err = coerce.ToInt(values["status"]) if err != nil { return err } - r.Data, _ = values["data"] + o.Data, _ = values["data"] return nil }