Skip to content

Commit f3d0188

Browse files
authored
Merge pull request #184 from johnbirchevans/add_null_as_valid_value
Add null as valid value
2 parents 9339fe2 + 7378b20 commit f3d0188

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

src/GDS/Mapper/GRPCv1.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use GDS\Property\Geopoint;
2222
use GDS\Schema;
2323
use Google\Cloud\Datastore\V1\PartitionId;
24+
use Google\Protobuf\NullValue;
2425
use Google\Type\LatLng;
2526
use Google\Protobuf\Timestamp;
2627
use Google\Protobuf\Internal\RepeatedField;
@@ -276,8 +277,8 @@ private function configureGooglePropertyValue(array $arr_field_def, $mix_value)
276277
}
277278
$obj_val->setExcludeFromIndexes(!$bol_index);
278279

279-
// null checks
280-
if(null === $mix_value) {
280+
if (null === $mix_value) {
281+
$obj_val->setNullValue(NullValue::NULL_VALUE);
281282
return $obj_val;
282283
}
283284

src/GDS/Mapper/RESTv1.php

+5
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ protected function createPropertyValue(array $arr_field_def, $mix_value)
401401
}
402402
$obj_property_value->excludeFromIndexes = !$bol_index;
403403

404+
if (null === $mix_value) {
405+
$obj_property_value->nullValue = $mix_value;
406+
return $obj_property_value;
407+
}
408+
404409
switch ($arr_field_def['type']) {
405410
case Schema::PROPERTY_STRING:
406411
$obj_property_value->stringValue = (string)$mix_value;

tests/GRPCv1MapperTest.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
use Google\Cloud\Datastore\V1\Entity as GRPC_Entity;
4+
use Google\Cloud\Datastore\V1\PartitionId;
5+
6+
class GRPCv1MapperTest extends \PHPUnit\Framework\TestCase
7+
{
8+
public function testValidValuesMapToGoogleEntity()
9+
{
10+
$obj_schema = (new \GDS\Schema('Person'))
11+
->addString('name')
12+
->addInteger('age')
13+
->addFloat('weight')
14+
->addGeopoint('location')
15+
->addDatetime('dob');
16+
17+
$obj_mapper = new \GDS\Mapper\GRPCv1();
18+
$obj_mapper
19+
->setSchema($obj_schema);
20+
21+
22+
$obj_gds_entity = new \GDS\Entity();
23+
$obj_gds_entity->setSchema($obj_schema);
24+
$obj_gds_entity->setKind('Person');
25+
26+
$obj_gds_entity->name = 'Dave';
27+
$obj_gds_entity->age = 21;
28+
$obj_gds_entity->weight = 92.6;
29+
$obj_gds_entity->location = new \GDS\Property\Geopoint(1.2, 3.4);
30+
$obj_gds_entity->dob = new DateTime('1979-02-05 08:30:00');
31+
32+
$obj_grpc_entity = new GRPC_Entity();
33+
34+
$obj_mapper->mapToGoogle($obj_gds_entity, $obj_grpc_entity);
35+
36+
37+
$obj_properties = json_decode($obj_grpc_entity->serializeToJsonString())->properties;
38+
39+
$this->assertTrue(property_exists($obj_properties->name, 'stringValue'));
40+
$this->assertTrue(property_exists($obj_properties->age, 'integerValue'));
41+
$this->assertTrue(property_exists($obj_properties->weight, 'doubleValue'));
42+
$this->assertTrue(property_exists($obj_properties->location, 'geoPointValue'));
43+
$this->assertTrue(property_exists($obj_properties->dob, 'timestampValue'));
44+
}
45+
46+
public function testNullValuesMapToGoogleEntity()
47+
{
48+
$obj_schema = (new \GDS\Schema('Person'))
49+
->addString('name')
50+
->addInteger('age')
51+
->addFloat('weight')
52+
->addGeopoint('location')
53+
->addDatetime('dob');
54+
55+
$obj_mapper = new \GDS\Mapper\GRPCv1();
56+
$obj_mapper
57+
->setSchema($obj_schema);
58+
59+
60+
$obj_gds_entity = new \GDS\Entity();
61+
$obj_gds_entity->setSchema($obj_schema);
62+
$obj_gds_entity->setKind('Person');
63+
64+
$obj_gds_entity->name = null;
65+
$obj_gds_entity->age = null;
66+
$obj_gds_entity->weight = null;
67+
$obj_gds_entity->location = null;
68+
$obj_gds_entity->dob = null;
69+
70+
$obj_grpc_entity = new GRPC_Entity();
71+
72+
$obj_mapper->mapToGoogle($obj_gds_entity, $obj_grpc_entity);
73+
74+
75+
$obj_properties = json_decode($obj_grpc_entity->serializeToJsonString())->properties;
76+
77+
$this->assertTrue(property_exists($obj_properties->name, 'nullValue'));
78+
$this->assertTrue(property_exists($obj_properties->age, 'nullValue'));
79+
$this->assertTrue(property_exists($obj_properties->weight, 'nullValue'));
80+
$this->assertTrue(property_exists($obj_properties->location, 'nullValue'));
81+
$this->assertTrue(property_exists($obj_properties->dob, 'nullValue'));
82+
}
83+
}

tests/RESTv1MapperTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -567,4 +567,49 @@ private function buildFakeResponse(): \stdClass
567567
]
568568
];
569569
}
570+
571+
public function testNullValuesMapToGoogle()
572+
{
573+
$obj_schema = (new \GDS\Schema('Person'))
574+
->addString('name')
575+
->addInteger('age')
576+
->addFloat('weight')
577+
->addGeopoint('location')
578+
->addDatetime('dob');
579+
580+
$obj_mapper = new \GDS\Mapper\RESTv1();
581+
$obj_mapper->setSchema($obj_schema);
582+
583+
$obj_gds_entity = new \GDS\Entity();
584+
$obj_gds_entity->setSchema($obj_schema);
585+
$obj_gds_entity->setKind('Person');
586+
587+
$obj_gds_entity->name = null;
588+
$obj_gds_entity->age = null;
589+
$obj_gds_entity->weight = null;
590+
$obj_gds_entity->location = null;
591+
$obj_gds_entity->dob = null;
592+
593+
$obj_rest_entity = $obj_mapper->mapToGoogle($obj_gds_entity);
594+
595+
$this->assertObjectHasAttribute('name', $obj_rest_entity->properties);
596+
$this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->name);
597+
$this->assertEquals(null, $obj_rest_entity->properties->name->nullValue);
598+
599+
$this->assertObjectHasAttribute('age', $obj_rest_entity->properties);
600+
$this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->age);
601+
$this->assertEquals(null, $obj_rest_entity->properties->age->nullValue);
602+
603+
$this->assertObjectHasAttribute('weight', $obj_rest_entity->properties);
604+
$this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->weight);
605+
$this->assertEquals(null, $obj_rest_entity->properties->weight->nullValue);
606+
607+
$this->assertObjectHasAttribute('location', $obj_rest_entity->properties);
608+
$this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->location);
609+
$this->assertEquals(null, $obj_rest_entity->properties->location->nullValue);
610+
611+
$this->assertObjectHasAttribute('dob', $obj_rest_entity->properties);
612+
$this->assertObjectHasAttribute('nullValue', $obj_rest_entity->properties->dob);
613+
$this->assertEquals(null, $obj_rest_entity->properties->dob->nullValue);
614+
}
570615
}

0 commit comments

Comments
 (0)