From d0fc98543edf7e7d6877b1f541b7144cf6b8169c Mon Sep 17 00:00:00 2001 From: karel rehor Date: Thu, 17 Oct 2024 11:03:07 +0200 Subject: [PATCH] chore: refactor escaping tag values --- influxdb3/point.go | 27 ++++++++++++++++++--------- influxdb3/point_test.go | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/influxdb3/point.go b/influxdb3/point.go index 991a900..e026e1d 100644 --- a/influxdb3/point.go +++ b/influxdb3/point.go @@ -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 { @@ -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])) } } diff --git a/influxdb3/point_test.go b/influxdb3/point_test.go index dee270c..c6dd183 100644 --- a/influxdb3/point_test.go +++ b/influxdb3/point_test.go @@ -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",