-
Notifications
You must be signed in to change notification settings - Fork 232
/
context.go
436 lines (414 loc) · 11 KB
/
context.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package schemahcl
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
)
// blockVar is an HCL resource that defines an input variable to the Atlas DDL document.
type blockVar struct {
Name string `hcl:",label"`
Type cty.Value `hcl:"type"`
Default cty.Value `hcl:"default,optional"`
Description string `hcl:"description,optional"`
}
// setInputVals sets the input values into the evaluation context. HCL documents can define
// input variables in the document body by defining "variable" blocks:
//
// variable "name" {
// type = string // also supported: number, bool
// default = "rotemtam"
// }
func (s *State) setInputVals(ctx *hcl.EvalContext, body hcl.Body, input map[string]cty.Value) error {
var doc struct {
Vars []*blockVar `hcl:"variable,block"`
Remain hcl.Body `hcl:",remain"`
}
if diag := gohcl.DecodeBody(body, ctx, &doc); diag.HasErrors() {
return diag
}
ctxVars := make(map[string]cty.Value)
for _, v := range doc.Vars {
var vv cty.Value
switch iv, ok := input[v.Name]; {
case !v.Type.Type().IsCapsuleType():
return fmt.Errorf(
"invalid type %q for variable %q. Valid types are: string, number, bool, list, map, or set",
v.Type.AsString(), v.Name,
)
case ok:
vv = iv
case v.Default != cty.NilVal:
vv = v.Default
default:
return fmt.Errorf("missing value for required variable %q", v.Name)
}
vt := v.Type.EncapsulatedValue().(*cty.Type)
// In case the input value is a primitive type and the expected type is a list,
// wrap it as a list because the variable type may not be known to the caller.
if vt.IsListType() && vv.Type().Equals(vt.ElementType()) {
vv = cty.ListVal([]cty.Value{vv})
}
cv, err := convert.Convert(vv, *vt)
if err != nil {
return fmt.Errorf("variable %q: %w", v.Name, err)
}
ctxVars[v.Name] = cv
}
mergeCtxVar(ctx, ctxVars)
return nil
}
// evalReferences evaluates data blocks.
func (s *State) evalReferences(ctx *hcl.EvalContext, body *hclsyntax.Body) error {
type node struct {
addr [3]string
edges func() []hcl.Traversal
value func() (cty.Value, error)
}
var (
nodes = make(map[[3]string]*node)
blocks = make(hclsyntax.Blocks, 0, len(body.Blocks))
)
for _, b := range body.Blocks {
switch b := b; b.Type {
case dataBlock:
if len(b.Labels) < 2 {
return fmt.Errorf("data block %q must have exactly 2 labels", b.Type)
}
h, ok := s.config.datasrc[b.Labels[0]]
if !ok {
return fmt.Errorf("missing data source handler for %q", b.Labels[0])
}
// Data references are combined from
// "data", "source" and "name" labels.
addr := [3]string{dataBlock, b.Labels[0], b.Labels[1]}
nodes[addr] = &node{
addr: addr,
value: func() (cty.Value, error) { return h(ctx, b) },
edges: func() []hcl.Traversal { return bodyVars(b.Body) },
}
case localsBlock:
for k, v := range b.Body.Attributes {
k, v := k, v
// Local references are combined from
// "local" and "name" labels.
addr := [3]string{localRef, k, ""}
nodes[addr] = &node{
addr: addr,
edges: func() []hcl.Traversal { return hclsyntax.Variables(v.Expr) },
value: func() (cty.Value, error) {
v, diags := v.Expr.Value(ctx)
if diags.HasErrors() {
return cty.NilVal, diags
}
return v, nil
},
}
}
default:
blocks = append(blocks, b)
}
}
var (
visit func(n *node) error
visited = make(map[*node]bool)
progress = make(map[*node]bool)
)
visit = func(n *node) error {
if visited[n] {
return nil
}
if progress[n] {
addr := n.addr[:]
if addr[2] == "" {
addr = addr[:2]
}
return fmt.Errorf("cyclic reference to %q", strings.Join(addr, "."))
}
progress[n] = true
for _, e := range n.edges() {
var addr [3]string
switch root := e.RootName(); {
case root == localRef && len(e) == 2:
addr = [3]string{localRef, e[1].(hcl.TraverseAttr).Name, ""}
case root == dataBlock && len(e) > 2:
addr = [3]string{dataBlock, e[1].(hcl.TraverseAttr).Name, e[2].(hcl.TraverseAttr).Name}
}
// Unrecognized reference.
if nodes[addr] == nil {
continue
}
if err := visit(nodes[addr]); err != nil {
return err
}
}
delete(progress, n)
v, err := n.value()
if err != nil {
return err
}
switch n.addr[0] {
case dataBlock:
data := make(map[string]cty.Value)
if vv, ok := ctx.Variables[dataBlock]; ok {
data = vv.AsValueMap()
}
src := make(map[string]cty.Value)
if vv, ok := data[n.addr[1]]; ok {
src = vv.AsValueMap()
}
src[n.addr[2]] = v
data[n.addr[1]] = cty.ObjectVal(src)
ctx.Variables[dataBlock] = cty.ObjectVal(data)
case localRef:
locals := make(map[string]cty.Value)
if vv, ok := ctx.Variables[localRef]; ok {
locals = vv.AsValueMap()
}
locals[n.addr[1]] = v
ctx.Variables[localRef] = cty.ObjectVal(locals)
}
return nil
}
for _, n := range nodes {
if err := visit(n); err != nil {
return err
}
}
body.Blocks = blocks
return nil
}
func mergeCtxVar(ctx *hcl.EvalContext, vals map[string]cty.Value) {
v, ok := ctx.Variables[varRef]
if ok {
v.ForEachElement(func(key cty.Value, val cty.Value) (stop bool) {
vals[key.AsString()] = val
return false
})
}
ctx.Variables[varRef] = cty.ObjectVal(vals)
}
func setBlockVars(ctx *hcl.EvalContext, b *hclsyntax.Body) (*hcl.EvalContext, error) {
defs := defRegistry(b)
vars, err := blockVars(b.Blocks, "", defs)
if err != nil {
return nil, err
}
if ctx.Variables == nil {
ctx.Variables = make(map[string]cty.Value)
}
for k, v := range vars {
ctx.Variables[k] = v
}
return ctx, nil
}
func blockVars(blocks hclsyntax.Blocks, parentAddr string, defs *blockDef) (map[string]cty.Value, error) {
vars := make(map[string]cty.Value)
for name, def := range defs.children {
v := make(map[string]cty.Value)
qv := make(map[string]map[string]cty.Value)
blocks := blocksOfType(blocks, name)
if len(blocks) == 0 {
vars[name] = cty.NullVal(def.asCty())
continue
}
var unlabeled int
for _, blk := range blocks {
qualifier, blkName := blockName(blk)
if blkName == "" {
blkName = strconv.Itoa(unlabeled)
unlabeled++
}
attrs := attrMap(blk.Body.Attributes)
// Fill missing attributes with zero values.
for n := range def.fields {
if _, ok := attrs[n]; !ok {
attrs[n] = cty.NullVal(ctyNilType)
}
}
self := addr(parentAddr, name, blkName, qualifier)
attrs["__ref"] = cty.StringVal(self)
varMap, err := blockVars(blk.Body.Blocks, self, def)
if err != nil {
return nil, err
}
// Merge children blocks in.
for k, v := range varMap {
attrs[k] = v
}
switch {
case qualifier != "":
obj := cty.ObjectVal(attrs)
if _, ok := qv[qualifier]; !ok {
qv[qualifier] = make(map[string]cty.Value)
}
qv[qualifier][blkName] = obj
obj = cty.ObjectVal(qv[qualifier])
v[qualifier] = obj
default:
v[blkName] = cty.ObjectVal(attrs)
}
}
if len(v) > 0 {
vars[name] = cty.ObjectVal(v)
}
}
return vars, nil
}
func addr(parentAddr, typeName, blkName, qualifier string) string {
var b strings.Builder
if parentAddr != "" {
b.WriteString(parentAddr)
b.WriteString(".")
}
b.WriteByte('$')
b.WriteString(typeName)
for _, s := range []string{qualifier, blkName} {
switch {
case s == "":
case validIdent(s):
b.WriteString(".")
b.WriteString(s)
default:
b.WriteString("[")
b.WriteString(strconv.Quote(s))
b.WriteString("]")
}
}
return b.String()
}
// validIdent reports if the given string can
// be used as an identifier in a reference.
func validIdent(s string) bool {
_, err := cty.ParseNumberVal(s)
return err == nil || hclsyntax.ValidIdentifier(s)
}
func blockName(blk *hclsyntax.Block) (qualifier string, name string) {
switch len(blk.Labels) {
case 0:
case 1:
name = blk.Labels[0]
default:
qualifier = blk.Labels[0]
name = blk.Labels[1]
}
return
}
func blocksOfType(blocks hclsyntax.Blocks, typeName string) []*hclsyntax.Block {
var out []*hclsyntax.Block
for _, block := range blocks {
if block.Type == typeName {
out = append(out, block)
}
}
return out
}
func attrMap(attrs hclsyntax.Attributes) map[string]cty.Value {
out := make(map[string]cty.Value)
for _, v := range attrs {
value, diag := v.Expr.Value(nil)
if diag.HasErrors() {
continue
}
out[v.Name] = value
}
return out
}
var (
ctyNilType = cty.Capsule("type", reflect.TypeOf(cty.NilType))
ctyTypeSpec = cty.Capsule("type", reflect.TypeOf(Type{}))
ctyRefType = cty.Capsule("ref", reflect.TypeOf(Ref{}))
ctyRawExpr = cty.Capsule("raw", reflect.TypeOf(RawExpr{}))
)
// Built-in blocks.
const (
varBlock = "variable"
dataBlock = "data"
localsBlock = "locals"
forEachAttr = "for_each"
eachRef = "each"
varRef = "var"
localRef = "local"
)
// defRegistry returns a tree of blockDef structs representing the schema of the
// blocks in the *hclsyntax.Body. The returned fields and children of each type
// are an intersection of all existing blocks of the same type.
func defRegistry(b *hclsyntax.Body) *blockDef {
reg := &blockDef{
fields: make(map[string]struct{}),
children: make(map[string]*blockDef),
}
for _, blk := range b.Blocks {
// variable definition blocks are available in the HCL source but not reachable by reference.
if blk.Type == varBlock {
continue
}
reg.child(extractDef(blk, reg))
}
return reg
}
// blockDef describes a type of block in the HCL document.
type blockDef struct {
name string
fields map[string]struct{}
parent *blockDef
children map[string]*blockDef
}
// child updates the definition for the child type of the blockDef.
func (t *blockDef) child(c *blockDef) {
ex, ok := t.children[c.name]
if !ok {
t.children[c.name] = c
return
}
for f := range c.fields {
ex.fields[f] = struct{}{}
}
for _, c := range c.children {
ex.child(c)
}
}
// asCty returns a cty.Type representing the blockDef.
func (t *blockDef) asCty() cty.Type {
f := make(map[string]cty.Type)
for attr := range t.fields {
f[attr] = ctyNilType
}
f["__ref"] = cty.String
for _, c := range t.children {
f[c.name] = c.asCty()
}
return cty.Object(f)
}
func extractDef(blk *hclsyntax.Block, parent *blockDef) *blockDef {
cur := &blockDef{
name: blk.Type,
parent: parent,
fields: make(map[string]struct{}),
children: make(map[string]*blockDef),
}
for _, a := range blk.Body.Attributes {
cur.fields[a.Name] = struct{}{}
}
for _, c := range blk.Body.Blocks {
cur.child(extractDef(c, cur))
}
return cur
}
func bodyVars(b *hclsyntax.Body) (vars []hcl.Traversal) {
for _, attr := range b.Attributes {
vars = append(vars, hclsyntax.Variables(attr.Expr)...)
}
for _, b := range b.Blocks {
vars = append(vars, bodyVars(b.Body)...)
}
return
}