Skip to content

Commit

Permalink
Merge pull request #22 from Rory-Z/chore/add-quotation
Browse files Browse the repository at this point in the history
add quotation mark for String
  • Loading branch information
gurkankaymak authored Aug 21, 2023
2 parents 9ccc664 + 9fcad82 commit 49329f9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.mod
go.sum
8 changes: 7 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hocon

import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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) {
Expand All @@ -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]")
Expand All @@ -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, "[\"!@#$%^&*()_+\",\"{}[]|;':\",./<>?\\\"]")
})
}

Expand Down

0 comments on commit 49329f9

Please sign in to comment.