github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/context/action.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package context
     5  
     6  import (
     7  	"gopkg.in/juju/names.v2"
     8  )
     9  
    10  // ActionData contains the tag, parameters, and results of an Action.
    11  type ActionData struct {
    12  	Name           string
    13  	Tag            names.ActionTag
    14  	Params         map[string]interface{}
    15  	Failed         bool
    16  	ResultsMessage string
    17  	ResultsMap     map[string]interface{}
    18  }
    19  
    20  // NewActionData builds a suitable ActionData struct with no nil members.
    21  // this should only be called in the event that an Action hook is being requested.
    22  func NewActionData(name string, tag *names.ActionTag, params map[string]interface{}) *ActionData {
    23  	return &ActionData{
    24  		Name:       name,
    25  		Tag:        *tag,
    26  		Params:     params,
    27  		ResultsMap: map[string]interface{}{},
    28  	}
    29  }
    30  
    31  // addValueToMap adds the given value to the map on which the method is run.
    32  // This allows us to merge maps such as {foo: {bar: baz}} and {foo: {baz: faz}}
    33  // into {foo: {bar: baz, baz: faz}}.
    34  func addValueToMap(keys []string, value string, target map[string]interface{}) {
    35  	next := target
    36  
    37  	for i := range keys {
    38  		// if we are on last key set the value.
    39  		// shouldn't be a problem.  overwrites existing vals.
    40  		if i == len(keys)-1 {
    41  			next[keys[i]] = value
    42  			break
    43  		}
    44  
    45  		if iface, ok := next[keys[i]]; ok {
    46  			switch typed := iface.(type) {
    47  			case map[string]interface{}:
    48  				// If we already had a map inside, keep
    49  				// stepping through.
    50  				next = typed
    51  			default:
    52  				// If we didn't, then overwrite value
    53  				// with a map and iterate with that.
    54  				m := map[string]interface{}{}
    55  				next[keys[i]] = m
    56  				next = m
    57  			}
    58  			continue
    59  		}
    60  
    61  		// Otherwise, it wasn't present, so make it and step
    62  		// into.
    63  		m := map[string]interface{}{}
    64  		next[keys[i]] = m
    65  		next = m
    66  	}
    67  }