]>
Commit | Line | Data |
---|---|---|
107c1cdb ND |
1 | package addrs |
2 | ||
3 | import ( | |
4 | "strings" | |
5 | ) | |
6 | ||
7 | // Module is an address for a module call within configuration. This is | |
8 | // the static counterpart of ModuleInstance, representing a traversal through | |
9 | // the static module call tree in configuration and does not take into account | |
10 | // the potentially-multiple instances of a module that might be created by | |
11 | // "count" and "for_each" arguments within those calls. | |
12 | // | |
13 | // This type should be used only in very specialized cases when working with | |
14 | // the static module call tree. Type ModuleInstance is appropriate in more cases. | |
15 | // | |
16 | // Although Module is a slice, it should be treated as immutable after creation. | |
17 | type Module []string | |
18 | ||
19 | // RootModule is the module address representing the root of the static module | |
20 | // call tree, which is also the zero value of Module. | |
21 | // | |
22 | // Note that this is not the root of the dynamic module tree, which is instead | |
23 | // represented by RootModuleInstance. | |
24 | var RootModule Module | |
25 | ||
26 | // IsRoot returns true if the receiver is the address of the root module, | |
27 | // or false otherwise. | |
28 | func (m Module) IsRoot() bool { | |
29 | return len(m) == 0 | |
30 | } | |
31 | ||
32 | func (m Module) String() string { | |
33 | if len(m) == 0 { | |
34 | return "" | |
35 | } | |
36 | return strings.Join([]string(m), ".") | |
37 | } | |
38 | ||
39 | // Child returns the address of a child call in the receiver, identified by the | |
40 | // given name. | |
41 | func (m Module) Child(name string) Module { | |
42 | ret := make(Module, 0, len(m)+1) | |
43 | ret = append(ret, m...) | |
44 | return append(ret, name) | |
45 | } | |
46 | ||
47 | // Parent returns the address of the parent module of the receiver, or the | |
48 | // receiver itself if there is no parent (if it's the root module address). | |
49 | func (m Module) Parent() Module { | |
50 | if len(m) == 0 { | |
51 | return m | |
52 | } | |
53 | return m[:len(m)-1] | |
54 | } | |
55 | ||
56 | // Call returns the module call address that corresponds to the given module | |
57 | // instance, along with the address of the module that contains it. | |
58 | // | |
59 | // There is no call for the root module, so this method will panic if called | |
60 | // on the root module address. | |
61 | // | |
62 | // In practice, this just turns the last element of the receiver into a | |
63 | // ModuleCall and then returns a slice of the receiever that excludes that | |
64 | // last part. This is just a convenience for situations where a call address | |
65 | // is required, such as when dealing with *Reference and Referencable values. | |
66 | func (m Module) Call() (Module, ModuleCall) { | |
67 | if len(m) == 0 { | |
68 | panic("cannot produce ModuleCall for root module") | |
69 | } | |
70 | ||
71 | caller, callName := m[:len(m)-1], m[len(m)-1] | |
72 | return caller, ModuleCall{ | |
73 | Name: callName, | |
74 | } | |
75 | } |