MCPcopy
hub / github.com/hashicorp/hcl / FirstMatchingBlock

Method FirstMatchingBlock

hclwrite/ast_body.go:124–138  ·  view source on GitHub ↗

FirstMatchingBlock returns a first matching block from the body that has the given name and labels or returns nil if there is currently no matching block.

(typeName string, labels []string)

Source from the content-addressed store, hash-verified

122// given name and labels or returns nil if there is currently no matching
123// block.
124func (b *Body) FirstMatchingBlock(typeName string, labels []string) *Block {
125 for _, block := range b.Blocks() {
126 if typeName == block.Type() {
127 labelNames := block.Labels()
128 if len(labels) == 0 && len(labelNames) == 0 {
129 return block
130 }
131 if reflect.DeepEqual(labels, labelNames) {
132 return block
133 }
134 }
135 }
136
137 return nil
138}
139
140// RemoveBlock removes the given block from the body, if it's in that body.
141// If it isn't present, this is a no-op.

Callers 6

TestBodyRemoveBlockFunction · 0.80
TestBlockSetTypeFunction · 0.80
TestBlockSetLabelsFunction · 0.80

Implementers 8

Bodyhclsyntax/structure.go
unknownBodyext/dynblock/unknown_body.go
expandBodyext/dynblock/expand_body.go
deepWrapperext/transform/transform.go
diagBodyext/transform/error.go
bodyjson/structure.go
mockBodyhcltest/mock.go

Calls 3

BlocksMethod · 0.95
LabelsMethod · 0.80
TypeMethod · 0.45

Tested by 6

TestBodyRemoveBlockFunction · 0.64
TestBlockSetTypeFunction · 0.64
TestBlockSetLabelsFunction · 0.64