From b712d9aa4de82ad9b34cf7365275a2a20e4b0111 Mon Sep 17 00:00:00 2001 From: Rory Z Date: Thu, 25 Aug 2022 11:58:32 +0800 Subject: [PATCH 1/4] fix: if the string element have forbidden characters, then add " for them --- .gitignore | 2 ++ config.go | 4 +++- config_test.go | 23 +++++++++++------------ 3 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f117bc7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +go.mod +go.sum \ No newline at end of file diff --git a/config.go b/config.go index 50d4d07..ad093e0 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,7 @@ package hocon import ( "fmt" + "regexp" "strconv" "strings" "time" @@ -287,7 +288,8 @@ func (s String) Type() Type { return StringType } func (s String) String() string { str := strings.Trim(string(s), `"`) - if strings.Contains(string(s), ":") { + compile := regexp.MustCompile("[\x20-\x40|\x5b-\x60|\x7b-\x7e]+") + if compile.MatchString(str) { return fmt.Sprintf(`"%s"`, str) } return str diff --git a/config_test.go b/config_test.go index 1d6f8d0..1688942 100644 --- a/config_test.go +++ b/config_test.go @@ -364,18 +364,17 @@ func TestObject_String(t *testing.T) { } }) - t.Run("return the string of an object that contains a string element with the ':' character", func(t *testing.T) { - got := Object{"a": String("0.0.0.0:80")}.String() - assertEquals(t, got, "{a:\"0.0.0.0:80\"}") + t.Run("return the string of an object that contains a single element with the forbidden characters", func(t *testing.T) { + got := Object{"a": String("!@#$%^&*()_+{}[];:',./<>?\"\\")}.String() + assertEquals(t, got, "{a:\"!@#$%^&*()_+{}[];:',./<>?\"\\\"}") }) - t.Run("return the string of an object that contains multiple elements with the ':' character", func(t *testing.T) { - got := Object{"a": String("0.0.0.0:80"), "b": Int(2)}.String() - if got != "{a:\"0.0.0.0:80\", b:2}" && got != "{b:2, a:\"0.0.0.0:80\"}" { - fail(t, got, "{a:1, b:2}") + t.Run("return the string of an object that contains multiple elements with the forbidden characters", func(t *testing.T) { + got := Object{"a": String("!@#$%^&*()_+{}[];:',./<>?\"\\"), "b": Int(2)}.String() + if got != "{a:\"!@#$%^&*()_+{}[];:',./<>?\"\\\", b:2}" && got != "{b:2, a:\"!@#$%^&*()_+{}[];:',./<>?\"}" { + fail(t, got, "{a:\"!@#$%^&*()_+{}[];:',./<>?\"\\\", b:2}") } }) - } func TestArray_String(t *testing.T) { @@ -395,13 +394,13 @@ func TestArray_String(t *testing.T) { }) t.Run("return the string of an array that contains a single elements with the ':' character", func(t *testing.T) { - got := Array{String("0.0.0.0:80")}.String() - assertEquals(t, got, "[\"0.0.0.0:80\"]") + got := Array{String("!@#$%^&*()_+{}[];:',./<>?\"\\")}.String() + assertEquals(t, got, "[\"!@#$%^&*()_+{}[];:',./<>?\"\\\"]") }) t.Run("return the string of an array that contains multiple elements with the ':' character", func(t *testing.T) { - got := Array{String("0.0.0.0:80"), String("localhost:443")}.String() - assertEquals(t, got, "[\"0.0.0.0:80\",\"localhost:443\"]") + got := Array{String("!@#$%^&*()_+"), String("{}[]|;':\",./<>?\\")}.String() + assertEquals(t, got, "[\"!@#$%^&*()_+\",\"{}[]|;':\",./<>?\\\"]") }) } From a02c1abad0961c33b144ab530d60f516ee1d2171 Mon Sep 17 00:00:00 2001 From: Rory Z Date: Fri, 26 Aug 2022 15:28:25 +0800 Subject: [PATCH 2/4] fix: fix String() function error when have a empty string --- config.go | 3 +++ config_test.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/config.go b/config.go index ad093e0..c35ab12 100644 --- a/config.go +++ b/config.go @@ -288,6 +288,9 @@ func (s String) Type() Type { return StringType } func (s String) String() string { str := strings.Trim(string(s), `"`) + if str == "" { + return `""` + } compile := regexp.MustCompile("[\x20-\x40|\x5b-\x60|\x7b-\x7e]+") if compile.MatchString(str) { return fmt.Sprintf(`"%s"`, str) diff --git a/config_test.go b/config_test.go index 1688942..b684458 100644 --- a/config_test.go +++ b/config_test.go @@ -352,6 +352,11 @@ func TestObject_String(t *testing.T) { assertEquals(t, got, "{}") }) + t.Run("return the string of an object that contains a empty string", func(t *testing.T) { + got := Object{"a": String("")}.String() + assertEquals(t, got, "{a:\"\"}") + }) + t.Run("return the string of an object that contains a single element", func(t *testing.T) { got := Object{"a": Int(1)}.String() assertEquals(t, got, "{a:1}") From cf53a5dcb947de6f2d5a6e01bcc24ee8b6cd40d3 Mon Sep 17 00:00:00 2001 From: Rory Z Date: Mon, 5 Sep 2022 14:12:42 +0800 Subject: [PATCH 3/4] test: add more test case --- config_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config_test.go b/config_test.go index b684458..b8975ca 100644 --- a/config_test.go +++ b/config_test.go @@ -388,6 +388,11 @@ func TestArray_String(t *testing.T) { assertEquals(t, got, "[]") }) + t.Run("return the string of an object that contains a empty string", func(t *testing.T) { + got := Array{String("")}.String() + assertEquals(t, got, "[\"\"]") + }) + t.Run("return the string of an array that contains a single element", func(t *testing.T) { got := Array{Int(1)}.String() assertEquals(t, got, "[1]") From 9fcad82fc250b3e70e5ae2e1e85c7b5af5bd3806 Mon Sep 17 00:00:00 2001 From: Rory Z <16801068+Rory-Z@users.noreply.github.com> Date: Thu, 17 Aug 2023 17:18:44 +0800 Subject: [PATCH 4/4] chore: replace regular expressions containing ASCII. Signed-off-by: Rory Z <16801068+Rory-Z@users.noreply.github.com> --- config.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index c35ab12..b4cd533 100644 --- a/config.go +++ b/config.go @@ -291,7 +291,8 @@ func (s String) String() string { if str == "" { return `""` } - compile := regexp.MustCompile("[\x20-\x40|\x5b-\x60|\x7b-\x7e]+") + compile := regexp.MustCompile("[ !\\\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~]+") + if compile.MatchString(str) { return fmt.Sprintf(`"%s"`, str) }