Skip to content

Commit 70eafdf

Browse files
authored
fix: serialization of \n, \r and \t to Line Protocol, = is valid sign for measurement name (#106)
1 parent d5c0e54 commit 70eafdf

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
## 1.10.0 [unreleased]
22

33
### Features
4-
1. [#23](https://github.com/influxdata/influxdb-client-csharp/pull/102): Added WriteApiAsync for asynchronous write without batching
4+
1. [#102](https://github.com/influxdata/influxdb-client-csharp/pull/102): Added WriteApiAsync for asynchronous write without batching
5+
6+
### Bug Fixes
7+
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
58

69
## 1.9.0 [2020-06-19]
710

Client.Test/PointDataTest.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ public void TagEmptyTagValue()
2121
Assert.AreEqual("h2o,location=europe level=2i", point.ToLineProtocol());
2222
}
2323

24+
[Test]
25+
public void TagEscapingKeyAndValue() {
26+
27+
var point = PointData.Measurement("h\n2\ro\t_data")
28+
.Tag("new\nline", "new\nline")
29+
.Tag("carriage\rreturn", "carriage\rreturn")
30+
.Tag("t\tab", "t\tab")
31+
.Field("level", 2);
32+
33+
Assert.AreEqual("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\rreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i", point.ToLineProtocol());
34+
}
35+
36+
[Test]
37+
public void EqualSignEscaping() {
38+
39+
var point = PointData.Measurement("h=2o")
40+
.Tag("l=ocation", "e=urope")
41+
.Field("l=evel", 2);
42+
43+
Assert.AreEqual("h=2o,l\\=ocation=e\\=urope l\\=evel=2i", point.ToLineProtocol());
44+
}
45+
2446
[Test]
2547
public void Immutability()
2648
{
@@ -60,7 +82,7 @@ public void MeasurementEscape()
6082
.Tag("", "warn")
6183
.Field("level", 2);
6284

63-
Assert.AreEqual("h2\\=o,location=europe level=2i", point.ToLineProtocol());
85+
Assert.AreEqual("h2=o,location=europe level=2i", point.ToLineProtocol());
6486

6587
point = PointData.Measurement("h2,o")
6688
.Tag("location", "europe")

Client/Writes/PointData.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public string ToLineProtocol(PointSettings pointSettings = null)
298298
{
299299
var sb = new StringBuilder();
300300

301-
EscapeKey(sb, _measurementName);
301+
EscapeKey(sb, _measurementName, false);
302302
AppendTags(sb, pointSettings);
303303
var appendedFields = AppendFields(sb);
304304
if (!appendedFields)
@@ -469,17 +469,32 @@ private void AppendTime(StringBuilder sb)
469469
/// </summary>
470470
/// <param name="sb">The sb.</param>
471471
/// <param name="key">The key.</param>
472-
private void EscapeKey(StringBuilder sb, string key)
472+
/// <param name="escapeEqual">Configure to escaping equal.</param>
473+
private void EscapeKey(StringBuilder sb, string key, bool escapeEqual = true)
473474
{
474475
foreach (var c in key)
475476
{
476477
switch (c)
477478
{
479+
case '\n':
480+
sb.Append("\\n");
481+
continue;
482+
case '\r':
483+
sb.Append("\\r");
484+
continue;
485+
case '\t':
486+
sb.Append("\\t");
487+
continue;
478488
case ' ':
479489
case ',':
480-
case '=':
481490
sb.Append("\\");
482491
break;
492+
case '=':
493+
if (escapeEqual)
494+
{
495+
sb.Append("\\");
496+
}
497+
break;
483498
}
484499

485500
sb.Append(c);

0 commit comments

Comments
 (0)