|
@@ -5,7 +5,6 @@
|
|
|
namespace Google\Protobuf;
|
|
|
|
|
|
use Google\Protobuf\Internal\GPBType;
|
|
|
-use Google\Protobuf\Internal\Message;
|
|
|
use Google\Protobuf\Internal\RepeatedField;
|
|
|
use Google\Protobuf\Internal\GPBUtil;
|
|
|
|
|
@@ -39,10 +38,13 @@ use Google\Protobuf\Internal\GPBUtil;
|
|
|
* ...
|
|
|
* Example 4: Pack and unpack a message in Go
|
|
|
* foo := &pb.Foo{...}
|
|
|
- * any, err := ptypes.MarshalAny(foo)
|
|
|
+ * any, err := anypb.New(foo)
|
|
|
+ * if err != nil {
|
|
|
+ * ...
|
|
|
+ * }
|
|
|
* ...
|
|
|
* foo := &pb.Foo{}
|
|
|
- * if err := ptypes.UnmarshalAny(any, foo); err != nil {
|
|
|
+ * if err := any.UnmarshalTo(foo); err != nil {
|
|
|
* ...
|
|
|
* }
|
|
|
* The pack methods provided by protobuf library will by default use
|
|
@@ -76,11 +78,12 @@ use Google\Protobuf\Internal\GPBUtil;
|
|
|
*
|
|
|
* Generated from protobuf message <code>google.protobuf.Any</code>
|
|
|
*/
|
|
|
-class Any extends \Google\Protobuf\Internal\Message
|
|
|
+class Any extends \Google\Protobuf\Internal\AnyBase
|
|
|
{
|
|
|
/**
|
|
|
* A URL/resource name that uniquely identifies the type of the serialized
|
|
|
- * protocol buffer message. The last segment of the URL's path must represent
|
|
|
+ * protocol buffer message. This string must contain at least
|
|
|
+ * one "/" character. The last segment of the URL's path must represent
|
|
|
* the fully qualified name of the type (as in
|
|
|
* `path/google.protobuf.Duration`). The name should be in a canonical form
|
|
|
* (e.g., leading "." is not accepted).
|
|
@@ -104,15 +107,13 @@ class Any extends \Google\Protobuf\Internal\Message
|
|
|
*
|
|
|
* Generated from protobuf field <code>string type_url = 1;</code>
|
|
|
*/
|
|
|
- private $type_url = '';
|
|
|
+ protected $type_url = '';
|
|
|
/**
|
|
|
* Must be a valid serialized protocol buffer of the above specified type.
|
|
|
*
|
|
|
* Generated from protobuf field <code>bytes value = 2;</code>
|
|
|
*/
|
|
|
- private $value = '';
|
|
|
-
|
|
|
- const TYPE_URL_PREFIX = 'type.googleapis.com/';
|
|
|
+ protected $value = '';
|
|
|
|
|
|
/**
|
|
|
* Constructor.
|
|
@@ -122,7 +123,8 @@ class Any extends \Google\Protobuf\Internal\Message
|
|
|
*
|
|
|
* @type string $type_url
|
|
|
* A URL/resource name that uniquely identifies the type of the serialized
|
|
|
- * protocol buffer message. The last segment of the URL's path must represent
|
|
|
+ * protocol buffer message. This string must contain at least
|
|
|
+ * one "/" character. The last segment of the URL's path must represent
|
|
|
* the fully qualified name of the type (as in
|
|
|
* `path/google.protobuf.Duration`). The name should be in a canonical form
|
|
|
* (e.g., leading "." is not accepted).
|
|
@@ -154,7 +156,8 @@ class Any extends \Google\Protobuf\Internal\Message
|
|
|
|
|
|
/**
|
|
|
* A URL/resource name that uniquely identifies the type of the serialized
|
|
|
- * protocol buffer message. The last segment of the URL's path must represent
|
|
|
+ * protocol buffer message. This string must contain at least
|
|
|
+ * one "/" character. The last segment of the URL's path must represent
|
|
|
* the fully qualified name of the type (as in
|
|
|
* `path/google.protobuf.Duration`). The name should be in a canonical form
|
|
|
* (e.g., leading "." is not accepted).
|
|
@@ -186,7 +189,8 @@ class Any extends \Google\Protobuf\Internal\Message
|
|
|
|
|
|
/**
|
|
|
* A URL/resource name that uniquely identifies the type of the serialized
|
|
|
- * protocol buffer message. The last segment of the URL's path must represent
|
|
|
+ * protocol buffer message. This string must contain at least
|
|
|
+ * one "/" character. The last segment of the URL's path must represent
|
|
|
* the fully qualified name of the type (as in
|
|
|
* `path/google.protobuf.Duration`). The name should be in a canonical form
|
|
|
* (e.g., leading "." is not accepted).
|
|
@@ -246,78 +250,5 @@ class Any extends \Google\Protobuf\Internal\Message
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * This method will try to resolve the type_url in Any message to get the
|
|
|
- * targeted message type. If failed, an error will be thrown. Otherwise,
|
|
|
- * the method will create a message of the targeted type and fill it with
|
|
|
- * the decoded value in Any.
|
|
|
- * @return Message unpacked message
|
|
|
- * @throws \Exception Type url needs to be type.googleapis.com/fully-qualified.
|
|
|
- * @throws \Exception Class hasn't been added to descriptor pool.
|
|
|
- * @throws \Exception cannot decode data in value field.
|
|
|
- */
|
|
|
- public function unpack()
|
|
|
- {
|
|
|
- // Get fully qualified name from type url.
|
|
|
- $url_prifix_len = strlen(GPBUtil::TYPE_URL_PREFIX);
|
|
|
- if (substr($this->type_url, 0, $url_prifix_len) !=
|
|
|
- GPBUtil::TYPE_URL_PREFIX) {
|
|
|
- throw new \Exception(
|
|
|
- "Type url needs to be type.googleapis.com/fully-qulified");
|
|
|
- }
|
|
|
- $fully_qualifed_name =
|
|
|
- substr($this->type_url, $url_prifix_len);
|
|
|
-
|
|
|
- // Create message according to fully qualified name.
|
|
|
- $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
|
|
|
- $desc = $pool->getDescriptorByProtoName($fully_qualifed_name);
|
|
|
- if (is_null($desc)) {
|
|
|
- throw new \Exception("Class ".$fully_qualifed_name
|
|
|
- ." hasn't been added to descriptor pool");
|
|
|
- }
|
|
|
- $klass = $desc->getClass();
|
|
|
- $msg = new $klass();
|
|
|
-
|
|
|
- // Merge data into message.
|
|
|
- $msg->mergeFromString($this->value);
|
|
|
- return $msg;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * The type_url will be created according to the given message’s type and
|
|
|
- * the value is encoded data from the given message..
|
|
|
- * @param message: A proto message.
|
|
|
- */
|
|
|
- public function pack($msg)
|
|
|
- {
|
|
|
- if (!$msg instanceof Message) {
|
|
|
- trigger_error("Given parameter is not a message instance.",
|
|
|
- E_USER_ERROR);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // Set value using serialized message.
|
|
|
- $this->value = $msg->serializeToString();
|
|
|
-
|
|
|
- // Set type url.
|
|
|
- $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
|
|
|
- $desc = $pool->getDescriptorByClassName(get_class($msg));
|
|
|
- $fully_qualifed_name = $desc->getFullName();
|
|
|
- $this->type_url = GPBUtil::TYPE_URL_PREFIX . $fully_qualifed_name;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * This method returns whether the type_url in any_message is corresponded
|
|
|
- * to the given class.
|
|
|
- * @param klass: The fully qualified PHP class name of a proto message type.
|
|
|
- */
|
|
|
- public function is($klass)
|
|
|
- {
|
|
|
- $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
|
|
|
- $desc = $pool->getDescriptorByClassName($klass);
|
|
|
- $fully_qualifed_name = $desc->getFullName();
|
|
|
- $type_url = GPBUtil::TYPE_URL_PREFIX . $fully_qualifed_name;
|
|
|
- return $this->type_url === $type_url;
|
|
|
- }
|
|
|
}
|
|
|
|