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

     1  package trigger
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     7  )
     8  
     9  // Metadata is the metadata for a Trigger
    10  type Metadata struct {
    11  	ID       string
    12  	Version  string
    13  	Handler  *HandlerMetadata
    14  	Settings map[string]*data.Attribute
    15  	Output   map[string]*data.Attribute
    16  	Reply    map[string]*data.Attribute
    17  }
    18  
    19  // EndpointMetadata is the metadata for a Trigger Endpoint
    20  type HandlerMetadata struct {
    21  	Settings []*data.Attribute `json:"settings"`
    22  }
    23  
    24  // NewMetadata creates a Metadata object from the json representation
    25  //todo should return error instead of panic
    26  func NewMetadata(jsonMetadata string) *Metadata {
    27  	md := &Metadata{}
    28  	err := json.Unmarshal([]byte(jsonMetadata), md)
    29  	if err != nil {
    30  		panic("Unable to parse trigger metadata: " + err.Error())
    31  	}
    32  
    33  	return md
    34  }
    35  
    36  // UnmarshalJSON overrides the default UnmarshalJSON for Metadata
    37  func (md *Metadata) UnmarshalJSON(b []byte) error {
    38  
    39  	ser := &struct {
    40  		Name     string            `json:"name"`
    41  		Version  string            `json:"version"`
    42  		Ref      string            `json:"ref"`
    43  		Handler  *HandlerMetadata  `json:"handler"`
    44  		Settings []*data.Attribute `json:"settings"`
    45  		Output   []*data.Attribute `json:"output"`
    46  		Reply    []*data.Attribute `json:"reply"`
    47  
    48  		//for backwards compatibility
    49  		Endpoint *HandlerMetadata  `json:"endpoint"`
    50  		Outputs  []*data.Attribute `json:"outputs"`
    51  	}{}
    52  
    53  	if err := json.Unmarshal(b, ser); err != nil {
    54  		return err
    55  	}
    56  
    57  	if len(ser.Ref) > 0 {
    58  		md.ID = ser.Ref
    59  	} else {
    60  		// Added for backwards compatibility
    61  		// TODO remove and add a proper error once the BC is removed
    62  		md.ID = ser.Name
    63  	}
    64  
    65  	md.Version = ser.Version
    66  
    67  	if ser.Handler != nil {
    68  		md.Handler = ser.Handler
    69  	} else {
    70  		// Added for backwards compatibility
    71  		// TODO remove and add a proper error once the BC is removed
    72  
    73  		if ser.Endpoint != nil {
    74  			md.Handler = ser.Endpoint
    75  		}
    76  	}
    77  
    78  	md.Settings = make(map[string]*data.Attribute, len(ser.Settings))
    79  	md.Output = make(map[string]*data.Attribute, len(ser.Outputs))
    80  	md.Reply = make(map[string]*data.Attribute, len(ser.Reply))
    81  
    82  	for _, attr := range ser.Settings {
    83  		md.Settings[attr.Name()] = attr
    84  	}
    85  
    86  	if len(ser.Output) > 0 {
    87  		for _, attr := range ser.Output {
    88  			md.Output[attr.Name()] = attr
    89  		}
    90  	} else {
    91  		// for backwards compatibility
    92  		for _, attr := range ser.Outputs {
    93  			md.Output[attr.Name()] = attr
    94  		}
    95  	}
    96  
    97  	for _, attr := range ser.Reply {
    98  		md.Reply[attr.Name()] = attr
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  // OutputsToAttrs converts the supplied output data to attributes
   105  // todo remove coerce option, coercion now happens by default
   106  func (md *Metadata) OutputsToAttrs(outputData map[string]interface{}, coerce bool) ([]*data.Attribute, error) {
   107  
   108  	attrs := make([]*data.Attribute, 0, len(md.Output))
   109  
   110  	for k, a := range md.Output {
   111  		v, _ := outputData[k]
   112  
   113  		//if coerce {
   114  		//	var err error
   115  		//	v, err = data.CoerceToValue(v, a.Type)
   116  		//
   117  		//	if err != nil {
   118  		//		return nil, err
   119  		//	}
   120  		//}
   121  
   122  		var err error
   123  		attr, err := data.NewAttribute(a.Name(), a.Type(), v)
   124  		attrs = append(attrs, attr)
   125  
   126  		if err != nil {
   127  			return nil, err
   128  		}
   129  	}
   130  
   131  	return attrs, nil
   132  }