-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathquery_tags.go
More file actions
32 lines (29 loc) · 1.04 KB
/
query_tags.go
File metadata and controls
32 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package dbsql
import "strings"
// SerializeQueryTags converts a map of query tags to the wire format string.
// The format is comma-separated key:value pairs (e.g., "team:engineering,app:etl").
//
// Escaping rules (consistent with Python and NodeJS connectors):
// - Keys: only backslashes are escaped
// - Values: backslashes, colons, and commas are escaped with a leading backslash
// - Empty string values result in just the key being emitted (no colon)
//
// Returns empty string if the map is nil or empty.
func SerializeQueryTags(tags map[string]string) string {
if len(tags) == 0 {
return ""
}
parts := make([]string, 0, len(tags))
for k, v := range tags {
escapedKey := strings.ReplaceAll(k, `\`, `\\`)
if v == "" {
parts = append(parts, escapedKey)
} else {
escapedValue := strings.ReplaceAll(v, `\`, `\\`)
escapedValue = strings.ReplaceAll(escapedValue, `:`, `\:`)
escapedValue = strings.ReplaceAll(escapedValue, `,`, `\,`)
parts = append(parts, escapedKey+":"+escapedValue)
}
}
return strings.Join(parts, ",")
}