Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid escaping string values with equal sign in complex parameters #116

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion commandline/type_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c typeConverter) convertToObject(value string, parameter parser.Parameter)
obj := map[string]interface{}{}
assigns := c.splitEscaped(value, ';')
for _, assign := range assigns {
keyValue := c.splitEscaped(assign, '=')
keyValue := c.splitEscapedMaxCount(assign, '=', 2)
if len(keyValue) < 2 {
keyValue = append(keyValue, "")
}
Expand Down Expand Up @@ -243,6 +243,10 @@ func (c typeConverter) convertValueToBooleanArray(value string, parameter parser
}

func (c typeConverter) splitEscaped(str string, separator byte) []string {
return c.splitEscapedMaxCount(str, separator, -1)
}

func (c typeConverter) splitEscapedMaxCount(str string, separator byte, maxCount int) []string {
result := []string{}
item := []byte{}
escaping := false
Expand All @@ -256,6 +260,14 @@ func (c typeConverter) splitEscaped(str string, separator byte) []string {
} else if char == separator && !escaping {
result = append(result, string(item))
item = []byte{}
if len(result) == maxCount-1 {
if i+1 < len(str) {
item = []byte(str[i+1:])
result = append(result, string(item))
}
break
}

} else {
escaping = false
item = append(item, char)
Expand Down
15 changes: 15 additions & 0 deletions commandline/type_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ func TestNegativeIndexIsIgnored(t *testing.T) {
}
}

func TestConvertStringAvoidEscapeEqualSign(t *testing.T) {
converter := newTypeConverter()

parameter := newParameter("tag", parser.ParameterTypeObject,
[]parser.Parameter{
newParameter("name", parser.ParameterTypeString, []parser.Parameter{}),
})
result, _ := converter.Convert("name=hello=", parameter)

name := getValue(result, "name")
if name != "hello=" {
t.Errorf("Result should contain equal sign, but got: %v", name)
}
}

func getValue(result interface{}, key string) interface{} {
return result.(map[string]interface{})[key]
}
Expand Down
Loading