File: transform_fixed.go

package info (click to toggle)
golang-github-hashicorp-hil 0.0~git20160711.1e86c6b-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 352 kB
  • sloc: yacc: 161; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 740 bytes parent folder | download
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
package hil

import (
	"github.com/hashicorp/hil/ast"
)

// FixedValueTransform transforms an AST to return a fixed value for
// all interpolations. i.e. you can make "hi ${anything}" always
// turn into "hi foo".
//
// The primary use case for this is for config validations where you can
// verify that interpolations result in a certain type of string.
func FixedValueTransform(root ast.Node, Value *ast.LiteralNode) ast.Node {
	// We visit the nodes in top-down order
	result := root
	switch n := result.(type) {
	case *ast.Output:
		for i, v := range n.Exprs {
			n.Exprs[i] = FixedValueTransform(v, Value)
		}
	case *ast.LiteralNode:
		// We keep it as-is
	default:
		// Anything else we replace
		result = Value
	}

	return result
}