7 "github.com/hashicorp/hcl2/hcl"
10 // AsHCLBlock returns the block data expressed as a *hcl.Block.
11 func (b *Block) AsHCLBlock() *hcl.Block {
16 lastHeaderRange := b.TypeRange
17 if len(b.LabelRanges) > 0 {
18 lastHeaderRange = b.LabelRanges[len(b.LabelRanges)-1]
26 DefRange: hcl.RangeBetween(b.TypeRange, lastHeaderRange),
27 TypeRange: b.TypeRange,
28 LabelRanges: b.LabelRanges,
32 // Body is the implementation of hcl.Body for the HCL native syntax.
37 // These are used with PartialContent to produce a "remaining items"
38 // body to return. They are nil on all bodies fresh out of the parser.
39 hiddenAttrs map[string]struct{}
40 hiddenBlocks map[string]struct{}
43 EndRange hcl.Range // Final token of the body, for reporting missing items
46 // Assert that *Body implements hcl.Body
47 var assertBodyImplBody hcl.Body = &Body{}
49 func (b *Body) walkChildNodes(w internalWalkFunc) {
54 func (b *Body) Range() hcl.Range {
58 func (b *Body) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostics) {
59 content, remainHCL, diags := b.PartialContent(schema)
61 // No we'll see if anything actually remains, to produce errors about
63 remain := remainHCL.(*Body)
65 for name, attr := range b.Attributes {
66 if _, hidden := remain.hiddenAttrs[name]; !hidden {
67 var suggestions []string
68 for _, attrS := range schema.Attributes {
69 if _, defined := content.Attributes[attrS.Name]; defined {
72 suggestions = append(suggestions, attrS.Name)
74 suggestion := nameSuggestion(name, suggestions)
76 suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
78 // Is there a block of the same name?
79 for _, blockS := range schema.Blocks {
80 if blockS.Type == name {
81 suggestion = fmt.Sprintf(" Did you mean to define a block of type %q?", name)
87 diags = append(diags, &hcl.Diagnostic{
88 Severity: hcl.DiagError,
89 Summary: "Unsupported argument",
90 Detail: fmt.Sprintf("An argument named %q is not expected here.%s", name, suggestion),
91 Subject: &attr.NameRange,
96 for _, block := range b.Blocks {
98 if _, hidden := remain.hiddenBlocks[blockTy]; !hidden {
99 var suggestions []string
100 for _, blockS := range schema.Blocks {
101 suggestions = append(suggestions, blockS.Type)
103 suggestion := nameSuggestion(blockTy, suggestions)
104 if suggestion != "" {
105 suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
107 // Is there an attribute of the same name?
108 for _, attrS := range schema.Attributes {
109 if attrS.Name == blockTy {
110 suggestion = fmt.Sprintf(" Did you mean to define argument %q? If so, use the equals sign to assign it a value.", blockTy)
116 diags = append(diags, &hcl.Diagnostic{
117 Severity: hcl.DiagError,
118 Summary: "Unsupported block type",
119 Detail: fmt.Sprintf("Blocks of type %q are not expected here.%s", blockTy, suggestion),
120 Subject: &block.TypeRange,
125 return content, diags
128 func (b *Body) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) {
129 attrs := make(hcl.Attributes)
130 var blocks hcl.Blocks
131 var diags hcl.Diagnostics
132 hiddenAttrs := make(map[string]struct{})
133 hiddenBlocks := make(map[string]struct{})
135 if b.hiddenAttrs != nil {
136 for k, v := range b.hiddenAttrs {
140 if b.hiddenBlocks != nil {
141 for k, v := range b.hiddenBlocks {
146 for _, attrS := range schema.Attributes {
148 attr, exists := b.Attributes[name]
149 _, hidden := hiddenAttrs[name]
150 if hidden || !exists {
152 diags = append(diags, &hcl.Diagnostic{
153 Severity: hcl.DiagError,
154 Summary: "Missing required argument",
155 Detail: fmt.Sprintf("The argument %q is required, but no definition was found.", attrS.Name),
156 Subject: b.MissingItemRange().Ptr(),
162 hiddenAttrs[name] = struct{}{}
163 attrs[name] = attr.AsHCLAttribute()
166 blocksWanted := make(map[string]hcl.BlockHeaderSchema)
167 for _, blockS := range schema.Blocks {
168 blocksWanted[blockS.Type] = blockS
171 for _, block := range b.Blocks {
172 if _, hidden := hiddenBlocks[block.Type]; hidden {
175 blockS, wanted := blocksWanted[block.Type]
180 if len(block.Labels) > len(blockS.LabelNames) {
182 if len(blockS.LabelNames) == 0 {
183 diags = append(diags, &hcl.Diagnostic{
184 Severity: hcl.DiagError,
185 Summary: fmt.Sprintf("Extraneous label for %s", name),
187 "No labels are expected for %s blocks.", name,
189 Subject: block.LabelRanges[0].Ptr(),
190 Context: hcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(),
193 diags = append(diags, &hcl.Diagnostic{
194 Severity: hcl.DiagError,
195 Summary: fmt.Sprintf("Extraneous label for %s", name),
197 "Only %d labels (%s) are expected for %s blocks.",
198 len(blockS.LabelNames), strings.Join(blockS.LabelNames, ", "), name,
200 Subject: block.LabelRanges[len(blockS.LabelNames)].Ptr(),
201 Context: hcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(),
207 if len(block.Labels) < len(blockS.LabelNames) {
209 diags = append(diags, &hcl.Diagnostic{
210 Severity: hcl.DiagError,
211 Summary: fmt.Sprintf("Missing %s for %s", blockS.LabelNames[len(block.Labels)], name),
213 "All %s blocks must have %d labels (%s).",
214 name, len(blockS.LabelNames), strings.Join(blockS.LabelNames, ", "),
216 Subject: &block.OpenBraceRange,
217 Context: hcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(),
222 blocks = append(blocks, block.AsHCLBlock())
225 // We hide blocks only after we've processed all of them, since otherwise
226 // we can't process more than one of the same type.
227 for _, blockS := range schema.Blocks {
228 hiddenBlocks[blockS.Type] = struct{}{}
232 Attributes: b.Attributes,
235 hiddenAttrs: hiddenAttrs,
236 hiddenBlocks: hiddenBlocks,
238 SrcRange: b.SrcRange,
239 EndRange: b.EndRange,
242 return &hcl.BodyContent{
246 MissingItemRange: b.MissingItemRange(),
250 func (b *Body) JustAttributes() (hcl.Attributes, hcl.Diagnostics) {
251 attrs := make(hcl.Attributes)
252 var diags hcl.Diagnostics
254 if len(b.Blocks) > 0 {
255 example := b.Blocks[0]
256 diags = append(diags, &hcl.Diagnostic{
257 Severity: hcl.DiagError,
258 Summary: fmt.Sprintf("Unexpected %q block", example.Type),
259 Detail: "Blocks are not allowed here.",
260 Subject: &example.TypeRange,
262 // we will continue processing anyway, and return the attributes
263 // we are able to find so that certain analyses can still be done
264 // in the face of errors.
267 if b.Attributes == nil {
271 for name, attr := range b.Attributes {
272 if _, hidden := b.hiddenAttrs[name]; hidden {
275 attrs[name] = attr.AsHCLAttribute()
281 func (b *Body) MissingItemRange() hcl.Range {
283 Filename: b.SrcRange.Filename,
284 Start: b.SrcRange.Start,
285 End: b.SrcRange.Start,
289 // Attributes is the collection of attribute definitions within a body.
290 type Attributes map[string]*Attribute
292 func (a Attributes) walkChildNodes(w internalWalkFunc) {
293 for _, attr := range a {
298 // Range returns the range of some arbitrary point within the set of
299 // attributes, or an invalid range if there are no attributes.
301 // This is provided only to complete the Node interface, but has no practical
303 func (a Attributes) Range() hcl.Range {
304 // An attributes doesn't really have a useful range to report, since
305 // it's just a grouping construct. So we'll arbitrarily take the
306 // range of one of the attributes, or produce an invalid range if we have
307 // none. In practice, there's little reason to ask for the range of
309 for _, attr := range a {
313 Filename: "<unknown>",
317 // Attribute represents a single attribute definition within a body.
318 type Attribute struct {
324 EqualsRange hcl.Range
327 func (a *Attribute) walkChildNodes(w internalWalkFunc) {
331 func (a *Attribute) Range() hcl.Range {
335 // AsHCLAttribute returns the block data expressed as a *hcl.Attribute.
336 func (a *Attribute) AsHCLAttribute() *hcl.Attribute {
340 return &hcl.Attribute{
345 NameRange: a.NameRange,
349 // Blocks is the list of nested blocks within a body.
352 func (bs Blocks) walkChildNodes(w internalWalkFunc) {
353 for _, block := range bs {
358 // Range returns the range of some arbitrary point within the list of
359 // blocks, or an invalid range if there are no blocks.
361 // This is provided only to complete the Node interface, but has no practical
363 func (bs Blocks) Range() hcl.Range {
368 Filename: "<unknown>",
372 // Block represents a nested block structure
379 LabelRanges []hcl.Range
380 OpenBraceRange hcl.Range
381 CloseBraceRange hcl.Range
384 func (b *Block) walkChildNodes(w internalWalkFunc) {
388 func (b *Block) Range() hcl.Range {
389 return hcl.RangeBetween(b.TypeRange, b.CloseBraceRange)
392 func (b *Block) DefRange() hcl.Range {
393 return hcl.RangeBetween(b.TypeRange, b.OpenBraceRange)