Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: escape tabs in tag strings #111

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.13.0 [unreleased]

1. [#111](https://github.com/InfluxCommunity/influxdb3-go/pull/111): Support tabs in tag values.

### Features

1. [#108](https://github.com/InfluxCommunity/influxdb3-go/pull/108): Allow Request.GetBody to be set when writing gzipped data to make calls more resilient.
Expand Down
4 changes: 4 additions & 0 deletions influxdb3/client_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ func TestEscapedStringValues(t *testing.T) {
map[string]string{
"tag1": "new\nline and space",
"tag2": "escaped\\nline and space",
"tag3": "escaped\nline and\ttab",
"tag4": "preescaped\\nline and\\ttab",
},
map[string]interface{}{
"fVal": 41.3,
Expand All @@ -359,5 +361,7 @@ func TestEscapedStringValues(t *testing.T) {
assert.EqualValues(t, "greetings\\nearthlings", qit.Value()["sVal"])
assert.EqualValues(t, "new\\nline and space", qit.Value()["tag1"])
assert.EqualValues(t, "escaped\\nline and space", qit.Value()["tag2"])
assert.EqualValues(t, "escaped\\nline and\\ttab", qit.Value()["tag3"])
assert.EqualValues(t, "preescaped\\nline and\\ttab", qit.Value()["tag4"])
}
}
16 changes: 11 additions & 5 deletions influxdb3/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ 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.
replacer := strings.NewReplacer(
"\n", "\\n",
"\t", "\\t",
)

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

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

Expand Down
14 changes: 8 additions & 6 deletions influxdb3/point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ 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",
"tag2": "escaped\\nline and space",
"ambiTag": "ambiguous\ntag",
"tabTag1": "drink\tTab",
"tabTag2": "Tab\\tulator",
},
map[string]interface{}{
"fVal": 41.3,
Expand All @@ -198,16 +200,16 @@ func TestPointWithNewlineTags(t *testing.T) {
line, err := p.MarshalBinary(lineprotocol.Nanosecond)
require.NoError(t, err)
assert.EqualValues(t,
"test,ambiTag=ambiguous\\ntag,tag1=new\\nline\\ and\\ space,tag2=escaped\\nline\\ and\\ space "+
"fVal=41.3 60000000070\n",
"test,ambiTag=ambiguous\\ntag,tabTag1=drink\\tTab,tabTag2=Tab\\tulator,"+
"tag1=new\\nline\\ and\\ space,tag2=escaped\\nline\\ and\\ space fVal=41.3 60000000070\n",
string(line))

line, err = p.MarshalBinaryWithDefaultTags(lineprotocol.Nanosecond, defaultTags)
require.NoError(t, err)
assert.EqualValues(t,
"test,ambiTag=ambiguous\\ntag,defTag1=default\\nline\\ and\\ space,defTag2=escaped"+
"\\ndefault\\ line\\ and\\ space,tag1=new\\nline\\ and\\ space,tag2=escaped\\nline\\ and\\ space "+
"fVal=41.3 60000000070\n",
"test,ambiTag=ambiguous\\ntag,defTag1=default\\nline\\ and\\ space,"+
"defTag2=escaped\\ndefault\\ line\\ and\\ space,tabTag1=drink\\tTab,tabTag2=Tab\\tulator,"+
"tag1=new\\nline\\ and\\ space,tag2=escaped\\nline\\ and\\ space fVal=41.3 60000000070\n",
string(line))

pInvalid := NewPoint("test", map[string]string{
Expand Down