AssignRenderer: improve/separate in-related code

fixes #844
This commit is contained in:
Thomas Gelf 2017-03-13 22:25:04 +01:00
parent 8b98113e83
commit da41e2f4db
3 changed files with 49 additions and 14 deletions

View File

@ -75,6 +75,15 @@ class AssignRenderer
}
}
protected function renderInArray($column, $expression)
{
return sprintf(
'%s in %s',
$column,
$expression
);
}
protected function renderContains(FilterExpression $filter)
{
return sprintf(
@ -91,14 +100,27 @@ class AssignRenderer
}
$column = $filter->getColumn();
$expression = $this->renderExpressionValue(json_decode($filter->getExpression()));
$rawExpression = json_decode($filter->getExpression());
$expression = $this->renderExpressionValue($rawExpression);
if (is_array($rawExpression) && $filter instanceof FilterMatch) {
return $this->renderInArray($column, $expression);
}
if ($filter instanceof FilterEqual) {
return sprintf(
'%s == %s',
$column,
$expression
);
if (is_array($rawExpression)) {
return sprintf(
'%s in %s',
$column,
$expression
);
} else {
return sprintf(
'%s == %s',
$column,
$expression
);
}
} elseif ($filter instanceof FilterMatch) {
if (strpos($expression, '*') === false) {
return $this->renderEquals($column, $expression);

View File

@ -137,9 +137,10 @@ class IcingaConfigHelper
return static::renderFloat($value);
// TODO:
// } elseif (is_object($value) || static::isAssocArray($value)) {
// return static::renderHash($value, $prefix);
// } elseif (is_array($value)) {
// return static::renderArray($value, $prefix);
// return static::renderHash($value, $prefix)
// TODO: also check array
} elseif (is_array($value)) {
return static::renderArray($value);
} elseif (is_string($value)) {
return static::renderString($value);
} else {

View File

@ -8,7 +8,7 @@ use Icinga\Module\Director\Test\BaseTestCase;
class AssignRendererTest extends BaseTestCase
{
public function testWhetherEqualMatchIsCorrectlyRendered()
public function testEqualMatchIsCorrectlyRendered()
{
$string = 'host.name="localhost"';
$expected = 'assign where host.name == "localhost"';
@ -18,7 +18,7 @@ class AssignRendererTest extends BaseTestCase
);
}
public function testWhetherWildcardsRenderAMatchMethod()
public function testWildcardsRenderAMatchMethod()
{
$string = 'host.address="127.0.0.*"';
$expected = 'assign where match("127.0.0.*", host.address)';
@ -28,7 +28,7 @@ class AssignRendererTest extends BaseTestCase
);
}
public function testWhetherACombinedFilterRendersCorrectly()
public function testACombinedFilterRendersCorrectly()
{
$string = 'host.name="*internal"|(service.vars.priority<2'
. '&host.vars.is_clustered=true)';
@ -42,7 +42,7 @@ class AssignRendererTest extends BaseTestCase
);
}
public function testWhetherSlashesAreNotEscaped()
public function testSlashesAreNotEscaped()
{
$string = 'host.name=' . json_encode('a/b');
@ -54,7 +54,7 @@ class AssignRendererTest extends BaseTestCase
);
}
public function testWhetherFakeContainsOperatorRendersCorrectly()
public function testFakeContainsOperatorRendersCorrectly()
{
$string = json_encode('member') . '=host.groups';
@ -75,6 +75,18 @@ class AssignRendererTest extends BaseTestCase
);
}
public function testInArrayIsRenderedCorrectly()
{
$string = 'host.name=' . json_encode(array('a' ,'b'));
$expected = 'assign where host.name in [ "a", "b" ]';
$this->assertEquals(
$expected,
$this->renderer($string)->renderAssign()
);
}
protected function renderer($string)
{
return AssignRenderer::forFilter(Filter::fromQueryString($string));