-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added option to optimize primitive schemas before exporting them to a…
… file (#19) * Added option to optimize primitive schemas before export * Revert adding option to optimizePrimitiveSchemas * Added PrimitiveSchemaOptimizer * Added PrimitiveSchemaOptimizer to SubSchemaMergeCommand * Allow string schemas * Updated README with new command * Fix CS * Enhance isPrimitive to support string schemas * Remove transformExportSchemaDefinition from interface * Remove space * Two spaces * Resolve discussions * Change primitive optimizer logic
- Loading branch information
Showing
9 changed files
with
174 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpKafka\PhpAvroSchemaGenerator\Optimizer; | ||
|
||
class PrimitiveSchemaOptimizer extends AbstractOptimizer implements OptimizerInterface | ||
{ | ||
/** | ||
* @param string $definition | ||
* @return string | ||
* @throws \JsonException | ||
*/ | ||
public function optimize(string $definition, bool $isPrimitive = false): string | ||
{ | ||
if (false === $isPrimitive) { | ||
return $definition; | ||
} | ||
|
||
$data = json_decode($definition, true, JSON_THROW_ON_ERROR); | ||
|
||
$data = $this->processSchema($data); | ||
|
||
return json_encode($data, JSON_THROW_ON_ERROR); | ||
} | ||
|
||
/** | ||
* @param mixed $data | ||
* @return mixed | ||
*/ | ||
private function processSchema($data) | ||
{ | ||
if (true === isset($data['type'])) { | ||
$data = $data['type']; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Unit\Optimizer; | ||
|
||
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\PrimitiveSchemaOptimizer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PrimitiveSchemaOptimizerTest extends TestCase | ||
{ | ||
public function testOptimize(): void | ||
{ | ||
$schema = '{"type": "string"}'; | ||
|
||
$expectedResult = json_encode(json_decode('"string"')); | ||
|
||
$optimizer = new PrimitiveSchemaOptimizer(); | ||
|
||
self::assertEquals($expectedResult, $optimizer->optimize($schema, true)); | ||
} | ||
|
||
public function testOptimizeForStringSchema(): void | ||
{ | ||
$schema = '"string"'; | ||
|
||
$expectedResult = json_encode(json_decode('"string"')); | ||
|
||
$optimizer = new PrimitiveSchemaOptimizer(); | ||
|
||
self::assertEquals($expectedResult, $optimizer->optimize($schema, true)); | ||
} | ||
|
||
public function testOptimizeForRecordSchema(): void | ||
{ | ||
$schema = '{"type":"record","namespace":"com.example","name":"Book","fields":[{"name":"isbn","type":"string"}]}'; | ||
|
||
$expectedResult = json_encode(json_decode($schema)); | ||
|
||
$optimizer = new PrimitiveSchemaOptimizer(); | ||
|
||
self::assertEquals($expectedResult, $optimizer->optimize($schema, false)); | ||
} | ||
} |
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