Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 26683a4

Browse filesBrowse files
committed
Handle None tags and fields
1 parent b65e4a4 commit 26683a4
Copy full SHA for 26683a4

File tree

2 files changed

+35
-7
lines changed
Filter options

2 files changed

+35
-7
lines changed

‎influxdb/line_protocol.py

Copy file name to clipboardExpand all lines: influxdb/line_protocol.py
+16-7Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ def _escape_tag(tag):
4747
def _escape_value(value):
4848
value = _get_unicode(value)
4949
if isinstance(value, text_type) and value != '':
50-
return "\"{}\"".format(value.replace(
51-
"\"", "\\\""
52-
))
50+
return "\"{}\"".format(
51+
value.replace(
52+
"\"", "\\\""
53+
).replace(
54+
"\n", "\\n"
55+
)
56+
)
5357
else:
5458
return str(value)
5559

@@ -60,6 +64,8 @@ def _get_unicode(data, force=False):
6064
"""
6165
if isinstance(data, binary_type):
6266
return data.decode('utf-8')
67+
elif data is None:
68+
return ''
6369
elif force:
6470
return str(data)
6571
else:
@@ -102,10 +108,13 @@ def make_lines(data, precision=None):
102108
# add fields
103109
field_values = []
104110
for field_key in sorted(point['fields'].keys()):
105-
field_values.append("{key}={value}".format(
106-
key=_escape_tag(field_key),
107-
value=_escape_value(point['fields'][field_key]),
108-
))
111+
key = _escape_tag(field_key)
112+
value = _escape_value(point['fields'][field_key])
113+
if key != '' and value != '':
114+
field_values.append("{key}={value}".format(
115+
key=key,
116+
value=value
117+
))
109118
field_values = ','.join(field_values)
110119
elements.append(field_values)
111120

‎influxdb/tests/test_line_protocol.py

Copy file name to clipboardExpand all lines: influxdb/tests/test_line_protocol.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def test_make_lines(self):
1010
data = {
1111
"tags": {
1212
"empty_tag": "",
13+
"none_tag": None,
1314
"integer_tag": 2,
1415
"string_tag": "hello"
1516
},
@@ -19,6 +20,7 @@ def test_make_lines(self):
1920
"fields": {
2021
"string_val": "hello!",
2122
"int_val": 1,
23+
"none_field": None,
2224
}
2325
}
2426
]
@@ -29,3 +31,20 @@ def test_make_lines(self):
2931
'test,integer_tag=2,string_tag=hello '
3032
'int_val=1,string_val="hello!"\n'
3133
)
34+
35+
def test_string_val_newline(self):
36+
data = {
37+
"points": [
38+
{
39+
"measurement": "m1",
40+
"fields": {
41+
"multi_line": "line1\nline1\nline3"
42+
}
43+
}
44+
]
45+
}
46+
47+
self.assertEqual(
48+
line_protocol.make_lines(data),
49+
'm1 multi_line="line1\\nline1\\nline3"\n'
50+
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.