Skip to content

Commit

Permalink
fix: add quotes to identifier with special character
Browse files Browse the repository at this point in the history
Signed-off-by: jakezhu9 <[email protected]>
  • Loading branch information
jakezhu9 committed Nov 27, 2023
1 parent 17b297a commit 756db42
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/tools/gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "embed"
"fmt"
"io"
"regexp"
"strings"
"text/template"
)
Expand Down Expand Up @@ -124,10 +125,15 @@ var kclKeywords = map[string]struct{}{
"rule": {},
}

var validNameRegexp = regexp.MustCompile(`\$?^[a-zA-Z_][a-zA-Z0-9_]*$`)

func formatName(name string) string {
if _, ok := kclKeywords[name]; ok {
return fmt.Sprintf("$%s", name)
}
if !validNameRegexp.MatchString(name) {
return fmt.Sprintf(`"%s"`, name)
}
return name
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/tools/gen/testdata/yaml/identifier/expect.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

identifier = {
"a-b" = "identifier with hyphen"
a_b = "identifier with underscore"
$if = "identifier with keyword"
"a/b" = "identifier with slash"
"a.b" = "identifier with dot"
"a#b" = "identifier with hash"
"a$b" = "identifier with dollar"
"a@b" = "identifier with at"
"a%b" = "identifier with percent"
"a*b" = "identifier with asterisk"
"a&b" = "identifier with ampersand"
"a(b" = "identifier with left parenthesis"
"a)b" = "identifier with right parenthesis"
"a,b" = "identifier with comma"
"a:b" = "identifier with colon"
"a;b" = "identifier with semicolon"
"a<b" = "identifier with less than"
"a>b" = "identifier with greater than"
"a=b" = "identifier with equal"
"a+b" = "identifier with plus"
"a\\b" = "identifier with backslash"
"a[b" = "identifier with left bracket"
"a]b" = "identifier with right bracket"
"a{b" = "identifier with left brace"
"a}b" = "identifier with right brace"
"a'b" = "identifier with single quote"
"a`b" = "identifier with back quote"
"a~b" = "identifier with tilde"
"a!b" = "identifier with exclamation"
"a?b" = "identifier with question"
"a|b" = "identifier with vertical bar"
"a^b" = "identifier with caret"
}
33 changes: 33 additions & 0 deletions pkg/tools/gen/testdata/yaml/identifier/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
identifier:
a-b: "identifier with hyphen"
a_b: "identifier with underscore"
if: "identifier with keyword"
a/b: "identifier with slash"
a.b: "identifier with dot"
a#b: "identifier with hash"
a$b: "identifier with dollar"
a@b: "identifier with at"
a%b: "identifier with percent"
a*b: "identifier with asterisk"
a&b: "identifier with ampersand"
a(b: "identifier with left parenthesis"
a)b: "identifier with right parenthesis"
a,b: "identifier with comma"
a:b: "identifier with colon"
a;b: "identifier with semicolon"
a<b: "identifier with less than"
a>b: "identifier with greater than"
a=b: "identifier with equal"
a+b: "identifier with plus"
a\\b: "identifier with backslash"
a[b: "identifier with left bracket"
a]b: "identifier with right bracket"
a{b: "identifier with left brace"
a}b: "identifier with right brace"
a'b: "identifier with single quote"
a`b: "identifier with back quote"
a~b: "identifier with tilde"
a!b: "identifier with exclamation"
a?b: "identifier with question"
a|b: "identifier with vertical bar"
a^b: "identifier with caret"

0 comments on commit 756db42

Please sign in to comment.