Skip to content

Commit

Permalink
Avoid escaping string values with equal sign in complex parameters
Browse files Browse the repository at this point in the history
All equal signs of an string assignment in complex parameters were
required to be escaped, e.g.

`--parts "id=hello\\=world"`

Extended the type converter so that the part after an equal sign will
always be treated as a simple value without splitting it again by the
equal sign.

This allows the user to use values with equals signs, like base64
encoded strings without the need to escape, e.g.

`--parts "id=hello=world"

This will set the id value to 'hello=world'.
  • Loading branch information
thschmitt committed May 10, 2024
1 parent d2379a7 commit b571383
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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

0 comments on commit b571383

Please sign in to comment.