Commit 703cd8e1 authored by Ryan Gordon's avatar Ryan Gordon Committed by Paul Yang

Switch to addEnumType to fix fatal error (#3225)

* Switch to addEnumType to fix fatal error

* Fixing more cases of HHVM incompatibility

* Updating tests to be hhvm compatible

* Fixing tests

* Fixing merge

* Don't use call_user_func, should hopefully fix tests

* Fixing spelling

* Fixing another misspelling in a test

* Fixing placement of append and kvUpdate functions

* Actually fix function helpers

* Remove double addEnumType. How did this ever work?

* Fixing a couple more tests

* Only use the setter if the return value isn't an object
parent 13255889
...@@ -78,12 +78,11 @@ class FileDescriptor ...@@ -78,12 +78,11 @@ class FileDescriptor
$message_proto, $proto, "")); $message_proto, $proto, ""));
} }
foreach ($proto->getEnumType() as $enum_proto) { foreach ($proto->getEnumType() as $enum_proto) {
$file->getEnumType()[] = $file->addEnumType(
$file->addEnumType( EnumDescriptor::buildFromProto(
EnumDescriptor::buildFromProto( $enum_proto,
$enum_proto, $proto,
$proto, ""));
""));
} }
return $file; return $file;
} }
......
...@@ -373,7 +373,7 @@ class Message ...@@ -373,7 +373,7 @@ class Message
$getter = $field->getGetter(); $getter = $field->getGetter();
while ($input->bytesUntilLimit() > 0) { while ($input->bytesUntilLimit() > 0) {
self::parseFieldFromStreamNoTag($input, $field, $value); self::parseFieldFromStreamNoTag($input, $field, $value);
$this->$getter()[] = $value; $this->appendHelper($field, $value);
} }
$input->popLimit($limit); $input->popLimit($limit);
return; return;
...@@ -382,11 +382,9 @@ class Message ...@@ -382,11 +382,9 @@ class Message
} }
if ($field->isMap()) { if ($field->isMap()) {
$getter = $field->getGetter(); $this->kvUpdateHelper($field, $value->getKey(), $value->getValue());
$this->$getter()[$value->getKey()] = $value->getValue();
} else if ($field->isRepeated()) { } else if ($field->isRepeated()) {
$getter = $field->getGetter(); $this->appendHelper($field, $value);
$this->$getter()[] = $value;
} else { } else {
$setter = $field->getSetter(); $setter = $field->getSetter();
$this->$setter($value); $this->$setter($value);
...@@ -533,9 +531,10 @@ class Message ...@@ -533,9 +531,10 @@ class Message
$klass = $value_field->getMessageType()->getClass(); $klass = $value_field->getMessageType()->getClass();
$copy = new $klass; $copy = new $klass;
$copy->mergeFrom($value); $copy->mergeFrom($value);
$this->$getter()[$key] = $copy;
$this->kvUpdateHelper($field, $key, $copy);
} else { } else {
$this->$getter()[$key] = $value; $this->kvUpdateHelper($field, $key, $value);
} }
} }
} }
...@@ -546,9 +545,9 @@ class Message ...@@ -546,9 +545,9 @@ class Message
$klass = $field->getMessageType()->getClass(); $klass = $field->getMessageType()->getClass();
$copy = new $klass; $copy = new $klass;
$copy->mergeFrom($tmp); $copy->mergeFrom($tmp);
$this->$getter()[] = $copy; $this->appendHelper($field, $copy);
} else { } else {
$this->$getter()[] = $tmp; $this->appendHelper($field, $tmp);
} }
} }
} }
...@@ -896,4 +895,30 @@ class Message ...@@ -896,4 +895,30 @@ class Message
} }
return $size; return $size;
} }
private function appendHelper($field, $append_value)
{
$getter = $field->getGetter();
$setter = $field->getSetter();
$field_arr_value = $this->$getter();
$field_arr_value[] = $append_value;
if (!is_object($field_arr_value)) {
$this->$setter($field_arr_value);
}
}
private function kvUpdateHelper($field, $update_key, $update_value)
{
$getter = $field->getGetter();
$setter = $field->getSetter();
$field_arr_value = $this->$getter();
$field_arr_value[$update_key] = $update_value;
if (!is_object($field_arr_value)) {
$this->$setter($field_arr_value);
}
}
} }
...@@ -743,24 +743,44 @@ class GeneratedClassTest extends TestBase ...@@ -743,24 +743,44 @@ class GeneratedClassTest extends TestBase
$n->setOptionalInt32(100); $n->setOptionalInt32(100);
$sub1 = new TestMessage_Sub(); $sub1 = new TestMessage_Sub();
$sub1->setA(101); $sub1->setA(101);
$sub1->getB()[] = 102;
$b = $sub1->getB();
$b[] = 102;
$sub1->setB($b);
$n->setOptionalMessage($sub1); $n->setOptionalMessage($sub1);
// Repeated // Repeated
$n->getRepeatedInt32()[] = 200; $repeatedInt32 = $n->getRepeatedInt32();
$n->getRepeatedString()[] = 'abc'; $repeatedInt32[] = 200;
$n->setRepeatedInt32($repeatedInt32);
$repeatedString = $n->getRepeatedString();
$repeatedString[] = 'abc';
$n->setRepeatedString($repeatedString);
$sub2 = new TestMessage_Sub(); $sub2 = new TestMessage_Sub();
$sub2->setA(201); $sub2->setA(201);
$n->getRepeatedMessage()[] = $sub2; $repeatedMessage = $n->getRepeatedMessage();
$repeatedMessage[] = $sub2;
$n->setRepeatedMessage($repeatedMessage);
// Map // Map
$n->getMapInt32Int32()[1] = 300; $mapInt32Int32 = $n->getMapInt32Int32();
$n->getMapInt32Int32()[-62] = 301; $mapInt32Int32[1] = 300;
$n->getMapStringString()['def'] = 'def'; $mapInt32Int32[-62] = 301;
$n->getMapInt32Message()[1] = new TestMessage_Sub(); $n->setMapInt32Int32($mapInt32Int32);
$n->getMapInt32Message()[1]->setA(302);
$n->getMapInt32Message()[2] = new TestMessage_Sub(); $mapStringString = $n->getMapStringString();
$n->getMapInt32Message()[2]->setA(303); $mapStringString['def'] = 'def';
$n->setMapStringString($mapStringString);
$mapInt32Message = $n->getMapInt32Message();
$mapInt32Message[1] = new TestMessage_Sub();
$mapInt32Message[1]->setA(302);
$mapInt32Message[2] = new TestMessage_Sub();
$mapInt32Message[2]->setA(303);
$n->setMapInt32Message($mapInt32Message);
$m->mergeFrom($n); $m->mergeFrom($n);
...@@ -795,9 +815,16 @@ class GeneratedClassTest extends TestBase ...@@ -795,9 +815,16 @@ class GeneratedClassTest extends TestBase
// Check sub-messages are copied by value. // Check sub-messages are copied by value.
$n->getOptionalMessage()->setA(-101); $n->getOptionalMessage()->setA(-101);
$this->assertSame(101, $m->getOptionalMessage()->getA()); $this->assertSame(101, $m->getOptionalMessage()->getA());
$n->getRepeatedMessage()[0]->setA(-201);
$repeatedMessage = $n->getRepeatedMessage();
$repeatedMessage[0]->setA(-201);
$n->setRepeatedMessage($repeatedMessage);
$this->assertSame(201, $m->getRepeatedMessage()[2]->getA()); $this->assertSame(201, $m->getRepeatedMessage()[2]->getA());
$n->getMapInt32Message()[1]->setA(-302);
$mapInt32Message = $n->getMapInt32Message();
$mapInt32Message[1]->setA(-302);
$n->setMapInt32Message($mapInt32Message);
$this->assertSame(302, $m->getMapInt32Message()[1]->getA()); $this->assertSame(302, $m->getMapInt32Message()[1]->getA());
// Test merge oneof. // Test merge oneof.
...@@ -843,7 +870,9 @@ class GeneratedClassTest extends TestBase ...@@ -843,7 +870,9 @@ class GeneratedClassTest extends TestBase
$m = new TestMessage(); $m = new TestMessage();
$sub = new NoNameSpaceMessage(); $sub = new NoNameSpaceMessage();
$m->setOptionalNoNamespaceMessage($sub); $m->setOptionalNoNamespaceMessage($sub);
$m->getRepeatedNoNamespaceMessage()[] = new NoNameSpaceMessage(); $repeatedNoNamespaceMessage = $m->getRepeatedNoNamespaceMessage();
$repeatedNoNamespaceMessage[] = new NoNameSpaceMessage();
$m->setRepeatedNoNamespaceMessage($repeatedNoNamespaceMessage);
$n = new NoNamespaceMessage(); $n = new NoNamespaceMessage();
$n->setB(NoNamespaceMessage_NestedEnum::ZERO); $n->setB(NoNamespaceMessage_NestedEnum::ZERO);
...@@ -853,7 +882,9 @@ class GeneratedClassTest extends TestBase ...@@ -853,7 +882,9 @@ class GeneratedClassTest extends TestBase
{ {
$m = new TestMessage(); $m = new TestMessage();
$m->setOptionalNoNamespaceEnum(NoNameSpaceEnum::VALUE_A); $m->setOptionalNoNamespaceEnum(NoNameSpaceEnum::VALUE_A);
$m->getRepeatedNoNamespaceEnum()[] = NoNameSpaceEnum::VALUE_A; $repeatedNoNamespaceEnum = $m->getRepeatedNoNamespaceEnum();
$repeatedNoNamespaceEnum[] = NoNameSpaceEnum::VALUE_A;
$m->setRepeatedNoNamespaceEnum($repeatedNoNamespaceEnum);
} }
######################################################### #########################################################
......
This diff is collapsed.
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