github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/trigger/config.go (about)

     1  package trigger
     2  
     3  import (
     4  	"github.com/TIBCOSoftware/flogo-lib/core/action"
     5  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     6  	"github.com/TIBCOSoftware/flogo-lib/util"
     7  )
     8  
     9  // Config is the configuration for a Trigger
    10  type Config struct {
    11  	Name     string                 `json:"name"`
    12  	Id       string                 `json:"id"`
    13  	Ref      string                 `json:"ref"`
    14  	Settings map[string]interface{} `json:"settings"`
    15  	Output   map[string]interface{} `json:"output"`
    16  	Handlers []*HandlerConfig       `json:"handlers"`
    17  
    18  	// Deprecated: Use Output
    19  	Outputs map[string]interface{} `json:"outputs"`
    20  }
    21  
    22  func (c *Config) FixUp(metadata *Metadata) {
    23  
    24  	//for backwards compatibility
    25  	if len(c.Output) == 0 {
    26  		c.Output = c.Outputs
    27  	}
    28  
    29  	for k, v := range c.Settings {
    30  		strValue, ok := v.(string)
    31  		if ok {
    32  			if strValue != "" && strValue[0] == '$' {
    33  				// Let resolver resolve value
    34  				val, err := data.GetBasicResolver().Resolve(strValue, nil)
    35  				if err != nil {
    36  					val = strValue
    37  				}
    38  				c.Settings[k] = val
    39  			}
    40  		}
    41  	}
    42  
    43  	// fix up top-level outputs
    44  	for name, value := range c.Output {
    45  
    46  		attr, ok := metadata.Output[name]
    47  
    48  		if ok {
    49  			newValue, err := data.CoerceToValue(value, attr.Type())
    50  
    51  			if err != nil {
    52  				//todo handle error
    53  			} else {
    54  				c.Output[name] = newValue
    55  			}
    56  		}
    57  	}
    58  
    59  	idGen, _ := util.NewGenerator()
    60  
    61  	// fix up handler outputs
    62  	for _, hc := range c.Handlers {
    63  
    64  		hc.parent = c
    65  
    66  		//for backwards compatibility
    67  		if hc.ActionId == "" {
    68  			hc.ActionId = idGen.NextAsString()
    69  		}
    70  
    71  		//for backwards compatibility
    72  		if len(hc.Output) == 0 {
    73  			hc.Output = hc.Outputs
    74  		}
    75  
    76  		for k, v := range hc.Settings {
    77  			strValue, ok := v.(string)
    78  			if ok {
    79  				if strValue != "" && strValue[0] == '$' {
    80  					// Let resolver resolve value
    81  					val, err := data.GetBasicResolver().Resolve(strValue, nil)
    82  					if err != nil {
    83  						val = strValue
    84  					}
    85  					hc.Settings[k] = val
    86  				}
    87  			}
    88  		}
    89  		// fix up outputs
    90  		for name, value := range hc.Output {
    91  
    92  			attr, ok := metadata.Output[name]
    93  
    94  			if ok {
    95  				newValue, err := data.CoerceToValue(value, attr.Type())
    96  
    97  				if err != nil {
    98  					//todo handle error
    99  				} else {
   100  					hc.Output[name] = newValue
   101  				}
   102  			}
   103  		}
   104  	}
   105  }
   106  
   107  func (c *Config) GetSetting(setting string) string {
   108  
   109  	strVal, err := data.CoerceToString(c.Settings[setting])
   110  
   111  	if err != nil {
   112  		return ""
   113  	}
   114  
   115  	return strVal
   116  }
   117  
   118  type HandlerConfig struct {
   119  	parent   *Config
   120  	Name     string                 `json:"name,omitempty"`
   121  	Settings map[string]interface{} `json:"settings"`
   122  	Output   map[string]interface{} `json:"output"`
   123  	Action   *ActionConfig
   124  
   125  	// Deprecated: Use Action (*action.Config)
   126  	ActionId string `json:"actionId"`
   127  	// Deprecated: Use Action (*action.Config)
   128  	ActionMappings *data.IOMappings `json:"actionMappings,omitempty"`
   129  	// Deprecated: Use Action (*action.Config)
   130  	ActionOutputMappings []*data.MappingDef `json:"actionOutputMappings,omitempty"`
   131  	// Deprecated: Use Action (*action.Config)
   132  	ActionInputMappings []*data.MappingDef `json:"actionInputMappings,omitempty"`
   133  	// Deprecated: Use Output
   134  	Outputs map[string]interface{} `json:"outputs"`
   135  }
   136  
   137  // ActionConfig is the configuration for the Action
   138  type ActionConfig struct {
   139  	*action.Config
   140  
   141  	Mappings *data.IOMappings `json:"mappings"`
   142  
   143  	Act action.Action
   144  }
   145  
   146  func (hc *HandlerConfig) GetSetting(setting string) string {
   147  
   148  	strVal, err := data.CoerceToString(hc.Settings[setting])
   149  
   150  	if err != nil {
   151  		return ""
   152  	}
   153  
   154  	return strVal
   155  }