Skip to content

Commit

Permalink
chore: refactor escaping tag values
Browse files Browse the repository at this point in the history
  • Loading branch information
karel-rehor committed Oct 17, 2024
1 parent f2d0d59 commit d0fc985
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions influxdb3/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ func (p *Point) MarshalBinaryWithDefaultTags(precision lineprotocol.Precision, d
enc.SetPrecision(precision)
enc.StartLine(p.Values.MeasurementName)

// N.B. Some customers have requested support for newline and tab chars in tag values (EAR 5476)
// Though this is outside the lineprotocol specification, it was supported in
// previous GO client versions.
tagEscapes := map[string]string{
"\n": "\\n",
"\t": "\\t",
}

replacer := func(reps map[string]string, s string) string {
result := s
for key, val := range reps {
result = strings.ReplaceAll(result, key, val)
}
return result
}

// sort Tags
tagKeys := make([]string, 0, len(p.Values.Tags)+len(defaultTags))
for k := range p.Values.Tags {
Expand All @@ -270,17 +286,10 @@ func (p *Point) MarshalBinaryWithDefaultTags(precision lineprotocol.Precision, d
lastKey = tagKey

// N.B. Some customers have requested support for newline and tab chars in tag values (EAR 5476)
// Though this is outside the lineprotocol specification, it was supported in
// previous GO client versions.
if value, ok := p.Values.Tags[tagKey]; ok {
enc.AddTag(tagKey,
strings.ReplaceAll(
strings.ReplaceAll(value, "\t", "\\t"),
"\n", "\\n"))
enc.AddTag(tagKey, replacer(tagEscapes, value))
} else {
enc.AddTag(tagKey, strings.ReplaceAll(
strings.ReplaceAll(defaultTags[tagKey], "\t", "\\t"),
"\n", "\\n"))
enc.AddTag(tagKey, replacer(tagEscapes, defaultTags[tagKey]))
}
}

Expand Down
2 changes: 1 addition & 1 deletion influxdb3/point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestPointDefaultTags(t *testing.T) {
assert.EqualValues(t, `test,tag1=a,tag2=b,tag3=f float64=80.1234567 60000000070`+"\n", string(line))
}

func TestPointWithNewlineTags(t *testing.T) {
func TestPointWithEscapedTags(t *testing.T) {
p := NewPoint("test",
map[string]string{
"tag1": "new\nline and space",
Expand Down

0 comments on commit d0fc985

Please sign in to comment.