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 3d5d91d..04abfb4 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,7 @@ package hocon import ( "fmt" + "regexp" "strconv" "strings" "time" @@ -287,7 +288,12 @@ func (s String) Type() Type { return StringType } func (s String) String() string { str := strings.Trim(string(s), `"`) - if strings.Contains(string(s), ":") { + if str == "" { + return `""` + } + compile := regexp.MustCompile("[ !\\\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~]+") + + if compile.MatchString(str) { return fmt.Sprintf(`"%s"`, str) } return str diff --git a/config_test.go b/config_test.go index 1d6f8d0..b8975ca 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}") @@ -364,18 +369,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) { @@ -384,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]") @@ -395,13 +404,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, "[\"!@#$%^&*()_+\",\"{}[]|;':\",./<>?\\\"]") }) }