Skip to content

Commit aabc284

Browse files
authored
Add hclext.Blocks's OfType helper (#169)
1 parent 94e00d7 commit aabc284

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

hclext/structure.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ func PartialContent(body hcl.Body, schema *BodySchema) (*BodyContent, hcl.Diagno
158158
return ret, diags
159159
}
160160

161+
// OfType filters the receiving block sequence by block type name,
162+
// returning a new block sequence including only the blocks of the
163+
// requested type.
164+
func (els Blocks) OfType(typeName string) Blocks {
165+
ret := make(Blocks, 0)
166+
for _, el := range els {
167+
if el.Type == typeName {
168+
ret = append(ret, el)
169+
}
170+
}
171+
return ret
172+
}
173+
161174
// ByType transforms the receiving block sequence into a map from type
162175
// name to block sequences of only that type.
163176
func (els Blocks) ByType() map[string]Blocks {

0 commit comments

Comments
 (0)