Skip to content

Commit 74b4523

Browse files
bednarrolincova
authored andcommitted
fix: serialization of \n, \r and \t to Line Protocol, = is valid sign for measurement name (#115)
* fix: serialization of `\n`, `\r` and `\t` to Line Protocol * fix: `=` is valid sign for measurement name
1 parent 3de98e0 commit 74b4523

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
### Bug Fixes
99
1. [#117](https://github.com/influxdata/influxdb-client-python/pull/117): Fixed appending default tags for single Point
10+
1. [#115](https://github.com/influxdata/influxdb-client-python/pull/115): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol, `=` is valid sign for measurement name
1011
1. [#118](https://github.com/influxdata/influxdb-client-python/issues/118): Fixed serialization of DataFrame with empty (NaN) values
1112

1213
## 1.8.0 [2020-06-19]

influxdb_client/client/write/point.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
EPOCH = UTC.localize(datetime.utcfromtimestamp(0))
1414
DEFAULT_WRITE_PRECISION = WritePrecision.NS
15-
_ESCAPE_KEY = str.maketrans({'\\': '\\\\', ',': r'\,', ' ': r'\ ', '=': r'\=', '\n': ''})
15+
_ESCAPE_MEASUREMENT = str.maketrans({'\\': '\\\\', ',': r'\,', ' ': r'\ ', '\n': '\\n', '\t': '\\t', '\r': '\\r'})
16+
_ESCAPE_KEY = str.maketrans({'\\': '\\\\', ',': r'\,', ' ': r'\ ', '=': r'\=', '\n': '\\n', '\t': '\\t', '\r': '\\r'})
1617
_ESCAPE_STRING = str.maketrans({'\"': r"\"", "\\": r"\\"})
1718

1819

@@ -76,7 +77,7 @@ def field(self, field, value):
7677
return self
7778

7879
def to_line_protocol(self):
79-
_measurement = _escape_key(self._name)
80+
_measurement = _escape_key(self._name, _ESCAPE_MEASUREMENT)
8081
_tags = _append_tags(self._tags)
8182
_fields = _append_fields(self._fields)
8283
if not _fields:
@@ -134,8 +135,10 @@ def _append_time(time, write_precision):
134135
return f" {int(_convert_timestamp(time, write_precision))}"
135136

136137

137-
def _escape_key(tag):
138-
return str(tag).translate(_ESCAPE_KEY)
138+
def _escape_key(tag, escape_list=None):
139+
if escape_list is None:
140+
escape_list = _ESCAPE_KEY
141+
return str(tag).translate(escape_list)
139142

140143

141144
def _escape_tag_value(value):

tests/test_point.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ def test_TagEmptyValue(self):
3636

3737
self.assertEqual("h2o,location=europe level=2i", point.to_line_protocol())
3838

39+
def test_TagEscapingKeyAndValue(self):
40+
41+
point = Point.measurement("h\n2\ro\t_data") \
42+
.tag("new\nline", "new\nline") \
43+
.tag("carriage\rreturn", "carriage\nreturn") \
44+
.tag("t\tab", "t\tab") \
45+
.field("level", 2)
46+
47+
self.assertEqual("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i", point.to_line_protocol())
48+
49+
def test_EqualSignEscaping(self):
50+
51+
point = Point.measurement("h=2o") \
52+
.tag("l=ocation", "e=urope") \
53+
.field("l=evel", 2)
54+
55+
self.assertEqual("h=2o,l\\=ocation=e\\=urope l\\=evel=2i", point.to_line_protocol())
56+
3957
def test_OverrideTagField(self):
4058
point = Point.measurement("h2o") \
4159
.tag("location", "europe") \

0 commit comments

Comments
 (0)