Skip to content

fix: serialization of \n, \r and \t to Line Protocol, = is valid sign for measurement name #106

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

Merged
merged 2 commits into from
Jun 29, 2020
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## 1.10.0 [unreleased]

### Features
1. [#23](https://github.com/influxdata/influxdb-client-csharp/pull/102): Added WriteApiAsync for asynchronous write without batching
1. [#102](https://github.com/influxdata/influxdb-client-csharp/pull/102): Added WriteApiAsync for asynchronous write without batching

### Bug Fixes
1. [#106](https://github.com/influxdata/influxdb-client-csharp/pull/106): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol, `=` is valid sign for measurement name

## 1.9.0 [2020-06-19]

Expand Down
24 changes: 23 additions & 1 deletion Client.Test/PointDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ public void TagEmptyTagValue()
Assert.AreEqual("h2o,location=europe level=2i", point.ToLineProtocol());
}

[Test]
public void TagEscapingKeyAndValue() {

var point = PointData.Measurement("h\n2\ro\t_data")
.Tag("new\nline", "new\nline")
.Tag("carriage\rreturn", "carriage\rreturn")
.Tag("t\tab", "t\tab")
.Field("level", 2);

Assert.AreEqual("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\rreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i", point.ToLineProtocol());
}

[Test]
public void EqualSignEscaping() {

var point = PointData.Measurement("h=2o")
.Tag("l=ocation", "e=urope")
.Field("l=evel", 2);

Assert.AreEqual("h=2o,l\\=ocation=e\\=urope l\\=evel=2i", point.ToLineProtocol());
}

[Test]
public void Immutability()
{
Expand Down Expand Up @@ -60,7 +82,7 @@ public void MeasurementEscape()
.Tag("", "warn")
.Field("level", 2);

Assert.AreEqual("h2\\=o,location=europe level=2i", point.ToLineProtocol());
Assert.AreEqual("h2=o,location=europe level=2i", point.ToLineProtocol());

point = PointData.Measurement("h2,o")
.Tag("location", "europe")
Expand Down
21 changes: 18 additions & 3 deletions Client/Writes/PointData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public string ToLineProtocol(PointSettings pointSettings = null)
{
var sb = new StringBuilder();

EscapeKey(sb, _measurementName);
EscapeKey(sb, _measurementName, false);
AppendTags(sb, pointSettings);
var appendedFields = AppendFields(sb);
if (!appendedFields)
Expand Down Expand Up @@ -469,17 +469,32 @@ private void AppendTime(StringBuilder sb)
/// </summary>
/// <param name="sb">The sb.</param>
/// <param name="key">The key.</param>
private void EscapeKey(StringBuilder sb, string key)
/// <param name="escapeEqual">Configure to escaping equal.</param>
private void EscapeKey(StringBuilder sb, string key, bool escapeEqual = true)
{
foreach (var c in key)
{
switch (c)
{
case '\n':
sb.Append("\\n");
continue;
case '\r':
sb.Append("\\r");
continue;
case '\t':
sb.Append("\\t");
continue;
case ' ':
case ',':
case '=':
sb.Append("\\");
break;
case '=':
if (escapeEqual)
{
sb.Append("\\");
}
break;
}

sb.Append(c);
Expand Down