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)
| 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. |
| 199 | func 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 | } |