From 756db42aa96715194e33a9f8931a70b7e8842d7f Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Mon, 27 Nov 2023 07:18:27 +0000 Subject: [PATCH] fix: add quotes to identifier with special character Signed-off-by: jakezhu9 --- pkg/tools/gen/template.go | 6 +++ .../gen/testdata/yaml/identifier/expect.k | 39 +++++++++++++++++++ .../gen/testdata/yaml/identifier/input.yaml | 33 ++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 pkg/tools/gen/testdata/yaml/identifier/expect.k create mode 100644 pkg/tools/gen/testdata/yaml/identifier/input.yaml diff --git a/pkg/tools/gen/template.go b/pkg/tools/gen/template.go index 1b51e970..eda4a4fe 100644 --- a/pkg/tools/gen/template.go +++ b/pkg/tools/gen/template.go @@ -5,6 +5,7 @@ import ( _ "embed" "fmt" "io" + "regexp" "strings" "text/template" ) @@ -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 } diff --git a/pkg/tools/gen/testdata/yaml/identifier/expect.k b/pkg/tools/gen/testdata/yaml/identifier/expect.k new file mode 100644 index 00000000..fc1a59c5 --- /dev/null +++ b/pkg/tools/gen/testdata/yaml/identifier/expect.k @@ -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" + "ab" = "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" +} diff --git a/pkg/tools/gen/testdata/yaml/identifier/input.yaml b/pkg/tools/gen/testdata/yaml/identifier/input.yaml new file mode 100644 index 00000000..40a6b2d5 --- /dev/null +++ b/pkg/tools/gen/testdata/yaml/identifier/input.yaml @@ -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" + ab: "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"