-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from fm-tibco/master
add appdata activity; additional cleanup
- Loading branch information
Showing
19 changed files
with
346 additions
and
49 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
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,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" | ||
} | ||
} | ||
} | ||
``` |
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,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 | ||
} |
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,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) | ||
} |
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,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" | ||
} | ||
] | ||
} |
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,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 | ||
) |
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,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= |
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
Oops, something went wrong.