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

Commit 3ab9e6a

Browse files
authored
Fixed setting field value (#93)
1 parent 3a62bfa commit 3ab9e6a

13 files changed

+364
-29
lines changed

protobuf.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ PHP_METHOD(ProtobufMessage, serializeToString)
571571

572572
PHP_METHOD(ProtobufMessage, set)
573573
{
574-
long field_number = -1;
575-
zval *prepared_value, **old_value, *value, **values, *val;
574+
long field_number;
575+
zval *prepared_value, *value, **values;
576576

577577
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &field_number, &value) == FAILURE) {
578578
RETURN_THIS();
@@ -583,14 +583,8 @@ PHP_METHOD(ProtobufMessage, set)
583583

584584
if (Z_TYPE_P(value) == IS_NULL) {
585585
// null value means 'no value', therefore should not be converted
586-
if ((old_value = pb_get_value(getThis(), values, field_number)) == NULL) {
587-
RETURN_THIS();
588-
}
589-
if (Z_TYPE_PP(old_value) != IS_NULL) {
590-
if ((prepared_value = pb_prepare_value(getThis(), field_number, value)) != NULL) {
591-
add_index_zval(*values, field_number, prepared_value);
592-
}
593-
}
586+
Z_ADDREF_P(value);
587+
add_index_zval(*values, field_number, value);
594588
} else {
595589
if ((prepared_value = pb_prepare_value(getThis(), field_number, value)) != NULL) {
596590
add_index_zval(*values, field_number, prepared_value);
@@ -741,19 +735,19 @@ static zval *pb_prepare_value(zval *this, uint32_t field_number, zval *value)
741735
PB_COMPILE_ERROR_EX(this, "unexpected '%s' field type %d in field descriptor", pb_get_field_name(this, field_number), zend_get_type_by_const(Z_LVAL_PP(type)));
742736
goto fail;
743737
}
738+
739+
if (Z_TYPE_P(value) != expected_type) {
740+
ALLOC_ZVAL(converted_value);
741+
INIT_PZVAL_COPY(converted_value, value);
742+
zval_copy_ctor(converted_value);
743+
convert_to_explicit_type(converted_value, expected_type);
744+
return converted_value;
745+
}
744746
} else if (Z_TYPE_PP(type) != IS_STRING) {
745747
PB_COMPILE_ERROR_EX(this, "unexpected %s type of '%s' field type in field descriptor", zend_get_type_by_const(Z_TYPE_PP(type)), pb_get_field_name(this, field_number));
746748
goto fail;
747749
}
748750

749-
if (Z_TYPE_P(value) != expected_type) {
750-
ALLOC_ZVAL(converted_value);
751-
INIT_PZVAL_COPY(converted_value, value);
752-
zval_copy_ctor(converted_value);
753-
convert_to_explicit_type(converted_value, expected_type);
754-
return converted_value;
755-
}
756-
757751
Z_ADDREF_P(value);
758752
return value;
759753
fail:

tests/Bar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from test.proto at 2016-08-15 23:40:08
3+
* Auto generated from test.proto at 2016-08-24 20:32:21
44
*/
55

66
namespace {

tests/Baz.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from test.proto at 2016-08-16 02:35:04
3+
* Auto generated from test.proto at 2016-08-24 20:32:21
44
*/
55

66
namespace {

tests/Foo.php

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from test.proto at 2016-08-15 23:40:08
3+
* Auto generated from test.proto at 2016-08-24 20:32:21
44
*/
55

66
namespace {
@@ -35,6 +35,8 @@ class Foo extends \ProtobufMessage
3535
const SINT32_PACKED_FIELD = 23;
3636
const BOOL_PACKED_FIELD = 24;
3737
const OPTIONAL_EMBEDDED_FIELD = 25;
38+
const REPEATED_OBJ_FIELD = 26;
39+
const REPEATED_STRING_FIELD = 27;
3840

3941
/* @var array Field descriptors */
4042
protected static $fields = array(
@@ -170,6 +172,16 @@ class Foo extends \ProtobufMessage
170172
'required' => false,
171173
'type' => '\Baz'
172174
),
175+
self::REPEATED_OBJ_FIELD => array(
176+
'name' => 'repeated_obj_field',
177+
'repeated' => true,
178+
'type' => '\Bar'
179+
),
180+
self::REPEATED_STRING_FIELD => array(
181+
'name' => 'repeated_string_field',
182+
'repeated' => true,
183+
'type' => \ProtobufMessage::PB_TYPE_STRING,
184+
),
173185
);
174186

175187
/**
@@ -212,6 +224,8 @@ public function reset()
212224
$this->values[self::SINT32_PACKED_FIELD] = array();
213225
$this->values[self::BOOL_PACKED_FIELD] = array();
214226
$this->values[self::OPTIONAL_EMBEDDED_FIELD] = null;
227+
$this->values[self::REPEATED_OBJ_FIELD] = array();
228+
$this->values[self::REPEATED_STRING_FIELD] = array();
215229
}
216230

217231
/**
@@ -1109,5 +1123,133 @@ public function getOptionalEmbeddedField()
11091123
{
11101124
return $this->get(self::OPTIONAL_EMBEDDED_FIELD);
11111125
}
1126+
1127+
/**
1128+
* Appends value to 'repeated_obj_field' list
1129+
*
1130+
* @param \Bar $value Value to append
1131+
*
1132+
* @return null
1133+
*/
1134+
public function appendRepeatedObjField(\Bar $value)
1135+
{
1136+
return $this->append(self::REPEATED_OBJ_FIELD, $value);
1137+
}
1138+
1139+
/**
1140+
* Clears 'repeated_obj_field' list
1141+
*
1142+
* @return null
1143+
*/
1144+
public function clearRepeatedObjField()
1145+
{
1146+
return $this->clear(self::REPEATED_OBJ_FIELD);
1147+
}
1148+
1149+
/**
1150+
* Returns 'repeated_obj_field' list
1151+
*
1152+
* @return \Bar[]
1153+
*/
1154+
public function getRepeatedObjField()
1155+
{
1156+
return $this->get(self::REPEATED_OBJ_FIELD);
1157+
}
1158+
1159+
/**
1160+
* Returns 'repeated_obj_field' iterator
1161+
*
1162+
* @return \ArrayIterator
1163+
*/
1164+
public function getRepeatedObjFieldIterator()
1165+
{
1166+
return new \ArrayIterator($this->get(self::REPEATED_OBJ_FIELD));
1167+
}
1168+
1169+
/**
1170+
* Returns element from 'repeated_obj_field' list at given offset
1171+
*
1172+
* @param int $offset Position in list
1173+
*
1174+
* @return \Bar
1175+
*/
1176+
public function getRepeatedObjFieldAt($offset)
1177+
{
1178+
return $this->get(self::REPEATED_OBJ_FIELD, $offset);
1179+
}
1180+
1181+
/**
1182+
* Returns count of 'repeated_obj_field' list
1183+
*
1184+
* @return int
1185+
*/
1186+
public function getRepeatedObjFieldCount()
1187+
{
1188+
return $this->count(self::REPEATED_OBJ_FIELD);
1189+
}
1190+
1191+
/**
1192+
* Appends value to 'repeated_string_field' list
1193+
*
1194+
* @param string $value Value to append
1195+
*
1196+
* @return null
1197+
*/
1198+
public function appendRepeatedStringField($value)
1199+
{
1200+
return $this->append(self::REPEATED_STRING_FIELD, $value);
1201+
}
1202+
1203+
/**
1204+
* Clears 'repeated_string_field' list
1205+
*
1206+
* @return null
1207+
*/
1208+
public function clearRepeatedStringField()
1209+
{
1210+
return $this->clear(self::REPEATED_STRING_FIELD);
1211+
}
1212+
1213+
/**
1214+
* Returns 'repeated_string_field' list
1215+
*
1216+
* @return string[]
1217+
*/
1218+
public function getRepeatedStringField()
1219+
{
1220+
return $this->get(self::REPEATED_STRING_FIELD);
1221+
}
1222+
1223+
/**
1224+
* Returns 'repeated_string_field' iterator
1225+
*
1226+
* @return \ArrayIterator
1227+
*/
1228+
public function getRepeatedStringFieldIterator()
1229+
{
1230+
return new \ArrayIterator($this->get(self::REPEATED_STRING_FIELD));
1231+
}
1232+
1233+
/**
1234+
* Returns element from 'repeated_string_field' list at given offset
1235+
*
1236+
* @param int $offset Position in list
1237+
*
1238+
* @return string
1239+
*/
1240+
public function getRepeatedStringFieldAt($offset)
1241+
{
1242+
return $this->get(self::REPEATED_STRING_FIELD, $offset);
1243+
}
1244+
1245+
/**
1246+
* Returns count of 'repeated_string_field' list
1247+
*
1248+
* @return int
1249+
*/
1250+
public function getRepeatedStringFieldCount()
1251+
{
1252+
return $this->count(self::REPEATED_STRING_FIELD);
1253+
}
11121254
}
11131255
}

tests/append_float_value.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Protocol Buffers appending floating-point value
3+
--SKIPIF--
4+
<?php require 'skipif.inc' ?>
5+
--FILE--
6+
<?php
7+
require 'Foo.php';
8+
9+
$foo = new Foo();
10+
11+
/* from float type */
12+
$foo->appendFloatPackedField(2.0);
13+
14+
/* from int type */
15+
$intValue = 3;
16+
$foo->appendFloatPackedField($intValue);
17+
var_dump($intValue);
18+
19+
/* from string type */
20+
$stringValue = '4';
21+
$foo->appendFloatPackedField($stringValue);
22+
var_dump($stringValue);
23+
24+
/* from null */
25+
$nullValue = null;
26+
$foo->appendFloatPackedField($nullValue);
27+
28+
$values = $foo->getFloatPackedField();
29+
var_dump(count($values));
30+
var_dump($values[0]);
31+
var_dump($values[1]);
32+
var_dump($values[2]);
33+
?>
34+
--EXPECT--
35+
int(3)
36+
string(1) "4"
37+
int(3)
38+
float(2)
39+
float(3)
40+
float(4)

tests/append_int_value.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Protocol Buffers appending integer value
3+
--SKIPIF--
4+
<?php require 'skipif.inc' ?>
5+
--FILE--
6+
<?php
7+
require 'Foo.php';
8+
9+
$foo = new Foo();
10+
11+
/* from int type */
12+
$foo->appendInt32PackedField(2);
13+
14+
/* from float type */
15+
$floatValue = 3.0;
16+
$foo->appendInt32PackedField($floatValue);
17+
var_dump($floatValue);
18+
19+
/* from string type */
20+
$stringValue = '4';
21+
$foo->appendInt32PackedField($stringValue);
22+
var_dump($stringValue);
23+
24+
/* from null */
25+
$nullValue = null;
26+
$foo->appendInt32PackedField($nullValue);
27+
28+
$values = $foo->getInt32PackedField();
29+
var_dump(count($values));
30+
var_dump($values[0]);
31+
var_dump($values[1]);
32+
var_dump($values[2]);
33+
?>
34+
--EXPECT--
35+
float(3)
36+
string(1) "4"
37+
int(3)
38+
int(2)
39+
int(3)
40+
int(4)

tests/append_object_value.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Protocol Buffers appending object value
3+
--SKIPIF--
4+
<?php require 'skipif.inc' ?>
5+
--FILE--
6+
<?php
7+
require 'Foo.php';
8+
require 'Bar.php';
9+
10+
$barDestructed = false;
11+
12+
class SpiedBar extends Bar {
13+
public function __destruct() {
14+
global $barDestructed;
15+
$barDestructed = true;
16+
}
17+
}
18+
19+
$bar = new SpiedBar();
20+
$bar->setDoubleField(1.0);
21+
22+
$foo = new Foo();
23+
$foo->appendRepeatedObjField($bar);
24+
25+
$values = $foo->getRepeatedObjField();
26+
var_dump(count($values));
27+
var_dump($values[0] === $bar);
28+
$values = null;
29+
30+
$foo->clearRepeatedObjField();
31+
var_dump($barDestructed);
32+
33+
$bar = null;
34+
var_dump($barDestructed);
35+
?>
36+
--EXPECT--
37+
int(1)
38+
bool(true)
39+
bool(false)
40+
bool(true)

0 commit comments

Comments
 (0)