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