This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Streaming Serializer [WIP][Concept] #90
Open
IljaN
wants to merge
5
commits into
feature/next
Choose a base branch
from
feature/streaming_serializer
base: feature/next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4733b49
Enable drone on feature branches
micbar 402b54a
Merge pull request #86 from owncloud/fix/drone
micbar 17b96ee
Remove wildcard on feature branch CI
micbar 54a853c
Merge pull request #87 from owncloud/fix/drone-on-branch
micbar ad51bb2
Serializer with streaming api [WIP]
IljaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace OCA\DataExporter\Io; | ||
|
||
use Symfony\Component\Serializer\Encoder\EncoderInterface; | ||
use Symfony\Component\Serializer\Encoder\JsonEncode; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
|
||
class JsonLinesEncoder implements EncoderInterface { | ||
const FORMAT = 'jsonl'; | ||
|
||
/** @var JsonEncode */ | ||
private $jsonEncoder; | ||
|
||
public function __construct() { | ||
//single jsonl lines are valid json | ||
$this->jsonEncoder = new JsonEncode(); | ||
} | ||
|
||
/** | ||
* Encodes data into the given format. | ||
* | ||
* @param mixed $data Data to encode | ||
* @param string $format Format name | ||
* @param array $context Options that normalizers/encoders have access to | ||
* | ||
* @return string|int|float|bool | ||
* | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function encode($data, $format, array $context = []) { | ||
$typeHint = $this->getEncodingTypeHint($context); | ||
|
||
if (!\in_array($typeHint, ['object', 'array'])) { | ||
throw new \InvalidArgumentException('Only objects and arrays supported for jsonl encoding'); | ||
} | ||
|
||
if ($typeHint === 'object') { | ||
return $this->jsonEncoder->encode($data, 'json') . PHP_EOL; | ||
} | ||
|
||
$jsonLines = ''; | ||
|
||
if ($typeHint === 'array' && \count($data) > 0) { | ||
foreach ($data as $line) { | ||
$jsonLines .= $this->jsonEncoder->encode($line, 'json') . PHP_EOL; | ||
} | ||
} | ||
|
||
return $jsonLines; | ||
} | ||
|
||
private function getEncodingTypeHint($context) { | ||
if (!isset($context[self::class]['type_hint'])) { | ||
throw new \InvalidArgumentException('Missing typehint for jsonl encoder'); | ||
} | ||
|
||
return $context[self::class]['type_hint']; | ||
} | ||
|
||
/** | ||
* Checks whether the serializer can encode to given format. | ||
* | ||
* @param string $format Format name | ||
* | ||
* @return bool | ||
*/ | ||
public function supportsEncoding($format) { | ||
return $format === self::FORMAT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace OCA\DataExporter\Tests\Acceptance\SerializerTest; | ||
|
||
use OCA\DataExporter\Io\Serializer; | ||
use OCA\DataExporter\Model\File; | ||
use Test\TestCase; | ||
|
||
class SerializerTest extends TestCase { | ||
const TEST_JSONL = <<< JSONL | ||
{"type":"file","path":"\/foo\/bar.txt","eTag":"12413rr","permissions":19} | ||
{"type":"folder","path":"\/pics","eTag":"43t3t3g3g","permissions":20} | ||
|
||
JSONL; | ||
|
||
/** @var Serializer */ | ||
private $ser; | ||
private $testId; | ||
|
||
private $testObjects; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
$this->testId = \bin2hex(\random_bytes(4)); | ||
$this->ser = new Serializer(); | ||
|
||
$this->testObjects = [ | ||
(new File()) | ||
->setPermissions(19) | ||
->setETag('12413rr') | ||
->setType(File::TYPE_FILE) | ||
->setPath('/foo/bar.txt'), | ||
(new File()) | ||
->setPermissions(20) | ||
->setETag('43t3t3g3g') | ||
->setType(File::TYPE_FOLDER) | ||
->setPath('/pics'), | ||
]; | ||
} | ||
|
||
public function testSerialize() { | ||
$stream = \fopen('php://memory', 'rb+'); | ||
|
||
// Serialize single objects | ||
foreach ($this->testObjects as $f) { | ||
$this->ser->serializeToStream($f, $stream); | ||
} | ||
|
||
\rewind($stream); | ||
|
||
$actual = \stream_get_contents($stream); | ||
$this->assertEquals(self::TEST_JSONL, $actual); | ||
|
||
\fclose($stream); | ||
} | ||
|
||
public function testDeserialization() { | ||
$stream = \fopen('php://memory', 'rb+'); | ||
\fwrite($stream, self::TEST_JSONL); | ||
\rewind($stream); | ||
|
||
/** @var File[] $expected */ | ||
$expected = $this->testObjects; | ||
/** @var File[] $actual */ | ||
$actual = $this->ser->deserializeStream($stream, File::class); | ||
|
||
foreach ($actual as $key => $obj) { | ||
$this->assertEquals($expected[$key]->getETag(), $obj->getETag()); | ||
$this->assertEquals($expected[$key]->getPath(), $obj->getPath()); | ||
$this->assertEquals($expected[$key]->getType(), $obj->getType()); | ||
$this->assertEquals($expected[$key]->getPermissions(), $obj->getPermissions()); | ||
} | ||
|
||
\fclose($stream); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method might not be needed as streams should be lazy by default