(v node, typeName string, typeRange *hcl.Range, labelsLeft []string, labelsUsed []string, labelRanges []hcl.Range, blocks *hcl.Blocks)
| 233 | } |
| 234 | |
| 235 | func (b *body) unpackBlock(v node, typeName string, typeRange *hcl.Range, labelsLeft []string, labelsUsed []string, labelRanges []hcl.Range, blocks *hcl.Blocks) (diags hcl.Diagnostics) { |
| 236 | if len(labelsLeft) > 0 { |
| 237 | labelName := labelsLeft[0] |
| 238 | jsonAttrs, attrDiags := b.collectDeepAttrs(v, &labelName) |
| 239 | diags = append(diags, attrDiags...) |
| 240 | |
| 241 | if len(jsonAttrs) == 0 { |
| 242 | diags = diags.Append(&hcl.Diagnostic{ |
| 243 | Severity: hcl.DiagError, |
| 244 | Summary: "Missing block label", |
| 245 | Detail: fmt.Sprintf("At least one object property is required, whose name represents the %s block's %s.", typeName, labelName), |
| 246 | Subject: v.StartRange().Ptr(), |
| 247 | }) |
| 248 | return |
| 249 | } |
| 250 | labelsUsed := append(labelsUsed, "") |
| 251 | labelRanges := append(labelRanges, hcl.Range{}) |
| 252 | for _, p := range jsonAttrs { |
| 253 | pk := p.Name |
| 254 | labelsUsed[len(labelsUsed)-1] = pk |
| 255 | labelRanges[len(labelRanges)-1] = p.NameRange |
| 256 | diags = append(diags, b.unpackBlock(p.Value, typeName, typeRange, labelsLeft[1:], labelsUsed, labelRanges, blocks)...) |
| 257 | } |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | // By the time we get here, we've peeled off all the labels and we're ready |
| 262 | // to deal with the block's actual content. |
| 263 | |
| 264 | // need to copy the label slices because their underlying arrays will |
| 265 | // continue to be mutated after we return. |
| 266 | labels := make([]string, len(labelsUsed)) |
| 267 | copy(labels, labelsUsed) |
| 268 | labelR := make([]hcl.Range, len(labelRanges)) |
| 269 | copy(labelR, labelRanges) |
| 270 | |
| 271 | switch tv := v.(type) { |
| 272 | case *nullVal: |
| 273 | // There is no block content, e.g the value is null. |
| 274 | return |
| 275 | case *objectVal: |
| 276 | // Single instance of the block |
| 277 | *blocks = append(*blocks, &hcl.Block{ |
| 278 | Type: typeName, |
| 279 | Labels: labels, |
| 280 | Body: &body{ |
| 281 | val: tv, |
| 282 | }, |
| 283 | |
| 284 | DefRange: tv.OpenRange, |
| 285 | TypeRange: *typeRange, |
| 286 | LabelRanges: labelR, |
| 287 | }) |
| 288 | case *arrayVal: |
| 289 | // Multiple instances of the block |
| 290 | for _, av := range tv.Values { |
| 291 | *blocks = append(*blocks, &hcl.Block{ |
| 292 | Type: typeName, |
no test coverage detected