get.porter.sh/porter@v1.3.0/pkg/runtime/actionInput.go (about)

     1  package runtime
     2  
     3  import (
     4  	"get.porter.sh/porter/pkg/manifest"
     5  	"get.porter.sh/porter/pkg/yaml"
     6  )
     7  
     8  type ActionInput struct {
     9  	action string
    10  	Steps  []*manifest.Step `yaml:"steps"`
    11  }
    12  
    13  // MarshalYAML marshals the step nested under the action
    14  // install:
    15  // - helm3:
    16  //   ...
    17  // Solution from https://stackoverflow.com/a/42547226
    18  func (a *ActionInput) MarshalYAML() (interface{}, error) {
    19  	// encode the original
    20  	b, err := yaml.Marshal(a.Steps)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	// decode it back to get a map
    26  	var tmp interface{}
    27  	err = yaml.Unmarshal(b, &tmp)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	stepMap := tmp.([]interface{})
    32  	actionMap := map[string]interface{}{
    33  		string(a.action): stepMap,
    34  	}
    35  	return actionMap, nil
    36  }