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

     1  package activity
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     7  )
     8  
     9  // Metadata is the metadata for the Activity
    10  type Metadata struct {
    11  	ID             string
    12  	Version        string
    13  	Settings       map[string]*data.Attribute
    14  	Input          map[string]*data.Attribute
    15  	Output         map[string]*data.Attribute
    16  	ProducesResult bool
    17  	DynamicIO      bool
    18  }
    19  
    20  // NewMetadata creates the metadata object from its json representation
    21  func NewMetadata(jsonMetadata string) *Metadata {
    22  	md := &Metadata{}
    23  	err := json.Unmarshal([]byte(jsonMetadata), md)
    24  	if err != nil {
    25  		panic("Unable to parse activity metadata: " + err.Error())
    26  	}
    27  
    28  	return md
    29  }
    30  
    31  // UnmarshalJSON overrides the default UnmarshalJSON for TaskEnv
    32  func (md *Metadata) UnmarshalJSON(b []byte) error {
    33  
    34  	ser := &struct {
    35  		Name    string `json:"name"`
    36  		Version string `json:"version"`
    37  		Ref     string `json:"ref"`
    38  
    39  		Settings  []*data.Attribute `json:"settings"`
    40  		Input     []*data.Attribute `json:"input"`
    41  		Output    []*data.Attribute `json:"output"`
    42  		Return    bool              `json:"return"`
    43  		Reply     bool              `json:"reply"`
    44  		DynamicIO bool              `json:"dynamicIO"`
    45  
    46  		//for backwards compatibility
    47  		Inputs  []*data.Attribute `json:"inputs"`
    48  		Outputs []*data.Attribute `json:"outputs"`
    49  	}{}
    50  
    51  	if err := json.Unmarshal(b, ser); err != nil {
    52  		return err
    53  	}
    54  
    55  	if len(ser.Ref) > 0 {
    56  		md.ID = ser.Ref
    57  	} else {
    58  		// Added for backwards compatibility
    59  		// TODO remove and add a proper error once the BC is removed
    60  		md.ID = ser.Name
    61  	}
    62  
    63  	md.Version = ser.Version
    64  
    65  	md.ProducesResult = ser.Reply || ser.Return
    66  	md.DynamicIO = ser.DynamicIO
    67  
    68  	md.Settings = make(map[string]*data.Attribute, len(ser.Settings))
    69  	md.Input = make(map[string]*data.Attribute, len(ser.Inputs))
    70  	md.Output = make(map[string]*data.Attribute, len(ser.Outputs))
    71  
    72  	for _, attr := range ser.Settings {
    73  		md.Settings[attr.Name()] = attr
    74  	}
    75  
    76  	if len(ser.Input) > 0 {
    77  		for _, attr := range ser.Input {
    78  			md.Input[attr.Name()] = attr
    79  		}
    80  	} else {
    81  		// for backwards compatibility
    82  		for _, attr := range ser.Inputs {
    83  			md.Input[attr.Name()] = attr
    84  		}
    85  	}
    86  
    87  	if len(ser.Output) > 0 {
    88  		for _, attr := range ser.Output {
    89  			md.Output[attr.Name()] = attr
    90  		}
    91  	} else {
    92  		// for backwards compatibility
    93  		for _, attr := range ser.Outputs {
    94  			md.Output[attr.Name()] = attr
    95  		}
    96  	}
    97  
    98  	return nil
    99  }