@@ -3,9 +3,10 @@ package annotations
33import (
44 "encoding/json"
55 "fmt"
6- "github.com/pkg/errors"
76 "strconv"
87 "strings"
8+
9+ "github.com/pkg/errors"
910)
1011
1112type ParseOptions struct {
@@ -151,15 +152,15 @@ func (p *suffixAnnotationParser) ParseStringMapAnnotation(annotation string, val
151152 if ! exists {
152153 return false , nil
153154 }
154- rawKVPairs := splitCommaSeparatedString (raw )
155+ rawKVPairs := splitKeyValuePairs (raw )
155156 keyValues := make (map [string ]string )
156157 for _ , kvPair := range rawKVPairs {
157158 parts := strings .SplitN (kvPair , "=" , 2 )
158159 if len (parts ) != 2 {
159160 return false , errors .Errorf ("failed to parse stringMap annotation, %v: %v" , matchedKey , raw )
160161 }
161- key := parts [0 ]
162- value := parts [1 ]
162+ key := strings . TrimSpace ( parts [0 ])
163+ value := strings . TrimSpace ( parts [1 ])
163164 if len (key ) == 0 {
164165 return false , errors .Errorf ("failed to parse stringMap annotation, %v: %v" , matchedKey , raw )
165166 }
@@ -212,3 +213,41 @@ func splitCommaSeparatedString(commaSeparatedString string) []string {
212213 }
213214 return result
214215}
216+
217+ // splitKeyValuePairs this function supports escaped comma
218+ func splitKeyValuePairs (s string ) []string {
219+ var result []string
220+ var currentString strings.Builder
221+
222+ for i := 0 ; i < len (s ); i ++ {
223+ if s [i ] == '\\' && i + 1 < len (s ) {
224+ // Escape sequence - add the escaped character literally
225+ currentString .WriteByte (s [i + 1 ])
226+ i ++ // skip the escaped character
227+ } else if s [i ] == ',' {
228+ // Check if next part contains '=' (a new key-value pair)
229+ remaining := strings .TrimSpace (s [i + 1 :])
230+ containsEqual := strings .Index (remaining , "=" )
231+ if containsEqual != - 1 {
232+ result = append (result , strings .TrimSpace (currentString .String ()))
233+ currentString .Reset ()
234+ } else if len (remaining ) > 0 {
235+ // There's content after comma but no '=' - this is malformed
236+ // Add current content and the malformed part as separate items
237+ result = append (result , strings .TrimSpace (currentString .String ()))
238+ result = append (result , remaining )
239+ return result
240+ } else {
241+ // Empty after comma, add comma to current
242+ currentString .WriteByte (s [i ])
243+ }
244+ } else {
245+ currentString .WriteByte (s [i ])
246+ }
247+ }
248+
249+ if currentString .Len () > 0 {
250+ result = append (result , strings .TrimSpace (currentString .String ()))
251+ }
252+ return result
253+ }
0 commit comments