Skip to content

Commit

Permalink
Merge pull request #74 from Zwartpet/patch/line-protocol
Browse files Browse the repository at this point in the history
Implemented comma, equal sign and space escaping
  • Loading branch information
wdalmut authored Apr 15, 2017
2 parents ee65b9a + e90e2bd commit 7ea56e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/Adapter/WriterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ protected function tagsToString(array $tags)
{
$tagLine = "";
if (count($tags) > 0) {
array_walk($tags, function(&$value, $key) {
$value = "{$key}={$value}";
array_walk($tags, function (&$value, $key) {
$value = "{$this->addSlashes($key)}={$this->addSlashes($value)}";
});
$tagLine = sprintf(",%s", implode(",", $tags));
}
Expand All @@ -63,15 +63,15 @@ protected function tagsToString(array $tags)

protected function pointsToString(array $elements)
{
array_walk($elements, function(&$value, $key) {
array_walk($elements, function (&$value, $key) {
$dataType = gettype($value);
if (!in_array($dataType, ["string", "double", "boolean", "integer"])) {
$dataType = "serializable";
}
$dataType = ucfirst($dataType);
if ($dataType!='Null') {
$value = call_user_func([$this, "convert{$dataType}"], $value);
$value = "{$key}={$value}";
$value = "{$this->addSlashes($key)}={$value}";
}
});
$elements = array_filter($elements);
Expand Down Expand Up @@ -102,4 +102,23 @@ protected function convertBoolean($value)
{
return (($value) ? "true" : "false");
}

/**
* Returns strings with space, comma, or equals sign characters backslashed per Influx write protocol syntax
*
* @param string $value
* @return string
*/
private function addSlashes($value)
{
return str_replace([
' ',
',',
'='
], [
'\ ',
'\,',
'\='
], $value);
}
}
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ public function query($query)
{
return $this->getReader()->query($query);
}
}
}
2 changes: 2 additions & 0 deletions tests/unit/Adapter/WriterTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function getElements()
{
return [
[["one" => "two"], "one=\"two\""],
[["one with space" => "two"], "one\\ with\\ space=\"two\""],
[["one with,comma" => "two"], "one\\ with\\,comma=\"two\""],
[["one" => "two", "three" => "four"], "one=\"two\",three=\"four\""],
[["one" => true, "three" => false], "one=true,three=false"],
[["one" => true, "three" => 0., "four" => 1.], "one=true,three=0,four=1"],
Expand Down

0 comments on commit 7ea56e1

Please sign in to comment.