github.com/terraform-linters/tflint-plugin-sdk@v0.22.0/hclext/structure_example_test.go (about)

     1  package hclext
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"github.com/hashicorp/hcl/v2/hclsyntax"
     8  )
     9  
    10  func ExampleContent() {
    11  	src := `
    12  noodle "foo" "bar" {
    13  	type = "rice"
    14  
    15  	bread "baz" {
    16  		type  = "focaccia"
    17  		baked = true
    18  	}
    19  	bread "quz" {
    20  		type = "rye"
    21  	}
    22  }`
    23  	file, diags := hclsyntax.ParseConfig([]byte(src), "test.tf", hcl.InitialPos)
    24  	if diags.HasErrors() {
    25  		panic(diags)
    26  	}
    27  
    28  	body, diags := Content(file.Body, &BodySchema{
    29  		Blocks: []BlockSchema{
    30  			{
    31  				Type:       "noodle",
    32  				LabelNames: []string{"name", "subname"},
    33  				Body: &BodySchema{
    34  					Attributes: []AttributeSchema{{Name: "type"}},
    35  					Blocks: []BlockSchema{
    36  						{
    37  							Type:       "bread",
    38  							LabelNames: []string{"name"},
    39  							Body: &BodySchema{
    40  								Attributes: []AttributeSchema{
    41  									{Name: "type", Required: true},
    42  									{Name: "baked"},
    43  								},
    44  							},
    45  						},
    46  					},
    47  				},
    48  			},
    49  		},
    50  	})
    51  	if diags.HasErrors() {
    52  		panic(diags)
    53  	}
    54  
    55  	for i, noodle := range body.Blocks {
    56  		fmt.Printf("- noodle[%d]: labels=%s, attributes=%d\n", i, noodle.Labels, len(noodle.Body.Attributes))
    57  		for i, bread := range noodle.Body.Blocks {
    58  			fmt.Printf("  - bread[%d]: labels=%s, attributes=%d\n", i, bread.Labels, len(bread.Body.Attributes))
    59  		}
    60  	}
    61  	// Output:
    62  	// - noodle[0]: labels=[foo bar], attributes=1
    63  	//   - bread[0]: labels=[baz], attributes=2
    64  	//   - bread[1]: labels=[quz], attributes=1
    65  }
    66  
    67  func ExamplePartialContent() {
    68  	src := `
    69  noodle "foo" "bar" {
    70  	type = "rice"
    71  
    72  	bread "baz" {
    73  		type  = "focaccia"
    74  		baked = true
    75  	}
    76  	bread "quz" {
    77  		type = "rye"
    78  	}
    79  }`
    80  	file, diags := hclsyntax.ParseConfig([]byte(src), "test.tf", hcl.InitialPos)
    81  	if diags.HasErrors() {
    82  		panic(diags)
    83  	}
    84  
    85  	body, diags := PartialContent(file.Body, &BodySchema{
    86  		Blocks: []BlockSchema{
    87  			{
    88  				Type:       "noodle",
    89  				LabelNames: []string{"name", "subname"},
    90  				Body: &BodySchema{
    91  					Blocks: []BlockSchema{
    92  						{
    93  							Type:       "bread",
    94  							LabelNames: []string{"name"},
    95  							Body: &BodySchema{
    96  								Attributes: []AttributeSchema{
    97  									{Name: "type", Required: true},
    98  								},
    99  							},
   100  						},
   101  					},
   102  				},
   103  			},
   104  		},
   105  	})
   106  	if diags.HasErrors() {
   107  		panic(diags)
   108  	}
   109  
   110  	for i, noodle := range body.Blocks {
   111  		fmt.Printf("- noodle[%d]: labels=%s, attributes=%d\n", i, noodle.Labels, len(noodle.Body.Attributes))
   112  		for i, bread := range noodle.Body.Blocks {
   113  			fmt.Printf("  - bread[%d]: labels=%s, attributes=%d\n", i, bread.Labels, len(bread.Body.Attributes))
   114  		}
   115  	}
   116  	// Output:
   117  	// - noodle[0]: labels=[foo bar], attributes=0
   118  	//   - bread[0]: labels=[baz], attributes=1
   119  	//   - bread[1]: labels=[quz], attributes=1
   120  }