Skip to content
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

feat: #145 try to convert non-string-tags to strings #146

Merged
merged 3 commits into from
Jan 10, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 3.5.0 [unreleased]

### Bug Fixes
1. [#146](https://github.com/influxdata/influxdb-client-php/pull/146): Try to convert non-string-tags to strings, generate a warning if a value cannot be converted

## 3.4.0 [2023-07-28]

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"autoload-dev": {
"psr-4": {
"InfluxDB2\\": "test/InfluxDB2"
"InfluxDB2Test\\": "tests/"
}
},
"scripts": {
Expand Down
16 changes: 14 additions & 2 deletions src/InfluxDB2/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public static function fromArray($data): ?Point
/** Adds or replaces a tag value for a point.
*
* @param [Object] key the tag name
* @param [Object] value the tag value
* @param string|object|null $value the tag value, can be "object" with "__toString" function or "object" implements Stringable interface
* @return Point
*/
public function addTag($key, $value): Point
public function addTag($key, ?string $value): Point
{
$this->tags[$key] = $value;

Expand Down Expand Up @@ -162,6 +162,18 @@ private function appendTags()
foreach (array_keys($this->tags) as $key) {
$value = $this->tags[$key];

if (!is_string($value) && null !== $value) {
if (
is_scalar($value) ||
(is_object($value) && method_exists($value, '__toString')) ||
(\PHP_VERSION_ID >= 80000 && $value instanceof \Stringable)
) {
$value = (string) $value;
} else {
trigger_error(sprintf('Tag value for key %s cannot be converted to string', $key), E_USER_WARNING);
}
}

if ($this->isNullOrEmptyString($key) || $this->isNullOrEmptyString($value)) {
continue;
}
Expand Down
44 changes: 33 additions & 11 deletions tests/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,45 @@ public function testEmptyValue()

public function testTagEscapingKeyAndValue()
{
$point = Point::measurement("h\n2\ro\t_data")
->addTag("new\nline", "new\nline")
->addTag("carriage\rreturn", "carriage\nreturn")
->addTag("t\tab", "t\tab")
->addField("level", 2);
$point = Point::measurement("h\n2\ro\t_data")
->addTag("new\nline", "new\nline")
->addTag("carriage\rreturn", "carriage\nreturn")
->addTag("t\tab", "t\tab")
->addField("level", 2);

$this->assertEquals(
"h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i",
$point->toLineProtocol()
);
}

public function testStringableTag()
{
// this is just a random native class, implementing __toString()
$tag = new StringableClass();

$point = new Point("data", ['test' => $tag], ['value' => 1]);

$this->assertStringStartsWith(
"data,test=stringable",
$point->toLineProtocol()
);
}

public function testNonStringableTag()
{
$this->expectWarning();
$this->expectWarningMessage('Tag value for key test cannot be converted to string');
$point = new Point("data", ['test' => []]);

$point->toLineProtocol();
}

public function testEqualSignEscaping()
{
$point = Point::measurement("h=2o")
->addTag("l=ocation", "e=urope")
->addField("l=evel", 2);
$point = Point::measurement("h=2o")
->addTag("l=ocation", "e=urope")
->addField("l=evel", 2);

$this->assertEquals("h=2o,l\\=ocation=e\\=urope l\\=evel=2i", $point->toLineProtocol());
}
Expand Down Expand Up @@ -279,14 +301,14 @@ public function testFromArrayWithoutName()
$this->assertNull($pointArray);
}

public function testTagNotString()
public function testTagNonString()
{
$point = Point::measurement('h2o')
->addTag('location', 'europe')
->addTag('tag_not_string', [])
->addTag('tag_not_string', 4711)
->addTag('tag_not_null', null)
->addField('level', 2);

$this->assertEquals('h2o,location=europe level=2i', $point->toLineProtocol());
$this->assertEquals('h2o,location=europe,tag_not_string=4711 level=2i', $point->toLineProtocol());
}
}
11 changes: 11 additions & 0 deletions tests/StringableClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace InfluxDB2Test;

class StringableClass
{
public function __toString()
{
return "stringable";
}
}
Loading