github.com/TIBCOSoftware/flogo-lib@v0.5.9/flogo/api.go (about)

     1  package flogo
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"reflect"
     7  
     8  	"github.com/TIBCOSoftware/flogo-lib/app/resource"
     9  	"github.com/TIBCOSoftware/flogo-lib/core/action"
    10  	"github.com/TIBCOSoftware/flogo-lib/core/data"
    11  	"github.com/TIBCOSoftware/flogo-lib/core/trigger"
    12  	"github.com/TIBCOSoftware/flogo-lib/engine"
    13  )
    14  
    15  // App is the structure that defines an application
    16  type App struct {
    17  	properties []*data.Attribute
    18  	triggers   []*Trigger
    19  	resources  []*resource.Config
    20  }
    21  
    22  // Trigger is the structure that defines a Trigger for the application
    23  type Trigger struct {
    24  	ref      string
    25  	settings map[string]interface{}
    26  	handlers []*Handler
    27  }
    28  
    29  // Handler is the structure that defines the handler for a Trigger
    30  type Handler struct {
    31  	settings map[string]interface{}
    32  	actions  []*Action
    33  }
    34  
    35  // HandlerFunc is the signature for a function to use as a handler for a Trigger
    36  type HandlerFunc func(ctx context.Context, inputs map[string]*data.Attribute) (map[string]*data.Attribute, error)
    37  
    38  // Action is the structure that defines the Action for a Handler
    39  type Action struct {
    40  	ref            string
    41  	act            action.Action
    42  	settings       map[string]interface{}
    43  	inputMappings  []string
    44  	outputMappings []string
    45  }
    46  
    47  // NewApp creates a new Flogo application
    48  func NewApp() *App {
    49  	return &App{}
    50  }
    51  
    52  // NewTrigger adds a new trigger to the application
    53  func (a *App) NewTrigger(trg trigger.Trigger, settings map[string]interface{}) *Trigger {
    54  
    55  	value := reflect.ValueOf(trg)
    56  	value = value.Elem()
    57  	ref := value.Type().PkgPath()
    58  
    59  	newTrg := &Trigger{ref: ref, settings: settings}
    60  	a.triggers = append(a.triggers, newTrg)
    61  
    62  	return newTrg
    63  }
    64  
    65  // AddProperty adds a shared property to the application
    66  func (a *App) AddProperty(name string, dataType data.Type, value interface{}) error {
    67  	property, err := data.NewAttribute(name, dataType, value)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	a.properties = append(a.properties, property)
    72  	return nil
    73  }
    74  
    75  // AddResource adds a Flogo resource to the application
    76  func (a *App) AddResource(id string, data json.RawMessage) {
    77  
    78  	res := &resource.Config{ID: id, Data: data}
    79  	a.resources = append(a.resources, res)
    80  }
    81  
    82  // Properties gets the shared properties of the application
    83  func (a *App) Properties() []*data.Attribute {
    84  
    85  	return a.properties
    86  }
    87  
    88  // Triggers gets the Triggers of the application
    89  func (a *App) Triggers() []*Trigger {
    90  
    91  	return a.triggers
    92  }
    93  
    94  // Settings gets the Trigger's settings
    95  func (t *Trigger) Settings() map[string]interface{} {
    96  
    97  	return t.settings
    98  }
    99  
   100  // NewHandler adds a new Handler to the Trigger
   101  func (t *Trigger) NewHandler(settings map[string]interface{}) *Handler {
   102  
   103  	newHandler := &Handler{settings: settings}
   104  	t.handlers = append(t.handlers, newHandler)
   105  
   106  	return newHandler
   107  }
   108  
   109  // NewFuncHandler adds a new Function Handler to the Trigger
   110  func (t *Trigger) NewFuncHandler(settings map[string]interface{}, handlerFunc HandlerFunc) *Handler {
   111  
   112  	newHandler := &Handler{settings: settings}
   113  	newAct := &Action{act: NewProxyAction(handlerFunc)}
   114  	newHandler.actions = append(newHandler.actions, newAct)
   115  
   116  	t.handlers = append(t.handlers, newHandler)
   117  
   118  	return newHandler
   119  }
   120  
   121  // Handlers gets the Trigger's Handlers
   122  func (t *Trigger) Handlers() []*Handler {
   123  	return t.handlers
   124  }
   125  
   126  // Settings gets the Handler's settings
   127  func (h *Handler) Settings() map[string]interface{} {
   128  	return h.settings
   129  }
   130  
   131  // NewAction creates a new Action for the Handler
   132  // note: Currently only the first Action is executed for the Handler
   133  func (h *Handler) NewAction(act action.Action, settings map[string]interface{}) *Action {
   134  
   135  	value := reflect.ValueOf(act)
   136  	value = value.Elem()
   137  	ref := value.Type().PkgPath()
   138  
   139  	newAct := &Action{ref: ref, settings: settings}
   140  	h.actions = append(h.actions, newAct)
   141  
   142  	return newAct
   143  }
   144  
   145  // Actions gets the Actions of the Handler
   146  func (h *Handler) Actions() []*Action {
   147  	return h.actions
   148  }
   149  
   150  // Settings gets the settings of the Action
   151  func (a *Action) Settings() map[string]interface{} {
   152  	return a.settings
   153  }
   154  
   155  // SetInputMappings sets the input mappings for the Action, which maps
   156  // the outputs of the Trigger to the inputs of the Action
   157  func (a *Action) SetInputMappings(mappings ...string) {
   158  	a.inputMappings = mappings
   159  }
   160  
   161  // SetOutputMappings sets the output mappings for the Action, which maps
   162  // the outputs of the Action to the return of the Trigger
   163  func (a *Action) SetOutputMappings(mappings ...string) {
   164  	a.outputMappings = mappings
   165  }
   166  
   167  // InputMappings gets the Action's input mappings
   168  func (a *Action) InputMappings() []string {
   169  	return a.inputMappings
   170  }
   171  
   172  // OutputMappings gets the Action's output mappings
   173  func (a *Action) OutputMappings() []string {
   174  	return a.outputMappings
   175  }
   176  
   177  // NewEngine creates a new flogo Engine from the specified App
   178  func NewEngine(a *App) (engine.Engine, error) {
   179  	appConfig := toAppConfig(a)
   180  	return engine.New(appConfig)
   181  }