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

Method Current

hclwrite/ast_block.go:139–180  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

137}
138
139func (bl *blockLabels) Current() []string {
140 labelNames := make([]string, 0, len(bl.items))
141 list := bl.items.List()
142
143 for _, label := range list {
144 switch labelObj := label.content.(type) {
145 case *identifier:
146 if labelObj.token.Type == hclsyntax.TokenIdent {
147 labelString := string(labelObj.token.Bytes)
148 labelNames = append(labelNames, labelString)
149 }
150
151 case *quoted:
152 tokens := labelObj.tokens
153 if len(tokens) == 3 &&
154 tokens[0].Type == hclsyntax.TokenOQuote &&
155 tokens[1].Type == hclsyntax.TokenQuotedLit &&
156 tokens[2].Type == hclsyntax.TokenCQuote {
157 // Note that TokenQuotedLit may contain escape sequences.
158 labelString, diags := hclsyntax.ParseStringLiteralToken(tokens[1].asHCLSyntax())
159
160 // If parsing the string literal returns error diagnostics
161 // then we can just assume the label doesn't match, because it's invalid in some way.
162 if !diags.HasErrors() {
163 labelNames = append(labelNames, labelString)
164 }
165 } else if len(tokens) == 2 &&
166 tokens[0].Type == hclsyntax.TokenOQuote &&
167 tokens[1].Type == hclsyntax.TokenCQuote {
168 // An open quote followed immediately by a closing quote is a
169 // valid but unusual blank string label.
170 labelNames = append(labelNames, "")
171 }
172
173 default:
174 // If neither of the previous cases are true (should be impossible)
175 // then we can just ignore it, because it's invalid too.
176 }
177 }
178
179 return labelNames
180}

Callers 1

LabelsMethod · 0.80

Calls 4

ParseStringLiteralTokenFunction · 0.92
ListMethod · 0.80
asHCLSyntaxMethod · 0.80
HasErrorsMethod · 0.80

Tested by

no test coverage detected