Skip to content

Commit afdbb01

Browse files
committed
support escaping comas in tags
1 parent 931ba4a commit afdbb01

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

pkg/common/utils.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package common
1818

1919
import (
20+
"bytes"
2021
"context"
2122
"errors"
2223
"fmt"
@@ -78,6 +79,8 @@ const (
7879
// Full or partial URL of the zone resource, in the format:
7980
// projects/{project}/zones/{zone}
8081
zoneURIPattern = "projects/[^/]+/zones/([^/]+)$"
82+
83+
escapeRune = '\\'
8184
)
8285

8386
var (
@@ -280,7 +283,7 @@ func ConvertLabelsStringToMap(labels string) (map[string]string, error) {
280283
// See https://cloud.google.com/resource-manager/docs/tags/tags-overview,
281284
// https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing for details
282285
func ConvertTagsStringToMap(tags string) (map[string]string, error) {
283-
const tagsDelimiter = ","
286+
const tagsDelimiter = ','
284287
const tagsParentIDKeyValueDelimiter = "/"
285288

286289
tagsMap := make(map[string]string)
@@ -311,7 +314,7 @@ func ConvertTagsStringToMap(tags string) (map[string]string, error) {
311314
}
312315

313316
checkTagParentIDKey := sets.String{}
314-
parentIDkeyValueStrings := strings.Split(tags, tagsDelimiter)
317+
parentIDkeyValueStrings := splitWithEscape(tags, tagsDelimiter, escapeRune)
315318
for _, parentIDkeyValueString := range parentIDkeyValueStrings {
316319
parentIDKeyValue := strings.Split(parentIDkeyValueString, tagsParentIDKeyValueDelimiter)
317320

pkg/common/utils_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,41 @@ func TestConvertTagsStringToMap(t *testing.T) {
618618
expectedOutput: nil,
619619
expectedError: true,
620620
},
621+
{
622+
name: "tag value contains escaped delimiters",
623+
tags: "parent1/key1/value\\,with\\,comas,parent2/key2/value3",
624+
expectedOutput: map[string]string{
625+
"parent1/key1": "value,with,comas",
626+
"parent2/key2": "value3",
627+
},
628+
expectedError: false,
629+
},
630+
{
631+
name: "tag value contains unescaped comas",
632+
tags: "parent1/key1/value,with,comas,parent2/key2/value3",
633+
expectedOutput: map[string]string{
634+
"parent1/key1": "value,with,comas",
635+
"parent2/key2": "value3",
636+
},
637+
expectedError: true,
638+
},
639+
{
640+
name: "tag value contains escapes for non delimiter characters",
641+
tags: "p\\arent1/key1/value1,parent2/key2/value2",
642+
expectedOutput: map[string]string{
643+
"parent1/key1": "value1",
644+
"parent2/key2": "value2",
645+
},
646+
expectedError: false,
647+
},
648+
{
649+
name: "tag value contains a sequence of escape characters",
650+
tags: "parent1/key1/\\\\value\\",
651+
expectedOutput: map[string]string{
652+
"parent1/key1": "value",
653+
},
654+
expectedError: false,
655+
},
621656
}
622657

623658
for _, tc := range testCases {
@@ -756,6 +791,11 @@ func TestConvertTagsStringToMap(t *testing.T) {
756791
tags: "parent/k/Special@value[10]{20}(30)-example",
757792
expectedError: false,
758793
},
794+
{
795+
name: "tag value may contain escaped comas",
796+
tags: "parent/k/v\\,v",
797+
expectedError: false,
798+
},
759799
}
760800

761801
for _, tc := range testCases {

0 commit comments

Comments
 (0)