-
Notifications
You must be signed in to change notification settings - Fork 10
/
module_call.go
192 lines (158 loc) · 5 KB
/
module_call.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package addrs
import (
"fmt"
)
// ModuleCall is the address of a call from the current module to a child
// module.
type ModuleCall struct {
referenceable
Name string
}
func (c ModuleCall) String() string {
return "module." + c.Name
}
func (c ModuleCall) UniqueKey() UniqueKey {
return c // A ModuleCall is its own UniqueKey
}
func (c ModuleCall) uniqueKeySigil() {}
// Instance returns the address of an instance of the receiver identified by
// the given key.
func (c ModuleCall) Instance(key InstanceKey) ModuleCallInstance {
return ModuleCallInstance{
Call: c,
Key: key,
}
}
func (c ModuleCall) Absolute(moduleAddr ModuleInstance) AbsModuleCall {
return AbsModuleCall{
Module: moduleAddr,
Call: c,
}
}
func (c ModuleCall) Equal(other ModuleCall) bool {
return c.Name == other.Name
}
// AbsModuleCall is the address of a "module" block relative to the root
// of the configuration.
//
// This is similar to ModuleInstance alone, but specifically represents
// the module block itself rather than any one of the instances that
// module block declares.
type AbsModuleCall struct {
Module ModuleInstance
Call ModuleCall
}
func (c AbsModuleCall) absMoveableSigil() {
// AbsModuleCall is "moveable".
}
func (c AbsModuleCall) String() string {
if len(c.Module) == 0 {
return "module." + c.Call.Name
}
return fmt.Sprintf("%s.module.%s", c.Module, c.Call.Name)
}
func (c AbsModuleCall) Instance(key InstanceKey) ModuleInstance {
ret := make(ModuleInstance, len(c.Module), len(c.Module)+1)
copy(ret, c.Module)
ret = append(ret, ModuleInstanceStep{
Name: c.Call.Name,
InstanceKey: key,
})
return ret
}
func (c AbsModuleCall) Equal(other AbsModuleCall) bool {
return c.Module.Equal(other.Module) && c.Call.Equal(other.Call)
}
type absModuleCallInstanceKey string
func (c AbsModuleCall) UniqueKey() UniqueKey {
return absModuleCallInstanceKey(c.String())
}
func (mk absModuleCallInstanceKey) uniqueKeySigil() {}
// ModuleCallInstance is the address of one instance of a module created from
// a module call, which might create multiple instances using "count" or
// "for_each" arguments.
//
// There is no "Abs" version of ModuleCallInstance because an absolute module
// path is represented by ModuleInstance.
type ModuleCallInstance struct {
referenceable
Call ModuleCall
Key InstanceKey
}
func (c ModuleCallInstance) String() string {
if c.Key == NoKey {
return c.Call.String()
}
return fmt.Sprintf("module.%s%s", c.Call.Name, c.Key)
}
func (c ModuleCallInstance) UniqueKey() UniqueKey {
return c // A ModuleCallInstance is its own UniqueKey
}
func (c ModuleCallInstance) uniqueKeySigil() {}
func (c ModuleCallInstance) Absolute(moduleAddr ModuleInstance) ModuleInstance {
ret := make(ModuleInstance, len(moduleAddr), len(moduleAddr)+1)
copy(ret, moduleAddr)
ret = append(ret, ModuleInstanceStep{
Name: c.Call.Name,
InstanceKey: c.Key,
})
return ret
}
// ModuleInstance returns the address of the module instance that corresponds
// to the receiving call instance when resolved in the given calling module.
// In other words, it returns the child module instance that the receving
// call instance creates.
func (c ModuleCallInstance) ModuleInstance(caller ModuleInstance) ModuleInstance {
return caller.Child(c.Call.Name, c.Key)
}
// Output returns the absolute address of an output of the receiver identified by its
// name.
func (c ModuleCallInstance) Output(name string) ModuleCallInstanceOutput {
return ModuleCallInstanceOutput{
Call: c,
Name: name,
}
}
// ModuleCallOutput is the address of a named output and its associated
// ModuleCall, which may expand into multiple module instances
type ModuleCallOutput struct {
referenceable
Call ModuleCall
Name string
}
func (m ModuleCallOutput) String() string {
return fmt.Sprintf("%s.%s", m.Call.String(), m.Name)
}
func (m ModuleCallOutput) UniqueKey() UniqueKey {
return m // A ModuleCallOutput is its own UniqueKey
}
func (m ModuleCallOutput) uniqueKeySigil() {}
// ModuleCallInstanceOutput is the address of a particular named output produced by
// an instance of a module call.
type ModuleCallInstanceOutput struct {
referenceable
Call ModuleCallInstance
Name string
}
// ModuleCallOutput returns the referenceable ModuleCallOutput for this
// particular instance.
func (co ModuleCallInstanceOutput) ModuleCallOutput() ModuleCallOutput {
return ModuleCallOutput{
Call: co.Call.Call,
Name: co.Name,
}
}
func (co ModuleCallInstanceOutput) String() string {
return fmt.Sprintf("%s.%s", co.Call.String(), co.Name)
}
func (co ModuleCallInstanceOutput) UniqueKey() UniqueKey {
return co // A ModuleCallInstanceOutput is its own UniqueKey
}
func (co ModuleCallInstanceOutput) uniqueKeySigil() {}
// AbsOutputValue returns the absolute output value address that corresponds
// to the receving module call output address, once resolved in the given
// calling module.
func (co ModuleCallInstanceOutput) AbsOutputValue(caller ModuleInstance) AbsOutputValue {
moduleAddr := co.Call.ModuleInstance(caller)
return moduleAddr.OutputValue(co.Name)
}