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

Function ValidIdentifier

hclsyntax/public.go:199–205  ·  view source on GitHub ↗

ValidIdentifier tests if the given string could be a valid identifier in a native syntax expression. This is useful when accepting names from the user that will be used as variable or attribute names in the scope, to ensure that any name chosen will be traversable using the variable or attribute tr

(s string)

Source from the content-addressed store, hash-verified

197// variable or attribute names in the scope, to ensure that any name chosen
198// will be traversable using the variable or attribute traversal syntax.
199func ValidIdentifier(s string) bool {
200 // This is a kinda-expensive way to do something pretty simple, but it
201 // is easiest to do with our existing scanner-related infrastructure here
202 // and nobody should be validating identifiers in a tight loop.
203 tokens := scanTokens([]byte(s), "", hcl.Pos{}, scanIdentOnly)
204 return len(tokens) == 2 && tokens[0].Type == TokenIdent && tokens[1].Type == TokenEOF
205}

Callers 3

appendTokensForValueFunction · 0.92
TypeStringFunction · 0.92
TestValidIdentifierFunction · 0.85

Calls 1

scanTokensFunction · 0.85

Tested by 1

TestValidIdentifierFunction · 0.68