Unverified Commit 342ae0eb authored by Paul Yang's avatar Paul Yang Committed by GitHub

Fix issues for php map when parsing missing key/value (#6588)

* For missing message value, map should create a default message
instance in order to keep its invariable.
* On 32-bit platform, int64 map key should be string
parent 516f8b15
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
namespace Google\Protobuf\Internal; namespace Google\Protobuf\Internal;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\Message; use Google\Protobuf\Internal\Message;
class MapEntry extends Message class MapEntry extends Message
...@@ -39,6 +40,19 @@ class MapEntry extends Message ...@@ -39,6 +40,19 @@ class MapEntry extends Message
public $key; public $key;
public $value; public $value;
public function __construct($desc) {
parent::__construct($desc);
// For MapEntry, getValue should always return a valid value. Thus, we
// need to create a default instance value if the value type is
// message, in case no value is provided in data.
$value_field = $desc->getFieldByNumber(2);
if ($value_field->getType() == GPBType::MESSAGE) {
$klass = $value_field->getMessageType()->getClass();
$value = new $klass;
$this->setValue($value);
}
}
public function setKey($key) { public function setKey($key) {
$this->key = $key; $this->key = $key;
} }
......
...@@ -91,13 +91,23 @@ class MapFieldIter implements \Iterator ...@@ -91,13 +91,23 @@ class MapFieldIter implements \Iterator
public function key() public function key()
{ {
$key = key($this->container); $key = key($this->container);
if ($this->key_type === GPBType::BOOL) { switch ($this->key_type) {
// PHP associative array stores bool as integer for key. case GPBType::INT64:
return boolval($key); case GPBType::UINT64:
} elseif ($this->key_type === GPBType::STRING) { case GPBType::FIXED64:
case GPBType::SFIXED64:
case GPBType::SINT64:
if (PHP_INT_SIZE === 8) {
return $key;
}
// Intentionally fall through
case GPBType::STRING:
// PHP associative array stores int string as int for key. // PHP associative array stores int string as int for key.
return strval($key); return strval($key);
} else { case GPBType::BOOL:
// PHP associative array stores bool as integer for key.
return boolval($key);
default:
return $key; return $key;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment