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

Function appendTokensForValue

hclwrite/generate.go:185–305  ·  view source on GitHub ↗
(val cty.Value, toks Tokens)

Source from the content-addressed store, hash-verified

183}
184
185func appendTokensForValue(val cty.Value, toks Tokens) Tokens {
186 switch {
187
188 case !val.IsKnown():
189 panic("cannot produce tokens for unknown value")
190
191 case val.IsNull():
192 toks = append(toks, &Token{
193 Type: hclsyntax.TokenIdent,
194 Bytes: []byte(`null`),
195 })
196
197 case val.Type() == cty.Bool:
198 var src []byte
199 if val.True() {
200 src = []byte(`true`)
201 } else {
202 src = []byte(`false`)
203 }
204 toks = append(toks, &Token{
205 Type: hclsyntax.TokenIdent,
206 Bytes: src,
207 })
208
209 case val.Type() == cty.Number:
210 bf := val.AsBigFloat()
211 srcStr := bf.Text('f', -1)
212 toks = append(toks, &Token{
213 Type: hclsyntax.TokenNumberLit,
214 Bytes: []byte(srcStr),
215 })
216
217 case val.Type() == cty.String:
218 // TODO: If it's a multi-line string ending in a newline, format
219 // it as a HEREDOC instead.
220 src := escapeQuotedStringLit(val.AsString())
221 toks = append(toks, &Token{
222 Type: hclsyntax.TokenOQuote,
223 Bytes: []byte{'"'},
224 })
225 if len(src) > 0 {
226 toks = append(toks, &Token{
227 Type: hclsyntax.TokenQuotedLit,
228 Bytes: src,
229 })
230 }
231 toks = append(toks, &Token{
232 Type: hclsyntax.TokenCQuote,
233 Bytes: []byte{'"'},
234 })
235
236 case val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType():
237 toks = append(toks, &Token{
238 Type: hclsyntax.TokenOBrack,
239 Bytes: []byte{'['},
240 })
241
242 i := 0

Callers 2

TokensForValueFunction · 0.85

Calls 3

ValidIdentifierFunction · 0.92
escapeQuotedStringLitFunction · 0.85
TypeMethod · 0.45

Tested by

no test coverage detected