protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Can you provide a method to directly transfer to PHPArray?

Open hkargv opened this issue 3 years ago • 2 comments

What language does this apply to? If it's a proto syntax change, is it for proto2 or proto3? If it's about generated code change, what programming language?

Describe the problem you are trying to solve. a

Describe the solution you'd like

Describe alternatives you've considered

Additional context Add any other context or screenshots about the feature request here. I think json_encode has performance loss. Can I use PHP Array directly?like : b

hkargv avatar Jan 01 '23 03:01 hkargv

Have you tried this?

$c2s = new XMan\Get\Request($C2S);

haberman avatar Jan 05 '23 19:01 haberman

CentOS 9 + PHP 8.2.1 + protobuf 21.12

Get.proto

syntax = "proto3";
package Get;

option php_namespace = "XMan\\Get";
option php_metadata_namespace = "XMan\\Get\\pb";

message C2S
{
	int32 uid = 1;
}

message Request
{
	C2S c2s = 1;
}

php:

<?php
include(__DIR__ . '/../vendor/autoload.php');

function protobuf_autoloader($class){
	if (strncmp($class, 'XMan\\', 5) === 0) {
		include_once(__DIR__ . '/' . str_replace('\\', '/', $class) . '.php');
	}
}
spl_autoload_register('protobuf_autoloader');

$C2S = array(
	'c2s' => array(
		'uid' => 2000
	)
);
$c2s = new XMan\Get\Request($C2S);
/**Uncaught Exception: Expect XMan\Get\C2S. in /wwwroot/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:198
Stack trace:
#0 /wwwroot/tests/XMan/Get/Request.php(61): Google\Protobuf\Internal\GPBUtil::checkMessage()
#1 /wwwroot/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(1080): XMan\Get\Request->setC2S()
#2 /wwwroot/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(79): Google\Protobuf\Internal\Message->mergeFromArray()
#3 /wwwroot/tests/XMan/Get/Request.php(32): Google\Protobuf\Internal\Message->__construct()
#4 /wwwroot/tests/zx.php(16): XMan\Get\Request->__construct()
#5 {main}
  thrown in /wwwroot/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php on line 198**/

die();

//The following code works fine
$c2s = new XMan\Get\Request();
$c2s->mergeFromJsonString(json_encode($C2S), true);
$C2S = $c2s->serializeToString();

$c2s = new XMan\Get\Request();
$c2s->mergeFromString($C2S);
$C2S = $c2s->serializeToJsonString(); //Can you provide a method like 'serializeToPHPArray' ?
$C2S = json_decode($C2S, true);
print_r($C2S);

ghost avatar Jan 06 '23 12:01 ghost

It looks like the library is accepting the array, but it doesn't like the sub-array for the c2s field initializer.

It looks like you are using pure PHP. Does this example work with the Native C extension?

If so, we should fix the pure-PHP library to match.

haberman avatar Feb 27 '23 20:02 haberman

It looks like the library is accepting the array, but it doesn't like the sub-array for the c2s field initializer.

It looks like you are using pure PHP. Does this example work with the Native C extension?

If so, we should fix the pure-PHP library to match.

Neither "pure PHP" nor "Native C extension"

IN Native C extension:

PHP Fatal error:  Uncaught TypeError: Given value is not an instance of XMan\Get\C2S. in /wwwroot/tests/XMan/Get/Request.php:32
Stack trace:
#0 /wwwroot/tests/XMan/Get/Request.php(32): Google\Protobuf\Internal\Message->__construct()
#1 /wwwroot/tests/zx.php(21): XMan\Get\Request->__construct()
#2 {main}
  thrown in /wwwroot/tests/XMan/Get/Request.php on line 32

ghost avatar Mar 04 '23 02:03 ghost

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

github-actions[bot] avatar Dec 17 '23 10:12 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Jan 02 '24 10:01 github-actions[bot]