/
nested_block.go
241 lines (221 loc) · 5.54 KB
/
nested_block.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package rules
import (
"fmt"
"math"
"sort"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
)
// NestedBlock is a wrapper of the nested block
type NestedBlock struct {
File *hcl.File
Block *hclsyntax.Block
Name string
SortField string
Range hcl.Range
HeadMetaArgs *HeadMetaArgs
Args *Args
NestedBlocks *NestedBlocks
ParentBlockNames []string
emit func(block Block) error
}
// CheckBlock checks the nestedBlock recursively to find the block not in order,
// and invoke the emit function on that block
func (b *NestedBlock) CheckBlock() error {
if !b.CheckOrder() {
return b.emit(b)
}
if b.NestedBlocks == nil {
return nil
}
var err error
for _, nb := range b.NestedBlocks.Blocks {
if subErr := nb.CheckBlock(); subErr != nil {
err = multierror.Append(err, subErr)
}
}
return err
}
// DefRange gets the definition range of the nested block
func (b *NestedBlock) DefRange() hcl.Range {
return b.Block.DefRange()
}
// CheckOrder checks whether the nestedBlock is sorted
func (b *NestedBlock) CheckOrder() bool {
return b.checkSubSectionOrder() && b.checkGap()
}
// ToString prints the sorted block
func (b *NestedBlock) ToString() string {
headMeta := toString(b.HeadMetaArgs)
args := toString(b.Args)
nb := toString(b.NestedBlocks)
var codes []string
for _, c := range []string{headMeta, args, nb} {
if c != "" {
codes = append(codes, c)
}
}
code := strings.Join(codes, "\n\n")
blockHead := string(b.Block.DefRange().SliceBytes(b.File.Bytes))
if strings.TrimSpace(code) == "" {
code = fmt.Sprintf("%s {}", blockHead)
} else {
code = fmt.Sprintf("%s {\n%s\n}", blockHead, code)
}
return string(hclwrite.Format([]byte(code)))
}
// NestedBlocks is the collection of nestedBlocks with the same type
type NestedBlocks struct {
Blocks []*NestedBlock
Range *hcl.Range
}
// CheckOrder checks whether this type of nestedBlocks are sorted
func (b *NestedBlocks) CheckOrder() bool {
return true
}
// ToString prints this type of nestedBlocks in order
func (b *NestedBlocks) ToString() string {
if b == nil {
return ""
}
sortedBlocks := make([]*NestedBlock, len(b.Blocks))
copy(sortedBlocks, b.Blocks)
sort.Slice(sortedBlocks, func(i, j int) bool {
return sortedBlocks[i].Range.Start.Line < sortedBlocks[j].Range.Start.Line
})
var lines []string
for _, nb := range sortedBlocks {
lines = append(lines, nb.ToString())
}
return string(hclwrite.Format([]byte(strings.Join(lines, "\n"))))
}
// GetRange returns the entire range of this type of nestedBlocks
func (b *NestedBlocks) GetRange() *hcl.Range {
if b == nil {
return nil
}
return b.Range
}
func (b *NestedBlocks) add(arg *NestedBlock) {
b.Blocks = append(b.Blocks, arg)
if b.Range == nil {
b.Range = &hcl.Range{
Filename: arg.Range.Filename,
Start: hcl.Pos{Line: math.MaxInt},
End: hcl.Pos{Line: -1},
}
}
if b.Range.Start.Line > arg.Range.Start.Line {
b.Range.Start = arg.Range.Start
}
if b.Range.End.Line < arg.Range.End.Line {
b.Range.End = arg.Range.End
}
}
func (b *NestedBlock) buildAttributes(attributes hclsyntax.Attributes) {
attrs := attributesByLines(attributes)
for _, attr := range attrs {
attrName := attr.Name
arg := buildAttrArg(attr, b.File)
if IsHeadMeta(attrName) {
b.addHeadMeta(arg)
continue
}
b.addAttribute(arg)
}
}
func (b *NestedBlock) buildNestedBlocks(nestedBlock hclsyntax.Blocks) {
for _, nb := range nestedBlock {
b.buildNestedBlock(nb)
}
}
func (b *NestedBlock) buildNestedBlock(nestedBlock *hclsyntax.Block) {
var nestedBlockName, sortField string
switch nestedBlock.Type {
case "dynamic":
nestedBlockName = nestedBlock.Labels[0]
sortField = strings.Join(nestedBlock.Labels, "")
default:
nestedBlockName = nestedBlock.Type
sortField = nestedBlock.Type
}
var parentBlockNames []string
if nestedBlockName == "content" && b.Block.Type == "dynamic" {
parentBlockNames = b.ParentBlockNames
} else {
parentBlockNames = append(b.ParentBlockNames, nestedBlockName)
}
nb := &NestedBlock{
Name: nestedBlockName,
SortField: sortField,
Range: nestedBlock.Range(),
Block: nestedBlock,
ParentBlockNames: parentBlockNames,
File: b.File,
emit: b.emit,
}
nb.buildAttributes(nestedBlock.Body.Attributes)
nb.buildNestedBlocks(nestedBlock.Body.Blocks)
b.addNestedBlock(nb)
}
func (b *NestedBlock) addHeadMeta(arg *Arg) {
if b.HeadMetaArgs == nil {
b.HeadMetaArgs = &HeadMetaArgs{}
}
b.HeadMetaArgs.add(arg)
}
func (b *NestedBlock) addAttribute(arg *Arg) {
if b.Args == nil {
b.Args = &Args{}
}
b.Args.add(arg)
}
func (b *NestedBlock) addNestedBlock(nb *NestedBlock) {
if b.NestedBlocks == nil {
b.NestedBlocks = &NestedBlocks{}
}
b.NestedBlocks.add(nb)
}
func (b *NestedBlock) checkSubSectionOrder() bool {
sections := []Section{
b.HeadMetaArgs,
b.Args,
b.NestedBlocks,
}
lastEndLine := -1
for _, s := range sections {
if !s.CheckOrder() {
return false
}
r := s.GetRange()
if r == nil {
continue
}
if r.Start.Line <= lastEndLine {
return false
}
lastEndLine = r.End.Line
}
return true
}
func (b *NestedBlock) checkGap() bool {
lastEndLine := -2
ranges := []*hcl.Range{
b.HeadMetaArgs.GetRange(),
b.Args.GetRange(),
b.NestedBlocks.GetRange(),
}
for _, r := range ranges {
if r == nil {
continue
}
if r.Start.Line-lastEndLine < 2 {
return false
}
lastEndLine = r.End.Line
}
return true
}