From aa70013bf3696c75c2f40c8ad4960be1005f4eba Mon Sep 17 00:00:00 2001 From: "john.brichevans" Date: Mon, 3 Apr 2023 17:33:14 +0100 Subject: [PATCH 1/3] Allow null values to be passed as a valid value --- src/GDS/Mapper/RESTv1.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/GDS/Mapper/RESTv1.php b/src/GDS/Mapper/RESTv1.php index 3e54abe..1abd67a 100644 --- a/src/GDS/Mapper/RESTv1.php +++ b/src/GDS/Mapper/RESTv1.php @@ -401,6 +401,11 @@ protected function createPropertyValue(array $arr_field_def, $mix_value) } $obj_property_value->excludeFromIndexes = !$bol_index; + if (is_null($mix_value)) { + $obj_property_value->nullValue = $mix_value; + return $obj_property_value; + } + switch ($arr_field_def['type']) { case Schema::PROPERTY_STRING: $obj_property_value->stringValue = (string)$mix_value; From ff103045dd1047d119180a59ac801ec2cd10e2e1 Mon Sep 17 00:00:00 2001 From: "john.brichevans" Date: Tue, 4 Apr 2023 11:38:01 +0100 Subject: [PATCH 2/3] Add test for the null changes --- tests/RESTv1MapperTest.php | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/RESTv1MapperTest.php b/tests/RESTv1MapperTest.php index 464d529..6fbadbe 100644 --- a/tests/RESTv1MapperTest.php +++ b/tests/RESTv1MapperTest.php @@ -567,4 +567,49 @@ private function buildFakeResponse(): \stdClass ] ]; } + + public function testNullValuesMapToGoogle() + { + $obj_schema = (new \GDS\Schema('Person')) + ->addString('name') + ->addInteger('age') + ->addFloat('weight') + ->addGeopoint('location') + ->addDatetime('dob'); + + $obj_mapper = new \GDS\Mapper\RESTv1(); + $obj_mapper->setSchema($obj_schema); + + $obj_gds_entity = new \GDS\Entity(); + $obj_gds_entity->setSchema($obj_schema); + $obj_gds_entity->setKind('Person'); + + $obj_gds_entity->name = null; + $obj_gds_entity->age = null; + $obj_gds_entity->weight = null; + $obj_gds_entity->location = null; + $obj_gds_entity->dob = null; + + $obj_rest_entity = $obj_mapper->mapToGoogle($obj_gds_entity); + + $this->assertObjectHasAttribute('name', $obj_rest_entity->properties); + $this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->name); + $this->assertEquals(null, $obj_rest_entity->properties->name->nullValue); + + $this->assertObjectHasAttribute('age', $obj_rest_entity->properties); + $this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->age); + $this->assertEquals(null, $obj_rest_entity->properties->age->nullValue); + + $this->assertObjectHasAttribute('weight', $obj_rest_entity->properties); + $this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->weight); + $this->assertEquals(null, $obj_rest_entity->properties->weight->nullValue); + + $this->assertObjectHasAttribute('location', $obj_rest_entity->properties); + $this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->location); + $this->assertEquals(null, $obj_rest_entity->properties->location->nullValue); + + $this->assertObjectHasAttribute('dob', $obj_rest_entity->properties); + $this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->dob); + $this->assertEquals(null, $obj_rest_entity->properties->dob->nullValue); + } } From 7378b2080bbd4d2de405180f8b503b70af829d42 Mon Sep 17 00:00:00 2001 From: "john.brichevans" Date: Fri, 14 Apr 2023 11:40:43 +0100 Subject: [PATCH 3/3] Update null check and add to GRPC with tests --- src/GDS/Mapper/GRPCv1.php | 5 ++- src/GDS/Mapper/RESTv1.php | 2 +- tests/GRPCv1MapperTest.php | 83 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 tests/GRPCv1MapperTest.php diff --git a/src/GDS/Mapper/GRPCv1.php b/src/GDS/Mapper/GRPCv1.php index 7497e2e..de8028d 100755 --- a/src/GDS/Mapper/GRPCv1.php +++ b/src/GDS/Mapper/GRPCv1.php @@ -21,6 +21,7 @@ use GDS\Property\Geopoint; use GDS\Schema; use Google\Cloud\Datastore\V1\PartitionId; +use Google\Protobuf\NullValue; use Google\Type\LatLng; use Google\Protobuf\Timestamp; use Google\Protobuf\Internal\RepeatedField; @@ -276,8 +277,8 @@ private function configureGooglePropertyValue(array $arr_field_def, $mix_value) } $obj_val->setExcludeFromIndexes(!$bol_index); - // null checks - if(null === $mix_value) { + if (null === $mix_value) { + $obj_val->setNullValue(NullValue::NULL_VALUE); return $obj_val; } diff --git a/src/GDS/Mapper/RESTv1.php b/src/GDS/Mapper/RESTv1.php index 1abd67a..de6e71d 100644 --- a/src/GDS/Mapper/RESTv1.php +++ b/src/GDS/Mapper/RESTv1.php @@ -401,7 +401,7 @@ protected function createPropertyValue(array $arr_field_def, $mix_value) } $obj_property_value->excludeFromIndexes = !$bol_index; - if (is_null($mix_value)) { + if (null === $mix_value) { $obj_property_value->nullValue = $mix_value; return $obj_property_value; } diff --git a/tests/GRPCv1MapperTest.php b/tests/GRPCv1MapperTest.php new file mode 100644 index 0000000..e74f81e --- /dev/null +++ b/tests/GRPCv1MapperTest.php @@ -0,0 +1,83 @@ +addString('name') + ->addInteger('age') + ->addFloat('weight') + ->addGeopoint('location') + ->addDatetime('dob'); + + $obj_mapper = new \GDS\Mapper\GRPCv1(); + $obj_mapper + ->setSchema($obj_schema); + + + $obj_gds_entity = new \GDS\Entity(); + $obj_gds_entity->setSchema($obj_schema); + $obj_gds_entity->setKind('Person'); + + $obj_gds_entity->name = 'Dave'; + $obj_gds_entity->age = 21; + $obj_gds_entity->weight = 92.6; + $obj_gds_entity->location = new \GDS\Property\Geopoint(1.2, 3.4); + $obj_gds_entity->dob = new DateTime('1979-02-05 08:30:00'); + + $obj_grpc_entity = new GRPC_Entity(); + + $obj_mapper->mapToGoogle($obj_gds_entity, $obj_grpc_entity); + + + $obj_properties = json_decode($obj_grpc_entity->serializeToJsonString())->properties; + + $this->assertTrue(property_exists($obj_properties->name, 'stringValue')); + $this->assertTrue(property_exists($obj_properties->age, 'integerValue')); + $this->assertTrue(property_exists($obj_properties->weight, 'doubleValue')); + $this->assertTrue(property_exists($obj_properties->location, 'geoPointValue')); + $this->assertTrue(property_exists($obj_properties->dob, 'timestampValue')); + } + + public function testNullValuesMapToGoogleEntity() + { + $obj_schema = (new \GDS\Schema('Person')) + ->addString('name') + ->addInteger('age') + ->addFloat('weight') + ->addGeopoint('location') + ->addDatetime('dob'); + + $obj_mapper = new \GDS\Mapper\GRPCv1(); + $obj_mapper + ->setSchema($obj_schema); + + + $obj_gds_entity = new \GDS\Entity(); + $obj_gds_entity->setSchema($obj_schema); + $obj_gds_entity->setKind('Person'); + + $obj_gds_entity->name = null; + $obj_gds_entity->age = null; + $obj_gds_entity->weight = null; + $obj_gds_entity->location = null; + $obj_gds_entity->dob = null; + + $obj_grpc_entity = new GRPC_Entity(); + + $obj_mapper->mapToGoogle($obj_gds_entity, $obj_grpc_entity); + + + $obj_properties = json_decode($obj_grpc_entity->serializeToJsonString())->properties; + + $this->assertTrue(property_exists($obj_properties->name, 'nullValue')); + $this->assertTrue(property_exists($obj_properties->age, 'nullValue')); + $this->assertTrue(property_exists($obj_properties->weight, 'nullValue')); + $this->assertTrue(property_exists($obj_properties->location, 'nullValue')); + $this->assertTrue(property_exists($obj_properties->dob, 'nullValue')); + } +} \ No newline at end of file