|
| 1 | +package blocktoattr |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/hashicorp/hcl/v2" |
| 7 | + "github.com/hashicorp/hcl/v2/hcldec" |
| 8 | + "github.com/hashicorp/hcl/v2/hclsyntax" |
| 9 | + "github.com/hashicorp/terraform/configs/configschema" |
| 10 | + "github.com/zclconf/go-cty/cty" |
| 11 | +) |
| 12 | + |
| 13 | +func ambiguousNestedBlock(nesting int) *configschema.NestedBlock { |
| 14 | + ret := &configschema.NestedBlock{ |
| 15 | + Nesting: configschema.NestingList, |
| 16 | + Block: configschema.Block{ |
| 17 | + Attributes: map[string]*configschema.Attribute{ |
| 18 | + "a": {Type: cty.String, Required: true}, |
| 19 | + "b": {Type: cty.String, Optional: true}, |
| 20 | + }, |
| 21 | + }, |
| 22 | + } |
| 23 | + if nesting > 0 { |
| 24 | + ret.BlockTypes = map[string]*configschema.NestedBlock{ |
| 25 | + "nested0": ambiguousNestedBlock(nesting - 1), |
| 26 | + "nested1": ambiguousNestedBlock(nesting - 1), |
| 27 | + "nested2": ambiguousNestedBlock(nesting - 1), |
| 28 | + "nested3": ambiguousNestedBlock(nesting - 1), |
| 29 | + "nested4": ambiguousNestedBlock(nesting - 1), |
| 30 | + "nested5": ambiguousNestedBlock(nesting - 1), |
| 31 | + "nested6": ambiguousNestedBlock(nesting - 1), |
| 32 | + "nested7": ambiguousNestedBlock(nesting - 1), |
| 33 | + "nested8": ambiguousNestedBlock(nesting - 1), |
| 34 | + "nested9": ambiguousNestedBlock(nesting - 1), |
| 35 | + } |
| 36 | + } |
| 37 | + return ret |
| 38 | +} |
| 39 | + |
| 40 | +func schemaWithAmbiguousNestedBlock(nesting int) *configschema.Block { |
| 41 | + return &configschema.Block{ |
| 42 | + BlockTypes: map[string]*configschema.NestedBlock{ |
| 43 | + "maybe_block": ambiguousNestedBlock(nesting), |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const configForFixupBlockAttrsBenchmark = ` |
| 49 | +maybe_block { |
| 50 | + a = "hello" |
| 51 | + b = "world" |
| 52 | + nested0 { |
| 53 | + a = "the" |
| 54 | + nested1 { |
| 55 | + a = "deeper" |
| 56 | + nested2 { |
| 57 | + a = "we" |
| 58 | + nested3 { |
| 59 | + a = "go" |
| 60 | + b = "inside" |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +` |
| 67 | + |
| 68 | +func configBodyForFixupBlockAttrsBenchmark() hcl.Body { |
| 69 | + f, diags := hclsyntax.ParseConfig([]byte(configForFixupBlockAttrsBenchmark), "", hcl.Pos{Line: 1, Column: 1}) |
| 70 | + if diags.HasErrors() { |
| 71 | + panic("test configuration is invalid") |
| 72 | + } |
| 73 | + return f.Body |
| 74 | +} |
| 75 | + |
| 76 | +func BenchmarkFixUpBlockAttrs(b *testing.B) { |
| 77 | + for i := 0; i < b.N; i++ { |
| 78 | + b.StopTimer() |
| 79 | + body := configBodyForFixupBlockAttrsBenchmark() |
| 80 | + schema := schemaWithAmbiguousNestedBlock(5) |
| 81 | + b.StartTimer() |
| 82 | + |
| 83 | + spec := schema.DecoderSpec() |
| 84 | + fixedBody := FixUpBlockAttrs(body, schema) |
| 85 | + val, diags := hcldec.Decode(fixedBody, spec, nil) |
| 86 | + if diags.HasErrors() { |
| 87 | + b.Fatal("diagnostics during decoding", diags) |
| 88 | + } |
| 89 | + if !val.Type().IsObjectType() { |
| 90 | + b.Fatal("result is not an object") |
| 91 | + } |
| 92 | + blockVal := val.GetAttr("maybe_block") |
| 93 | + if !blockVal.Type().IsListType() || blockVal.LengthInt() != 1 { |
| 94 | + b.Fatal("result has wrong value for 'maybe_block'") |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments