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

     1  package action
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/TIBCOSoftware/flogo-lib/core/data"
     6  )
     7  
     8  // Metadata is the metadata for the Activity
     9  type Metadata struct {
    10  	ID       string `json:"ref"`
    11  	Version  string
    12  	Settings map[string]*data.Attribute
    13  	Input    map[string]*data.Attribute
    14  	Output   map[string]*data.Attribute
    15  
    16  	Async    bool `json:"async"`
    17  	Passthru bool `json:"passthru"`
    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  		Async    bool              `json:"async"`
    43  		Passthru bool              `json:"passthru"`
    44  	}{}
    45  
    46  	if err := json.Unmarshal(b, ser); err != nil {
    47  		return err
    48  	}
    49  
    50  	if len(ser.Ref) > 0 {
    51  		md.ID = ser.Ref
    52  	} else {
    53  		// Added for backwards compatibility
    54  		// TODO remove and add a proper error once the BC is removed
    55  		md.ID = ser.Name
    56  	}
    57  
    58  	md.Version = ser.Version
    59  
    60  	md.Async = ser.Async
    61  	md.Passthru = ser.Passthru
    62  
    63  	md.Settings = make(map[string]*data.Attribute, len(ser.Settings))
    64  	md.Input = make(map[string]*data.Attribute, len(ser.Input))
    65  	md.Output = make(map[string]*data.Attribute, len(ser.Output))
    66  
    67  	for _, attr := range ser.Settings {
    68  		md.Settings[attr.Name()] = attr
    69  	}
    70  
    71  	if len(ser.Input) > 0 {
    72  		for _, attr := range ser.Input {
    73  			md.Input[attr.Name()] = attr
    74  		}
    75  	}
    76  
    77  	if len(ser.Output) > 0 {
    78  		for _, attr := range ser.Output {
    79  			md.Output[attr.Name()] = attr
    80  		}
    81  	}
    82  
    83  	return nil
    84  }