diff options
author | Jake Champlin <jake.champlin.27@gmail.com> | 2017-06-06 12:40:07 -0400 |
---|---|---|
committer | Jake Champlin <jake.champlin.27@gmail.com> | 2017-06-06 12:40:07 -0400 |
commit | bae9f6d2fd5eb5bc80929bd393932b23f14d7c93 (patch) | |
tree | ca9ab12a7d78b1fc27a8f734729081357ce6d252 /vendor/github.com/hashicorp/hil/ast/output.go | |
parent | 254c495b6bebab3fb72a243c4bce858d79e6ee99 (diff) | |
download | terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.gz terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.zst terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.zip |
Initial transfer of provider code
Diffstat (limited to 'vendor/github.com/hashicorp/hil/ast/output.go')
-rw-r--r-- | vendor/github.com/hashicorp/hil/ast/output.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hil/ast/output.go b/vendor/github.com/hashicorp/hil/ast/output.go new file mode 100644 index 0000000..1e27f97 --- /dev/null +++ b/vendor/github.com/hashicorp/hil/ast/output.go | |||
@@ -0,0 +1,78 @@ | |||
1 | package ast | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "fmt" | ||
6 | ) | ||
7 | |||
8 | // Output represents the root node of all interpolation evaluations. If the | ||
9 | // output only has one expression which is either a TypeList or TypeMap, the | ||
10 | // Output can be type-asserted to []interface{} or map[string]interface{} | ||
11 | // respectively. Otherwise the Output evaluates as a string, and concatenates | ||
12 | // the evaluation of each expression. | ||
13 | type Output struct { | ||
14 | Exprs []Node | ||
15 | Posx Pos | ||
16 | } | ||
17 | |||
18 | func (n *Output) Accept(v Visitor) Node { | ||
19 | for i, expr := range n.Exprs { | ||
20 | n.Exprs[i] = expr.Accept(v) | ||
21 | } | ||
22 | |||
23 | return v(n) | ||
24 | } | ||
25 | |||
26 | func (n *Output) Pos() Pos { | ||
27 | return n.Posx | ||
28 | } | ||
29 | |||
30 | func (n *Output) GoString() string { | ||
31 | return fmt.Sprintf("*%#v", *n) | ||
32 | } | ||
33 | |||
34 | func (n *Output) String() string { | ||
35 | var b bytes.Buffer | ||
36 | for _, expr := range n.Exprs { | ||
37 | b.WriteString(fmt.Sprintf("%s", expr)) | ||
38 | } | ||
39 | |||
40 | return b.String() | ||
41 | } | ||
42 | |||
43 | func (n *Output) Type(s Scope) (Type, error) { | ||
44 | // Special case no expressions for backward compatibility | ||
45 | if len(n.Exprs) == 0 { | ||
46 | return TypeString, nil | ||
47 | } | ||
48 | |||
49 | // Special case a single expression of types list or map | ||
50 | if len(n.Exprs) == 1 { | ||
51 | exprType, err := n.Exprs[0].Type(s) | ||
52 | if err != nil { | ||
53 | return TypeInvalid, err | ||
54 | } | ||
55 | switch exprType { | ||
56 | case TypeList: | ||
57 | return TypeList, nil | ||
58 | case TypeMap: | ||
59 | return TypeMap, nil | ||
60 | } | ||
61 | } | ||
62 | |||
63 | // Otherwise ensure all our expressions are strings | ||
64 | for index, expr := range n.Exprs { | ||
65 | exprType, err := expr.Type(s) | ||
66 | if err != nil { | ||
67 | return TypeInvalid, err | ||
68 | } | ||
69 | // We only look for things we know we can't coerce with an implicit conversion func | ||
70 | if exprType == TypeList || exprType == TypeMap { | ||
71 | return TypeInvalid, fmt.Errorf( | ||
72 | "multi-expression HIL outputs may only have string inputs: %d is type %s", | ||
73 | index, exprType) | ||
74 | } | ||
75 | } | ||
76 | |||
77 | return TypeString, nil | ||
78 | } | ||