-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
binder_nodes.go
189 lines (167 loc) 路 5.79 KB
/
binder_nodes.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
// Copyright 2016-2020, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pcl
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)
// bindNode binds a single node in a program. The node's dependencies are bound prior to the node itself; it is an
// error for a node to depend--directly or indirectly--upon itself.
func (b *binder) bindNode(node Node) hcl.Diagnostics {
if node.isBound() {
return nil
}
if node.isBinding() {
// TODO(pdg): print trace
rng := node.SyntaxNode().Range()
return hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "circular reference",
Subject: &rng,
}}
}
node.markBinding()
var diagnostics hcl.Diagnostics
deps := b.getDependencies(node)
node.setDependencies(deps)
// Bind any nodes this node depends on.
for _, dep := range deps {
diags := b.bindNode(dep)
diagnostics = append(diagnostics, diags...)
}
switch node := node.(type) {
case *ConfigVariable:
diags := b.bindConfigVariable(node)
diagnostics = append(diagnostics, diags...)
case *LocalVariable:
diags := b.bindLocalVariable(node)
diagnostics = append(diagnostics, diags...)
case *Resource:
diags := b.bindResource(node)
diagnostics = append(diagnostics, diags...)
case *Component:
diags := b.bindComponent(node)
diagnostics = append(diagnostics, diags...)
case *OutputVariable:
diags := b.bindOutputVariable(node)
diagnostics = append(diagnostics, diags...)
default:
contract.Failf("unexpected node of type %T (%v)", node, node.SyntaxNode().Range())
}
node.markBound()
return diagnostics
}
// getDependencies returns the dependencies for the given node.
func (b *binder) getDependencies(node Node) []Node {
depSet := codegen.Set{}
var deps []Node
diags := hclsyntax.VisitAll(node.SyntaxNode(), func(node hclsyntax.Node) hcl.Diagnostics {
depName := ""
switch node := node.(type) {
case *hclsyntax.FunctionCallExpr:
// TODO(pdg): function scope binds tighter than "normal" scope
depName = node.Name
case *hclsyntax.ScopeTraversalExpr:
depName = node.Traversal.RootName()
default:
return nil
}
// Missing reference errors will be issued during expression binding.
referent, _ := b.root.BindReference(depName)
if node, ok := referent.(Node); ok && !depSet.Has(node) {
depSet.Add(node)
deps = append(deps, node)
}
return nil
})
contract.Assertf(len(diags) == 0, "unexpected diagnostics: %v", diags)
return SourceOrderNodes(deps)
}
func expressionIsLiteralNull(expr model.Expression) bool {
switch expr := expr.(type) {
case *model.LiteralValueExpression:
return expr.Value.IsNull()
default:
return false
}
}
func (b *binder) bindConfigVariable(node *ConfigVariable) hcl.Diagnostics {
block, diagnostics := model.BindBlock(node.syntax, model.StaticScope(b.root), b.tokens, b.options.modelOptions()...)
if defaultValue, ok := block.Body.Attribute("default"); ok {
node.DefaultValue = defaultValue.Value
// when default is null and the type is not already optional
// turn the config type T into option(T)
if expressionIsLiteralNull(node.DefaultValue) && !model.IsOptionalType(node.typ) {
node.typ = model.NewOptionalType(node.typ)
node.Nullable = true
}
if model.InputType(node.typ).ConversionFrom(node.DefaultValue.Type()) == model.NoConversion {
errorDiagnostic := model.ExprNotConvertible(model.InputType(node.typ), node.DefaultValue)
diagnostics = append(diagnostics, errorDiagnostic)
}
}
if attr, ok := block.Body.Attribute(LogicalNamePropertyKey); ok {
logicalName, lDiags := getStringAttrValue(attr)
if lDiags != nil {
diagnostics = diagnostics.Append(lDiags)
} else {
node.logicalName = logicalName
}
}
if descriptionAttr, ok := block.Body.Attribute("description"); ok {
description, diags := getStringAttrValue(descriptionAttr)
if diags != nil {
diagnostics = diagnostics.Append(diags)
} else {
node.Description = description
}
}
if nullableAttr, ok := block.Body.Attribute("nullable"); ok {
nullable, diags := getBooleanAttributeValue(nullableAttr)
if diags != nil {
diagnostics = diagnostics.Append(diags)
} else {
node.Nullable = nullable
}
}
node.Definition = block
return diagnostics
}
func (b *binder) bindLocalVariable(node *LocalVariable) hcl.Diagnostics {
attr, diagnostics := model.BindAttribute(node.syntax, b.root, b.tokens, b.options.modelOptions()...)
node.Definition = attr
return diagnostics
}
func (b *binder) bindOutputVariable(node *OutputVariable) hcl.Diagnostics {
block, diagnostics := model.BindBlock(node.syntax, model.StaticScope(b.root), b.tokens, b.options.modelOptions()...)
if logicalNameAttr, ok := block.Body.Attribute(LogicalNamePropertyKey); ok {
logicalName, lDiags := getStringAttrValue(logicalNameAttr)
if lDiags != nil {
diagnostics = diagnostics.Append(lDiags)
} else {
node.logicalName = logicalName
}
}
if value, ok := block.Body.Attribute("value"); ok {
node.Value = value.Value
if model.InputType(node.typ).ConversionFrom(node.Value.Type()) == model.NoConversion {
diagnostics = append(diagnostics, model.ExprNotConvertible(model.InputType(node.typ), node.Value))
}
}
node.Definition = block
return diagnostics
}