-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody.go
37 lines (30 loc) · 854 Bytes
/
body.go
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
package krabhcl
import (
"fmt"
"github.com/hashicorp/hcl/v2"
)
type Body struct {
hcl.Body
}
func (b *Body) DefRangesFromPartialContentBlocks(schema *hcl.BodySchema) []hcl.Range {
ret := []hcl.Range{}
content, _, diags := b.PartialContent(schema)
if len(diags) > 0 {
panic(fmt.Sprintf("krabhcl.Body failed to extract PartialContent from schema: %v", diags))
}
for _, block := range content.Blocks {
ret = append(ret, block.DefRange)
}
return ret
}
func (b *Body) DefRangesFromPartialContentAttributes(schema *hcl.BodySchema) map[string]hcl.Range {
ret := map[string]hcl.Range{}
content, _, diags := b.PartialContent(schema)
if len(diags) > 0 {
panic(fmt.Sprintf("krabhcl.Body failed to extract PartialContent from schema: %v", diags))
}
for _, attr := range content.Attributes {
ret[attr.Name] = attr.Range
}
return ret
}