Skip to content

Commit

Permalink
Merge pull request #34 from fm-tibco/master
Browse files Browse the repository at this point in the history
add appdata activity; additional cleanup
  • Loading branch information
Frank Martinez authored Apr 3, 2019
2 parents f0817b0 + 6cbaf53 commit 7b523f1
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 49 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions activity/actreply/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions activity/actreturn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions activity/appdata/README.md
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"
}
}
}
```
109 changes: 109 additions & 0 deletions activity/appdata/activity.go
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
}
72 changes: 72 additions & 0 deletions activity/appdata/activity_test.go
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)
}
42 changes: 42 additions & 0 deletions activity/appdata/descriptor.json
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"
}
]
}
6 changes: 6 additions & 0 deletions activity/appdata/go.mod
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
)
19 changes: 19 additions & 0 deletions activity/appdata/go.sum
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=
7 changes: 5 additions & 2 deletions activity/channel/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions activity/counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion activity/counter/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Output struct {
}

func init() {
activity.Register(&Activity{}, New)
_ = activity.Register(&Activity{}, New)
}

var activityMd = activity.ToMetadata(&Settings{}, &Output{})
Expand Down
Loading

0 comments on commit 7b523f1

Please sign in to comment.