-
Notifications
You must be signed in to change notification settings - Fork 0
/
intermediate.go
228 lines (193 loc) · 7.37 KB
/
intermediate.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
log "github.com/Sirupsen/logrus"
)
// arguments/inputs
type ResourceArgumentUsage struct {
Arg *ResourceArgument `form:"Arg" json:"Arg" xml:"Arg"`
UsagePath [][]string `form:"UsagePath" json:"UsagePath" xml:"UsagePath"`
}
type ModuleInputUsage struct {
Input *ModuleInstance `form:"Input" json:"Input" xml:"Input"`
UsagePath [][]string `form:"UsagePath" json:"UsagePath" xml:"UsagePath"`
}
type ModuleInput struct {
Name string `form:"Name" json:"Name" xml:"Name"`
IsLoaded bool `form:"-" json:"-" xml:"-"`
AsArgument []ResourceArgumentUsage `form:"AsArgument" json:"AsArgument" xml:"AsArgument"`
AsModuleInput []ModuleInputUsage `form:"AsModuleInput" json:"AsModuleInput" xml:"AsModuleInput"`
}
// attributes/outputs
type ResourceAttributeUsage struct {
Attr *ResourceAttribute `form:"Arg" json:"Arg" xml:"Arg"`
}
type ModuleOutputUsage struct {
Input *ModuleInstance `form:"Input" json:"Input" xml:"Input"`
}
type ModuleOutput struct {
Name string `form:"Name" json:"Name" xml:"Name"`
IsLoaded bool `form:"-" json:"-" xml:"-"`
FromAttribute []ResourceAttributeUsage `form:"FromAttribute" json:"FromAttribute" xml:"FromAttribute"`
FromModuleOutput []ModuleOutputUsage `form:"FromModuleOutput" json:"FromModuleOutput" xml:"FromModuleOutput"`
}
// modules
type ModuleInstance struct {
InstanceName string `form:"InstanceName" json:"InstanceName" xml:"InstanceName"`
ModulePath string `form:"ModulePath" json:"ModulePath" xml:"ModulePath"`
Instance *Module `form:"-" json:"-" xml:"-"`
}
type Module struct {
Name string `form:"Name" json:"Name" xml:"Name"`
IsLoaded bool `form:"-" json:"-" xml:"-"`
ModuleInstances []ModuleInstance `form:"ModuleInstances" json:"ModuleInstances" xml:"ModuleInstances"`
Inputs []*ModuleInput `form:"Inputs" json:"Inputs" xml:"Inputs"`
Outputs []*ModuleOutput `form:"Outputs" json:"Outputs" xml:"Outputs"`
}
// The state
type HierarchyState struct {
AllModules []Module `form:"AllModules" json:"AllModules" xml:"AllModules"`
allInputs []ModuleInput
allOutputs []ModuleOutput
allModulesMap map[string]*Module
allInputsMap map[string]*ModuleInput
allOutputsMap map[string]*ModuleOutput
}
func NewHierarchyState() *HierarchyState {
return &HierarchyState{
AllModules: make([]Module, 0, 128),
allInputs: make([]ModuleInput, 0, 2048),
allOutputs: make([]ModuleOutput, 0, 2048),
allModulesMap: make(map[string]*Module),
allInputsMap: make(map[string]*ModuleInput),
allOutputsMap: make(map[string]*ModuleOutput),
}
}
type VariableID string
type ResourceFieldID struct {
Name string
InstanceName string
FieldName string
}
type ModuleFieldID struct {
InstanceName string
FieldName string
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Module methods
func (h *HierarchyState) NewModule(name string) *Module {
m, found := h.allModulesMap[name]
if !found {
h.AllModules = append(h.AllModules,
Module{
Name: name,
IsLoaded: false,
ModuleInstances: make([]ModuleInstance, 0, 128),
Inputs: make([]*ModuleInput, 0, 128),
Outputs: make([]*ModuleOutput, 0, 128),
})
m = &h.AllModules[len(h.AllModules)-1]
h.allModulesMap[name] = m
}
return m
}
func (m *Module) FindModuleInstance(instanceName string) *ModuleInstance {
for _, instance := range m.ModuleInstances {
if instance.InstanceName == instanceName {
return &instance
}
}
return nil
}
func (m *Module) NewInstance(instanceName string, instanceSubmodulePath string, instance *Module) {
if nil == m.FindModuleInstance(instanceName) {
m.ModuleInstances = append(m.ModuleInstances, ModuleInstance{Instance: instance, ModulePath: instance.Name, InstanceName: instanceName})
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Module inputs
func (h *HierarchyState) NewInput(module *Module, id VariableID) *ModuleInput {
name := string(id)
inputKey := module.Name + "." + name
if "." == module.Name {
// root module
inputKey = "." + name
}
input, found := h.allInputsMap[inputKey]
if !found {
h.allInputs = append(h.allInputs, ModuleInput{Name: name, IsLoaded: false})
input = &h.allInputs[len(h.allInputs)-1]
h.allInputsMap[inputKey] = input
module.Inputs = append(module.Inputs, input)
}
return input
}
func (m *ModuleInput) AttachArgument(usagePath []string, argument *ResourceArgument) {
for _, elem := range m.AsArgument {
if elem.Arg == argument {
return
}
}
m.AsArgument = append(m.AsArgument, ResourceArgumentUsage{Arg: argument, UsagePath: [][]string{usagePath}})
}
func (m *ModuleInput) AttachModuleInput(usagePath []string, instance *ModuleInstance) {
for _, elem := range m.AsModuleInput {
if elem.Input == instance {
return
}
}
m.AsModuleInput = append(m.AsModuleInput, ModuleInputUsage{Input: instance, UsagePath: [][]string{usagePath}})
}
func (h *HierarchyState) ConnectInputToArgument(module *Module, id VariableID, usagePath []string, argument *ResourceArgument) {
log.Debugf("module %v name %v attach argument %v", module.Name, id, argument)
value := h.NewInput(module, id)
value.AttachArgument(usagePath, argument)
}
func (h *HierarchyState) ConnectInputToModuleInput(module *Module, id VariableID, usagePath []string, instance *ModuleInstance) {
log.Debugf("module %v name %v attach input %v", module.Name, id, instance)
value := h.NewInput(module, id)
value.AttachModuleInput(usagePath, instance)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Module outputs
func (h *HierarchyState) NewOutput(module *Module, id VariableID) *ModuleOutput {
name := string(id)
outputKey := module.Name + "." + name
if "." == module.Name {
// root module
outputKey = "." + name
}
output, found := h.allOutputsMap[outputKey]
if !found {
h.allOutputs = append(h.allOutputs, ModuleOutput{Name: name, IsLoaded: false})
output = &h.allOutputs[len(h.allOutputs)-1]
h.allOutputsMap[outputKey] = output
module.Outputs = append(module.Outputs, output)
}
return output
}
func (m *ModuleOutput) AttachAttribute(attribute *ResourceAttribute) {
for _, elem := range m.FromAttribute {
if elem.Attr == attribute {
return
}
}
m.FromAttribute = append(m.FromAttribute, ResourceAttributeUsage{Attr: attribute})
}
func (m *ModuleOutput) AttachModuleOutput(instance *ModuleInstance) {
for _, elem := range m.FromModuleOutput {
if elem.Input == instance {
return
}
}
m.FromModuleOutput = append(m.FromModuleOutput, ModuleOutputUsage{Input: instance})
}
func (h *HierarchyState) ConnectOutputToAttribute(module *Module, id VariableID, attribute *ResourceAttribute) {
log.Debugf("module %v name %v attach attribute %v", module.Name, id, attribute)
value := h.NewOutput(module, id)
value.AttachAttribute(attribute)
}
func (h *HierarchyState) ConnectOutputToModuleOutput(instance *ModuleInstance, id VariableID, moduleFieldUsage ModuleFieldID) {
log.Debugf("instance %v name %v attach module output %v", instance, moduleFieldUsage)
value := h.NewOutput(instance.Instance, id)
value.AttachModuleOutput(instance)
}