From d55792964b5b79f21c22fdb445941a48f9296f3a Mon Sep 17 00:00:00 2001 From: saranshdhingra Date: Fri, 19 Apr 2024 16:00:24 +0530 Subject: [PATCH 1/3] Added a customEncoder in the Serializer --- src/Serializer.php | 17 ++++++++++- tests/Tests/Unit/SerializerTest.php | 46 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/Serializer.php b/src/Serializer.php index 49f4031c4..3a487a6fe 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -69,6 +69,11 @@ class Serializer private $messageTypeTransformers; private $decodeFieldTransformers; private $decodeMessageTypeTransformers; + // Array of key-value pairs which specify a custom encoding function. + // The key is the proto class and the value is the function + // which will be used to convert the proto instead of the + // encodeMessage method from the Serializer class. + private $customEncoders; private $descriptorMaps = []; @@ -84,12 +89,14 @@ public function __construct( $fieldTransformers = [], $messageTypeTransformers = [], $decodeFieldTransformers = [], - $decodeMessageTypeTransformers = [] + $decodeMessageTypeTransformers = [], + $customEncoders = [], ) { $this->fieldTransformers = $fieldTransformers; $this->messageTypeTransformers = $messageTypeTransformers; $this->decodeFieldTransformers = $decodeFieldTransformers; $this->decodeMessageTypeTransformers = $decodeMessageTypeTransformers; + $this->customEncoders = $customEncoders; } /** @@ -101,6 +108,14 @@ public function __construct( */ public function encodeMessage($message) { + $cls = get_class($message); + + // If we have supplied a customEncoder for this class type, + // then we use that instead of the general encodeMessage definition. + if(array_key_exists($cls, $this->customEncoders)) { + $func = $this->customEncoders[$cls]; + return call_user_func($func, $message); + } // Get message descriptor $pool = DescriptorPool::getGeneratedPool(); $messageType = $pool->getDescriptorByClassName(get_class($message)); diff --git a/tests/Tests/Unit/SerializerTest.php b/tests/Tests/Unit/SerializerTest.php index 393305327..080b4b0d0 100644 --- a/tests/Tests/Unit/SerializerTest.php +++ b/tests/Tests/Unit/SerializerTest.php @@ -291,4 +291,50 @@ public function testSpecialEncodingDecodingByFieldType() $this->assertEqualsWithDelta($data['blue'], $array['blue'], 0.0000001); $this->assertEqualsWithDelta($data['alpha']['value'], $array['alpha']['value'], 0.0000001); } + + /** + * @dataProvider customEncoderProvider + */ + public function testCustomEncoderForEncodeMessage($customEncoder, $protoObj, $expectedData) + { + $serializer = new Serializer([], [], [], [], $customEncoder); + + $data = $serializer->encodeMessage($protoObj); + array_walk($expectedData, function ($val, $key, $expectedData) { + $this->assertEquals($val, $expectedData[$key]); + }, $expectedData); + } + + public function customEncoderProvider() + { + $original = [ + 'red' => 50.0, + 'blue' => 50.0 + ]; + + $modified = [ + 'red' => 100, + 'blue' => 100 + ]; + + return [ + [ + [ + Color::class => function ($message) use($modified) { + return $modified; + } + ], + new Color($original), + $modified + ], + [ + // When no custom encoder is supplied, the encodeMessage will return the data + // that is passed into the proto + [ + ], + new Color($original), + $original + ] + ]; + } } From 0d7adc2fd96caf2a3fe4ac6483de7d53977940f9 Mon Sep 17 00:00:00 2001 From: saranshdhingra Date: Fri, 19 Apr 2024 17:12:16 +0530 Subject: [PATCH 2/3] Lint fixes --- src/Serializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serializer.php b/src/Serializer.php index 3a487a6fe..1c39ced47 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -112,7 +112,7 @@ public function encodeMessage($message) // If we have supplied a customEncoder for this class type, // then we use that instead of the general encodeMessage definition. - if(array_key_exists($cls, $this->customEncoders)) { + if (array_key_exists($cls, $this->customEncoders)) { $func = $this->customEncoders[$cls]; return call_user_func($func, $message); } From b800b727916eb9b7160f13f4f7e29a93bb8249a6 Mon Sep 17 00:00:00 2001 From: saranshdhingra Date: Wed, 24 Apr 2024 18:55:58 +0530 Subject: [PATCH 3/3] Resolved PR comments --- tests/Tests/Unit/SerializerTest.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/Tests/Unit/SerializerTest.php b/tests/Tests/Unit/SerializerTest.php index 080b4b0d0..386f7b254 100644 --- a/tests/Tests/Unit/SerializerTest.php +++ b/tests/Tests/Unit/SerializerTest.php @@ -312,20 +312,23 @@ public function customEncoderProvider() 'blue' => 50.0 ]; - $modified = [ - 'red' => 100, - 'blue' => 100 + $expected = [ + 'red' => 100.0, + 'blue' => 100.0 ]; return [ [ [ - Color::class => function ($message) use($modified) { - return $modified; + Color::class => function ($message) { + return [ + 'red' => $message->getRed() * 2, + 'blue' => $message->getBlue() * 2 + ]; } ], new Color($original), - $modified + $expected ], [ // When no custom encoder is supplied, the encodeMessage will return the data @@ -334,6 +337,17 @@ public function customEncoderProvider() ], new Color($original), $original + ], + [ + // When a custom encoder for a different protois supplied, + // the encodeMessage will return the data as if no custom encoder was supplied + [ + Status::class => function ($message) { + return ['foo' => 'bar']; + } + ], + new Color($original), + $original ] ]; }