Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Fixed unset field default values (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjagodzinski authored Aug 29, 2016
1 parent 3ab9e6a commit cbea42f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
9 changes: 3 additions & 6 deletions src/Allegro/Protobuf/Compiler/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,11 @@ private function _describeSingleField(FieldDescriptor $field, CodeStringBuffer $
if ($phpType != 'object') {
$typeName = $phpType;
$argumentClass = '';
$returnCast = '(' . $phpType . ')';
} else {
$typeName = $this->_createFullyQualifiedClassName($field->getTypeDescriptor());
$argumentClass = $typeName . ' ';
$returnCast = '';
}

$comment = new CommentStringBuffer(self::TAB, self::EOL);
Expand Down Expand Up @@ -703,12 +705,7 @@ private function _describeSingleField(FieldDescriptor $field, CodeStringBuffer $
->append($comment)
->append('public function get' . $field->getCamelCaseName() . '()')
->append('{')
->append(
'return ' .
'$this->get(self::' . $field->getConstName() . ');',
false,
1
)
->append('return ' . $returnCast . '$this->get(self::' . $field->getConstName() . ');', false, 1)
->append('}');
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Bar.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Auto generated from test.proto at 2016-08-24 20:32:21
* Auto generated from test.proto at 2016-08-29 12:36:15
*/

namespace {
Expand Down Expand Up @@ -68,7 +68,7 @@ public function setDoubleField($value)
*/
public function getDoubleField()
{
return $this->get(self::DOUBLE_FIELD);
return (double)$this->get(self::DOUBLE_FIELD);
}
}
}
4 changes: 2 additions & 2 deletions tests/Baz.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Auto generated from test.proto at 2016-08-24 20:32:21
* Auto generated from test.proto at 2016-08-29 12:36:15
*/

namespace {
Expand Down Expand Up @@ -68,7 +68,7 @@ public function setId($value)
*/
public function getId()
{
return $this->get(self::ID);
return (integer)$this->get(self::ID);
}
}
}
32 changes: 16 additions & 16 deletions tests/Foo.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Auto generated from test.proto at 2016-08-24 20:32:21
* Auto generated from test.proto at 2016-08-29 12:36:15
*/

namespace {
Expand Down Expand Up @@ -257,7 +257,7 @@ public function setDoubleField($value)
*/
public function getDoubleField()
{
return $this->get(self::DOUBLE_FIELD);
return (double)$this->get(self::DOUBLE_FIELD);
}

/**
Expand All @@ -279,7 +279,7 @@ public function setFloatField($value)
*/
public function getFloatField()
{
return $this->get(self::FLOAT_FIELD);
return (double)$this->get(self::FLOAT_FIELD);
}

/**
Expand All @@ -301,7 +301,7 @@ public function setInt32Field($value)
*/
public function getInt32Field()
{
return $this->get(self::INT32_FIELD);
return (integer)$this->get(self::INT32_FIELD);
}

/**
Expand All @@ -323,7 +323,7 @@ public function setInt64Field($value)
*/
public function getInt64Field()
{
return $this->get(self::INT64_FIELD);
return (integer)$this->get(self::INT64_FIELD);
}

/**
Expand All @@ -345,7 +345,7 @@ public function setUint32Field($value)
*/
public function getUint32Field()
{
return $this->get(self::UINT32_FIELD);
return (integer)$this->get(self::UINT32_FIELD);
}

/**
Expand All @@ -367,7 +367,7 @@ public function setUint64Field($value)
*/
public function getUint64Field()
{
return $this->get(self::UINT64_FIELD);
return (integer)$this->get(self::UINT64_FIELD);
}

/**
Expand All @@ -389,7 +389,7 @@ public function setSint32Field($value)
*/
public function getSint32Field()
{
return $this->get(self::SINT32_FIELD);
return (integer)$this->get(self::SINT32_FIELD);
}

/**
Expand All @@ -411,7 +411,7 @@ public function setSint64Field($value)
*/
public function getSint64Field()
{
return $this->get(self::SINT64_FIELD);
return (integer)$this->get(self::SINT64_FIELD);
}

/**
Expand All @@ -433,7 +433,7 @@ public function setFixed32Field($value)
*/
public function getFixed32Field()
{
return $this->get(self::FIXED32_FIELD);
return (integer)$this->get(self::FIXED32_FIELD);
}

/**
Expand All @@ -455,7 +455,7 @@ public function setFixed64Field($value)
*/
public function getFixed64Field()
{
return $this->get(self::FIXED64_FIELD);
return (integer)$this->get(self::FIXED64_FIELD);
}

/**
Expand All @@ -477,7 +477,7 @@ public function setSfixed32Field($value)
*/
public function getSfixed32Field()
{
return $this->get(self::SFIXED32_FIELD);
return (integer)$this->get(self::SFIXED32_FIELD);
}

/**
Expand All @@ -499,7 +499,7 @@ public function setSfixed64Field($value)
*/
public function getSfixed64Field()
{
return $this->get(self::SFIXED64_FIELD);
return (integer)$this->get(self::SFIXED64_FIELD);
}

/**
Expand All @@ -521,7 +521,7 @@ public function setBoolField($value)
*/
public function getBoolField()
{
return $this->get(self::BOOL_FIELD);
return (boolean)$this->get(self::BOOL_FIELD);
}

/**
Expand All @@ -543,7 +543,7 @@ public function setStringField($value)
*/
public function getStringField()
{
return $this->get(self::STRING_FIELD);
return (string)$this->get(self::STRING_FIELD);
}

/**
Expand All @@ -565,7 +565,7 @@ public function setBytesField($value)
*/
public function getBytesField()
{
return $this->get(self::BYTES_FIELD);
return (string)$this->get(self::BYTES_FIELD);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/unset_field_default_values.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Protocol Buffers unset field default values
--SKIPIF--
<?php require 'skipif.inc' ?>
--FILE--
<?php
require 'Foo.php';

$foo = new Foo();
var_dump($foo->getDoubleField());
var_dump($foo->getFloatField());
var_dump($foo->getInt32Field());
var_dump($foo->getInt64Field());
var_dump($foo->getUint32Field());
var_dump($foo->getUint64Field());
var_dump($foo->getSint32Field());
var_dump($foo->getSint64Field());
var_dump($foo->getFixed32Field());
var_dump($foo->getFixed64Field());
var_dump($foo->getSfixed32Field());
var_dump($foo->getSfixed64Field());
var_dump($foo->getBoolField());
var_dump($foo->getStringField());
var_dump($foo->getBytesField());
var_dump($foo->getRepeatedField());
var_dump($foo->getEmbeddedField());
?>
--EXPECT--
float(0)
float(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
bool(false)
string(0) ""
string(0) ""
array(0) {
}
NULL

0 comments on commit cbea42f

Please sign in to comment.