Skip to content

Commit

Permalink
fix: Arrays of null where data is expected now parses properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondrej Vana committed Oct 10, 2015
1 parent 1c9fdf8 commit d0d98f0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/Keboola/Json/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ protected function analyzeRow($row, $type)
$rowType = $this->getType($row);

// If the row is scalar, make it a {"data" => $value} object
if (is_scalar($row) || is_null($row)) {
if (is_scalar($row)) {
$struct[Parser::DATA_COLUMN] = $this->getType($row);
$row = (object) [Parser::DATA_COLUMN => $row];
} elseif (is_object($row)) {
// process each property of the object
foreach($row as $key => $field) {
Expand All @@ -125,7 +124,6 @@ protected function analyzeRow($row, $type)
$fieldType = 'NULL';
}
}

$struct[$key] = $fieldType;
}
} elseif ($this->nestedArrayAsJson && is_array($row)) {
Expand All @@ -134,7 +132,8 @@ protected function analyzeRow($row, $type)
['row' => $row]
);
$rowType = $struct[Parser::DATA_COLUMN] = 'string';
$row = (object) [Parser::DATA_COLUMN => json_encode($row)];
} elseif (is_null($row)) {
// do nothing
} else {
throw new JsonParserException("Unsupported data row in '{$type}'!", ['row' => $row]);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Keboola/Json/Struct.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function load(array $struct = [])
*/
public function add($type, array $struct)
{
// TODO $this->struct[$type] should NEVER be 'NULL' - remove in the future
if (empty($this->struct[$type]) || $this->struct[$type] == "NULL") {
// If we don't know the type yet
$this->struct[$type] = $struct;
Expand Down Expand Up @@ -168,6 +169,8 @@ protected function upgradeToArrayCheck($oldType, $newType)
&& (
($this->isArrayOf($oldType) == $newType)
|| $newType == 'arrayOf' . $oldType
|| $this->isArrayOf($oldType) == 'NULL'
|| $this->isArrayOf($newType) == 'NULL'
);
}

Expand All @@ -179,6 +182,10 @@ protected function upgradeToArrayCheck($oldType, $newType)
protected function upgradeToArray($oldType, $newType)
{
if ($this->isArrayOf($oldType)) {
if ($this->isArrayOf($oldType) == 'NULL') {
return $this->isArrayOf($newType) ? $newType : 'arrayOf' . $newType;
}

return $oldType;
} elseif ($oldType == 'array') {
return 'arrayOf' . $newType;
Expand Down
60 changes: 60 additions & 0 deletions tests/Keboola/Json/AnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,64 @@ public function testAnalyzeEmptyArrayOfObjectAutoUpgrade()
$analyzer->getStruct()->getStruct()
);
}

public function testArrayOfNull()
{
$analyzer = new Analyzer($this->getLogger('analyzer', true));
$analyzer->getStruct()->setAutoUpgradeToArray(true);

$analyzer->analyze(
[
(object) [
'val' => ['stringArr'],
'obj' => [(object) ['key' => 'objValue']]
],
(object) [
'val' => [null],
'obj' => [null]
]
],
's2null'
);

$analyzer->analyze(
[
(object) [
'val' => ['stringArr'],
'obj' => [(object) ['key' => 'objValue']]
],
(object) [
'val' => [null],
'obj' => [null]
]
],
'null2s'
);

$this->assertEquals(
[
's2null' => [
'val' => 'arrayOfscalar',
'obj' => 'arrayOfobject'
],
's2null.val' => [
'data' => 'scalar'
],
'null2s' => [
'val' => 'arrayOfscalar',
'obj' => 'arrayOfobject'
],
'null2s.val' => [
'data' => 'scalar'
],
's2null.obj' => [
'key' => 'scalar'
],
'null2s.obj' => [
'key' => 'scalar'
]
],
$analyzer->getStruct()->getStruct()
);
}
}
77 changes: 77 additions & 0 deletions tests/Keboola/Json/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,81 @@ public function testEmptyData()

$parser->process([]);
}

public function testArrayOfNull()
{
$parser = $this->getParser();
$parser->getStruct()->setAutoUpgradeToArray(true);

$parser->process(
[
(object) [
'val' => ['stringArr'],
'obj' => [(object) ['key' => 'objValue']]
],
(object) [
'val' => [null],
'obj' => [null]
]
],
's2null'
);

$parser->process(
[
(object) [
'val' => ['stringArr'],
'obj' => [(object) ['key' => 'objValue']]
],
(object) [
'val' => [null],
'obj' => [null]
]
],
'null2s'
);

self::assertEquals(
'"val","obj"' . PHP_EOL .
'"s2null_eb89917794221aeda822735efbab9069","s2null_eb89917794221aeda822735efbab9069"' . PHP_EOL .
'"s2null_77cca534224f13ec1fa45c6c0c98557d","s2null_77cca534224f13ec1fa45c6c0c98557d"' . PHP_EOL .
'',
file_get_contents($parser->getCsvFiles()['s2null'])
);

self::assertEquals('"data","JSON_parentId"' . PHP_EOL .
'"stringArr","s2null_eb89917794221aeda822735efbab9069"' . PHP_EOL .
'"","s2null_77cca534224f13ec1fa45c6c0c98557d"' . PHP_EOL .
'',
file_get_contents($parser->getCsvFiles()['s2null_val'])
);

self::assertEquals('"key","JSON_parentId"' . PHP_EOL .
'"objValue","s2null_eb89917794221aeda822735efbab9069"' . PHP_EOL .
'"","s2null_77cca534224f13ec1fa45c6c0c98557d"' . PHP_EOL .
'',
file_get_contents($parser->getCsvFiles()['s2null_obj'])
);

self::assertEquals('"val","obj"' . PHP_EOL .
'"null2s_eb89917794221aeda822735efbab9069","null2s_eb89917794221aeda822735efbab9069"' . PHP_EOL .
'"null2s_77cca534224f13ec1fa45c6c0c98557d","null2s_77cca534224f13ec1fa45c6c0c98557d"' . PHP_EOL .
'',
file_get_contents($parser->getCsvFiles()['null2s'])
);

self::assertEquals('"data","JSON_parentId"' . PHP_EOL .
'"stringArr","null2s_eb89917794221aeda822735efbab9069"' . PHP_EOL .
'"","null2s_77cca534224f13ec1fa45c6c0c98557d"' . PHP_EOL .
'',
file_get_contents($parser->getCsvFiles()['null2s_val'])
);

self::assertEquals('"key","JSON_parentId"' . PHP_EOL .
'"objValue","null2s_eb89917794221aeda822735efbab9069"' . PHP_EOL .
'"","null2s_77cca534224f13ec1fa45c6c0c98557d"' . PHP_EOL .
'',
file_get_contents($parser->getCsvFiles()['null2s_obj'])
);
}
}

0 comments on commit d0d98f0

Please sign in to comment.