Skip to content

Commit

Permalink
Merge pull request #184 from johnbirchevans/add_null_as_valid_value
Browse files Browse the repository at this point in the history
Add null as valid value
  • Loading branch information
tomwalder authored Apr 14, 2023
2 parents 9339fe2 + 7378b20 commit f3d0188
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/GDS/Mapper/GRPCv1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions src/GDS/Mapper/RESTv1.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ protected function createPropertyValue(array $arr_field_def, $mix_value)
}
$obj_property_value->excludeFromIndexes = !$bol_index;

if (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;
Expand Down
83 changes: 83 additions & 0 deletions tests/GRPCv1MapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

use Google\Cloud\Datastore\V1\Entity as GRPC_Entity;
use Google\Cloud\Datastore\V1\PartitionId;

class GRPCv1MapperTest extends \PHPUnit\Framework\TestCase
{
public function testValidValuesMapToGoogleEntity()
{
$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 = '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'));
}
}
45 changes: 45 additions & 0 deletions tests/RESTv1MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit f3d0188

Please sign in to comment.