Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize well known type values #6713

Merged
merged 2 commits into from Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
128 changes: 67 additions & 61 deletions php/ext/google/protobuf/message.c
Expand Up @@ -1770,19 +1770,19 @@ zend_class_entry* api_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Api",
Api, api)
zend_declare_property_null(api_type, "name", strlen("name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(api_type, "name", strlen("name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(api_type, "methods", strlen("methods"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(api_type, "options", strlen("options"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(api_type, "version", strlen("version"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(api_type, "version", strlen("version"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(api_type, "source_context", strlen("source_context"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(api_type, "mixins", strlen("mixins"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(api_type, "syntax", strlen("syntax"),
zend_declare_property_long(api_type, "syntax", strlen("syntax"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down Expand Up @@ -1816,7 +1816,7 @@ zend_class_entry* bool_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\BoolValue",
BoolValue, bool_value)
zend_declare_property_null(bool_value_type, "value", strlen("value"),
zend_declare_property_bool(bool_value_type, "value", strlen("value"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down Expand Up @@ -1844,8 +1844,8 @@ zend_class_entry* bytes_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\BytesValue",
BytesValue, bytes_value)
zend_declare_property_null(bytes_value_type, "value", strlen("value"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(bytes_value_type, "value", strlen("value"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

PHP_METHOD(BytesValue, __construct) {
Expand All @@ -1872,8 +1872,8 @@ zend_class_entry* double_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\DoubleValue",
DoubleValue, double_value)
zend_declare_property_null(double_value_type, "value", strlen("value"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_double(double_value_type, "value", strlen("value"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

PHP_METHOD(DoubleValue, __construct) {
Expand Down Expand Up @@ -1908,15 +1908,15 @@ zend_class_entry* enum_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Enum",
Enum, enum)
zend_declare_property_null(enum_type, "name", strlen("name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(enum_type, "name", strlen("name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(enum_type, "enumvalue", strlen("enumvalue"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(enum_type, "options", strlen("options"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(enum_type, "source_context", strlen("source_context"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(enum_type, "syntax", strlen("syntax"),
zend_declare_property_long(enum_type, "syntax", strlen("syntax"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down Expand Up @@ -1952,9 +1952,9 @@ zend_class_entry* enum_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\EnumValue",
EnumValue, enum_value)
zend_declare_property_null(enum_value_type, "name", strlen("name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(enum_value_type, "number", strlen("number"),
zend_declare_property_string(enum_value_type, "name", strlen("name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_long(enum_value_type, "number", strlen("number"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(enum_value_type, "options", strlen("options"),
ZEND_ACC_PRIVATE TSRMLS_CC);
Expand Down Expand Up @@ -2032,26 +2032,27 @@ zend_class_entry* field_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Field",
Field, field)
zend_declare_property_null(field_type, "kind", strlen("kind"),
zend_declare_property_long(field_type, "kind", strlen("kind"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "cardinality", strlen("cardinality"),
zend_declare_property_long(field_type, "cardinality", strlen("cardinality"),
0, ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_long(field_type, "number", strlen("number"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "number", strlen("number"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "name", strlen("name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "type_url", strlen("type_url"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "oneof_index", strlen("oneof_index"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "packed", strlen("packed"),
zend_declare_property_string(field_type, "name", strlen("name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(field_type, "type_url", strlen("type_url"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_long(field_type, "oneof_index", strlen("oneof_index"),
0, ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_bool(field_type, "packed", strlen("packed"), false,
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "options", strlen("options"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "json_name", strlen("json_name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(field_type, "default_value", strlen("default_value"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(field_type, "json_name", strlen("json_name"),
"", ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(field_type, "default_value",
strlen("default_value"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

PHP_METHOD(Field, __construct) {
Expand Down Expand Up @@ -2087,8 +2088,8 @@ zend_class_entry* float_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\FloatValue",
FloatValue, float_value)
zend_declare_property_null(float_value_type, "value", strlen("value"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_double(float_value_type, "value", strlen("value"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

PHP_METHOD(FloatValue, __construct) {
Expand Down Expand Up @@ -2138,7 +2139,7 @@ zend_class_entry* int32_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Int32Value",
Int32Value, int32_value)
zend_declare_property_null(int32_value_type, "value", strlen("value"),
zend_declare_property_long(int32_value_type, "value", strlen("value"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down Expand Up @@ -2166,7 +2167,7 @@ zend_class_entry* int64_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Int64Value",
Int64Value, int64_value)
zend_declare_property_null(int64_value_type, "value", strlen("value"),
zend_declare_property_long(int64_value_type, "value", strlen("value"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down Expand Up @@ -2234,19 +2235,23 @@ zend_class_entry* method_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Method",
Method, method)
zend_declare_property_null(method_type, "name", strlen("name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(method_type, "request_type_url", strlen("request_type_url"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(method_type, "request_streaming", strlen("request_streaming"),
zend_declare_property_string(method_type, "name", strlen("name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(method_type, "request_type_url",
strlen("request_type_url"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_bool(method_type, "request_streaming",
strlen("request_streaming"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(method_type, "response_type_url", strlen("response_type_url"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(method_type, "response_streaming", strlen("response_streaming"),
zend_declare_property_string(method_type, "response_type_url",
strlen("response_type_url"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_bool(method_type, "response_streaming",
strlen("response_streaming"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(method_type, "options", strlen("options"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(method_type, "syntax", strlen("syntax"),
zend_declare_property_long(method_type, "syntax", strlen("syntax"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down Expand Up @@ -2282,10 +2287,10 @@ zend_class_entry* mixin_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Mixin",
Mixin, mixin)
zend_declare_property_null(mixin_type, "name", strlen("name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(mixin_type, "root", strlen("root"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(mixin_type, "name", strlen("name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(mixin_type, "root", strlen("root"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

PHP_METHOD(Mixin, __construct) {
Expand Down Expand Up @@ -2315,8 +2320,8 @@ zend_class_entry* option_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Option",
Option, option)
zend_declare_property_null(option_type, "name", strlen("name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(option_type, "name", strlen("name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(option_type, "value", strlen("value"),
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END
Expand Down Expand Up @@ -2346,8 +2351,9 @@ zend_class_entry* source_context_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\SourceContext",
SourceContext, source_context)
zend_declare_property_null(source_context_type, "file_name", strlen("file_name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(source_context_type, "file_name",
strlen("file_name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

PHP_METHOD(SourceContext, __construct) {
Expand All @@ -2374,8 +2380,8 @@ zend_class_entry* string_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\StringValue",
StringValue, string_value)
zend_declare_property_null(string_value_type, "value", strlen("value"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(string_value_type, "value", strlen("value"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

PHP_METHOD(StringValue, __construct) {
Expand Down Expand Up @@ -2440,17 +2446,17 @@ zend_class_entry* type_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\Type",
Type, type)
zend_declare_property_null(type_type, "name", strlen("name"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(type_type, "name", strlen("name"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(type_type, "fields", strlen("fields"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(type_type, "oneofs", strlen("oneofs"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(type_type, "oneofs", strlen("oneofs"), "",
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(type_type, "options", strlen("options"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(type_type, "source_context", strlen("source_context"),
ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_null(type_type, "syntax", strlen("syntax"),
zend_declare_property_long(type_type, "syntax", strlen("syntax"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down Expand Up @@ -2483,7 +2489,7 @@ zend_class_entry* u_int32_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\UInt32Value",
UInt32Value, u_int32_value)
zend_declare_property_null(u_int32_value_type, "value", strlen("value"),
zend_declare_property_long(u_int32_value_type, "value", strlen("value"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down Expand Up @@ -2511,7 +2517,7 @@ zend_class_entry* u_int64_value_type;
// Init class entry.
PHP_PROTO_INIT_SUBMSGCLASS_START("Google\\Protobuf\\UInt64Value",
UInt64Value, u_int64_value)
zend_declare_property_null(u_int64_value_type, "value", strlen("value"),
zend_declare_property_long(u_int64_value_type, "value", strlen("value"), 0,
ZEND_ACC_PRIVATE TSRMLS_CC);
PHP_PROTO_INIT_SUBMSGCLASS_END

Expand Down
9 changes: 9 additions & 0 deletions php/tests/encode_decode_test.php
Expand Up @@ -5,6 +5,7 @@

use Google\Protobuf\RepeatedField;
use Google\Protobuf\GPBType;
use Foo\TestStringValue;
use Foo\TestAny;
use Foo\TestEnum;
use Foo\TestMessage;
Expand Down Expand Up @@ -167,6 +168,14 @@ public function testEncodeTopLevelStringValue()
$this->assertSame("\"a\"", $m->serializeToJsonString());
}

public function testEncodeStringValue()
{
$m = new TestStringValue(['field' => new StringValue(['value' => ''])]);
var_dump($m->getField());
var_dump($m->serializeToJsonString());
$this->assertSame("{\"field\":\"\"}", $m->serializeToJsonString());
}

public function testDecodeTopLevelBytesValue()
{
$m = new BytesValue();
Expand Down
1 change: 1 addition & 0 deletions php/tests/memory_leak_test.php
Expand Up @@ -31,6 +31,7 @@
require_once('generated/Foo/TestPhpDoc.php');
require_once('generated/Foo/TestRandomFieldOrder.php');
require_once('generated/Foo/TestReverseFieldOrder.php');
require_once('generated/Foo/TestStringValue.php');
require_once('generated/Foo/TestUnpackedMessage.php');
require_once('generated/Foo/testLowerCaseMessage.php');
require_once('generated/Foo/testLowerCaseEnum.php');
Expand Down
5 changes: 5 additions & 0 deletions php/tests/proto/test.proto
@@ -1,6 +1,7 @@
syntax = "proto3";

import 'google/protobuf/any.proto';
import 'google/protobuf/wrappers.proto';
import 'google/protobuf/struct.proto';
import 'proto/test_include.proto';
import 'proto/test_no_namespace.proto';
Expand Down Expand Up @@ -207,3 +208,7 @@ enum testLowerCaseEnum {
message TestAny {
google.protobuf.Any any = 1;
}

message TestStringValue {
google.protobuf.StringValue field = 1;
}