-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support escaped commas in service tags for connect (#1532)
* support escaped commas in service tag annotations
- Loading branch information
Showing
7 changed files
with
118 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package parsetags | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ParseTags parses the tags annotation into a slice of tags. | ||
// Tags are split on commas (except for escaped commas "\,"). | ||
func ParseTags(tagsAnno string) []string { | ||
|
||
// This algorithm parses the tagsAnno string into a slice of strings. | ||
// Ideally we'd just split on commas but since Consul tags support commas, | ||
// we allow users to escape commas so they're included in the tag, e.g. | ||
// the annotation "tag\,with\,commas,tag2" will become the tags: | ||
// ["tag,with,commas", "tag2"]. | ||
|
||
var tags []string | ||
// nextTag is built up char by char until we see a comma. Then we | ||
// append it to tags. | ||
var nextTag string | ||
|
||
for _, runeChar := range tagsAnno { | ||
runeStr := fmt.Sprintf("%c", runeChar) | ||
|
||
// Not a comma, just append to nextTag. | ||
if runeStr != "," { | ||
nextTag += runeStr | ||
continue | ||
} | ||
|
||
// Reached a comma but there's nothing in nextTag, | ||
// skip. (e.g. "a,,b" => ["a", "b"]) | ||
if len(nextTag) == 0 { | ||
continue | ||
} | ||
|
||
// Check if the comma was escaped comma, e.g. "a\,b". | ||
if string(nextTag[len(nextTag)-1]) == `\` { | ||
// Replace the backslash with a comma. | ||
nextTag = nextTag[0:len(nextTag)-1] + "," | ||
continue | ||
} | ||
|
||
// Non-escaped comma. We're ready to push nextTag onto tags and reset nextTag. | ||
tags = append(tags, strings.TrimSpace(nextTag)) | ||
nextTag = "" | ||
} | ||
|
||
// We're done the loop but nextTag still contains the last tag. | ||
if len(nextTag) > 0 { | ||
tags = append(tags, strings.TrimSpace(nextTag)) | ||
} | ||
|
||
return tags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package parsetags | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseTags(t *testing.T) { | ||
cases := []struct { | ||
tagsAnno string | ||
exp []string | ||
}{ | ||
{ | ||
"tag", | ||
[]string{"tag"}, | ||
}, | ||
{ | ||
",,removes,,empty,elems,,", | ||
[]string{"removes", "empty", "elems"}, | ||
}, | ||
{ | ||
"removes , white ,space ", | ||
[]string{"removes", "white", "space"}, | ||
}, | ||
{ | ||
`\,leading,comma`, | ||
[]string{",leading", "comma"}, | ||
}, | ||
{ | ||
`trailing,comma\,`, | ||
[]string{"trailing", "comma,"}, | ||
}, | ||
{ | ||
`mid\,dle,com\,ma`, | ||
[]string{"mid,dle", "com,ma"}, | ||
}, | ||
{ | ||
`\,\,multi\,\,,\,com\,\,ma`, | ||
[]string{",,multi,,", ",com,,ma"}, | ||
}, | ||
{ | ||
` every\,\, , thing `, | ||
[]string{"every,,", "thing"}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
require.Equal(t, c.exp, ParseTags(c.tagsAnno)) | ||
} | ||
} |