-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Expand file tree
/
Copy pathfixup_bench_test.go
More file actions
97 lines (89 loc) · 2.48 KB
/
fixup_bench_test.go
File metadata and controls
97 lines (89 loc) · 2.48 KB
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
package blocktoattr
import (
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/zclconf/go-cty/cty"
)
func ambiguousNestedBlock(nesting int) *configschema.NestedBlock {
ret := &configschema.NestedBlock{
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"a": {Type: cty.String, Required: true},
"b": {Type: cty.String, Optional: true},
},
},
}
if nesting > 0 {
ret.BlockTypes = map[string]*configschema.NestedBlock{
"nested0": ambiguousNestedBlock(nesting - 1),
"nested1": ambiguousNestedBlock(nesting - 1),
"nested2": ambiguousNestedBlock(nesting - 1),
"nested3": ambiguousNestedBlock(nesting - 1),
"nested4": ambiguousNestedBlock(nesting - 1),
"nested5": ambiguousNestedBlock(nesting - 1),
"nested6": ambiguousNestedBlock(nesting - 1),
"nested7": ambiguousNestedBlock(nesting - 1),
"nested8": ambiguousNestedBlock(nesting - 1),
"nested9": ambiguousNestedBlock(nesting - 1),
}
}
return ret
}
func schemaWithAmbiguousNestedBlock(nesting int) *configschema.Block {
return &configschema.Block{
BlockTypes: map[string]*configschema.NestedBlock{
"maybe_block": ambiguousNestedBlock(nesting),
},
}
}
const configForFixupBlockAttrsBenchmark = `
maybe_block {
a = "hello"
b = "world"
nested0 {
a = "the"
nested1 {
a = "deeper"
nested2 {
a = "we"
nested3 {
a = "go"
b = "inside"
}
}
}
}
}
`
func configBodyForFixupBlockAttrsBenchmark() hcl.Body {
f, diags := hclsyntax.ParseConfig([]byte(configForFixupBlockAttrsBenchmark), "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
panic("test configuration is invalid")
}
return f.Body
}
func BenchmarkFixUpBlockAttrs(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
body := configBodyForFixupBlockAttrsBenchmark()
schema := schemaWithAmbiguousNestedBlock(5)
b.StartTimer()
spec := schema.DecoderSpec()
fixedBody := FixUpBlockAttrs(body, schema)
val, diags := hcldec.Decode(fixedBody, spec, nil)
if diags.HasErrors() {
b.Fatal("diagnostics during decoding", diags)
}
if !val.Type().IsObjectType() {
b.Fatal("result is not an object")
}
blockVal := val.GetAttr("maybe_block")
if !blockVal.Type().IsListType() || blockVal.LengthInt() != 1 {
b.Fatal("result has wrong value for 'maybe_block'")
}
}
}