在 PHP 中创建交易过滤器以创建 GetTransactionsRequest 对象
Creating a Transaction Filter in PHP for creating a GetTransactionsRequest object
我正在编写一个向 DA 分类帐发出请求的客户端。我遵循我在之前 post .
中收到的建议
我需要 运行 'GetTransactions' rpc。这样做需要 GetTransactionsRequest 对象。 GetTransactionsRequest 对象有一个名为 'filter' 的必需 属性,它是 TransactionFilter 类型。我在创建交易过滤器来满足我的需求时遇到问题。它的 .proto 文件是:
// Used for filtering Transaction and Active Contract Set streams.
// Determines which on-ledger events will be served to the client.
message TransactionFilter {
// Keys of the map determine which parties' on-ledger transactions are being queried.
// Values of the map determine which events are disclosed in the stream per party.
// At the minimum, a party needs to set an empty Filters message to receive any events.
// Required
map<string, Filters> filters_by_party = 1;
}
'filters_by_party' 的唯一字段是必需的。
在 php 中设置此字段需要以下函数:
/**
* Keys of the map determine which parties' on-ledger transactions are being queried.
* Values of the map determine which events are disclosed in the stream per party.
* At the minimum, a party needs to set an empty Filters message to receive any events.
* Required
*
* Generated from protobuf field <code>map<string, .com.digitalasset.ledger.api.v1.Filters> filters_by_party = 1;</code>
* @param array|\Google\Protobuf\Internal\MapField $var
* @return $this
*/
public function setFiltersByParty($var)
{
$arr = GPBUtil::checkMapField($var, \Google\Protobuf\Internal\GPBType::STRING, \Google\Protobuf\Internal\GPBType::MESSAGE, \Com\Digitalasset\Ledger\Api\V1\Filters::class);
$this->filters_by_party = $arr;
return $this;
}
用于在 mapFiled 对象中设置值的 php 函数是:
/**
* Assign the element at the given key.
*
* This will also be called for: $arr[$key] = $value
*
* @param object $key The key of the element to be fetched.
* @param object $value The element to be assigned.
* @return void
* @throws ErrorException Invalid type for key.
* @throws ErrorException Invalid type for value.
* @throws ErrorException Non-existing key.
*/
public function offsetSet($key, $value)
{
$this->checkKey($this->key_type, $key);
switch ($this->value_type) {
case GPBType::SFIXED32:
case GPBType::SINT32:
case GPBType::INT32:
case GPBType::ENUM:
GPBUtil::checkInt32($value);
break;
case GPBType::FIXED32:
case GPBType::UINT32:
GPBUtil::checkUint32($value);
break;
case GPBType::SFIXED64:
case GPBType::SINT64:
case GPBType::INT64:
GPBUtil::checkInt64($value);
break;
case GPBType::FIXED64:
case GPBType::UINT64:
GPBUtil::checkUint64($value);
break;
case GPBType::FLOAT:
GPBUtil::checkFloat($value);
break;
case GPBType::DOUBLE:
GPBUtil::checkDouble($value);
break;
case GPBType::BOOL:
GPBUtil::checkBool($value);
break;
case GPBType::STRING:
GPBUtil::checkString($value, true);
break;
case GPBType::MESSAGE:
if (is_null($value)) {
trigger_error("Map element cannot be null.", E_USER_ERROR);
}
GPBUtil::checkMessage($value, $this->klass);
break;
default:
break;
}
$this->container[$key] = $value;
}
例如,如何将派对名称 'dealer1' 和 'dealer2' 设置为 filters_by_party 的派对。我尝试了以下代码:
$parties= new Google\Protobuf\Internal\MapField(Google\Protobuf\Internal\GPBType::STRING,Google\Protobuf\Internal\GPBType::MESSAGE);
$parties->offsetSet(0,"dealer1");
$parties->offsetSet(1,"dealer2");
导致以下错误:
PHP Fatal error: Given value is not message. in /home/vantage/damlprojects/loaner_car/php/ledger_client.php on line 85
我不明白为什么 'message'
filter_by_party 'set' 函数需要。我不知道如何将经销商名称写成 'meessage' 的形式。似乎做一件本该简单的事情却很复杂。将输入 $var 设置为 'setFiltersByParty' 函数的正确方法是什么?
也许您可以在这里找到一些信息:
https://developers.google.com/protocol-buffers/docs/reference/php-generated#fields
对于 map 字段,我想应该是这样的:
$m->getFiltersByParty()["string"] = new Filters();
设置 'dealer1' 和 dealer2' 的代码应如下所示:
$parties= new Google\Protobuf\Internal\MapField(Google\Protobuf\Internal\GPBType::STRING,
Google\Protobuf\Internal\GPBType::MESSAGE, "Com\Digitalasset\Ledger\Api\V1\Filters");
$partyIdentifier = new Com\Digitalasset\Ledger\Api\V1\Identifier();
$partyIdentifier->setPackageId($the_package_id); //last one from ListPackages()
$partyIdentifier->setEntityName("<Module>"); //from DAML code
$partyIdentifier->setModuleName("<Template>"); //from DAML code
$partyInclusiveFilters = new Com\Digitalasset\Ledger\Api\V1\InclusiveFilters();
$partyInclusiveFilters->setTemplateIds(array($partyIdentifier));
$partyFilters = new Com\Digitalasset\Ledger\Api\V1\Filters();
$partyFilters->setInclusive($partyInclusiveFilters);
$parties->offsetSet("dealer1",$partyFilters);
$parties->offsetSet("dealer2",$partyFilters);
重要的是将MapField构造函数的第三个参数设置为"Com\Digitalasset\Ledger\Api\V1\Filters"。正如 protobuf 文件所建议的那样:
message TransactionFilter {
// Keys of the map determine which parties' on-ledger transactions are being queried.
// Values of the map determine which events are disclosed in the stream per party.
// At the minimum, a party needs to set an empty Filters message to receive any events.
// Required
map<string, Filters> filters_by_party = 1;
}
filters_by_party 是具有 'Filter' 的 value_type 的 MapField。 'Filter' 是一种消息类型(请参阅您的 transaction_filter.proto 文件),因此 MapField 构造函数需要知道它是什么类型的消息。
经销商名称未以 "message" 的形式写入,而是 'filters_by_party' MapField 中对应 'Filter' 的 key_value目的。 'Filter' 是一种消息。我想 'simple' 是一个相对术语。
我正在编写一个向 DA 分类帐发出请求的客户端。我遵循我在之前 post
中收到的建议
我需要 运行 'GetTransactions' rpc。这样做需要 GetTransactionsRequest 对象。 GetTransactionsRequest 对象有一个名为 'filter' 的必需 属性,它是 TransactionFilter 类型。我在创建交易过滤器来满足我的需求时遇到问题。它的 .proto 文件是:
// Used for filtering Transaction and Active Contract Set streams.
// Determines which on-ledger events will be served to the client.
message TransactionFilter {
// Keys of the map determine which parties' on-ledger transactions are being queried.
// Values of the map determine which events are disclosed in the stream per party.
// At the minimum, a party needs to set an empty Filters message to receive any events.
// Required
map<string, Filters> filters_by_party = 1;
}
'filters_by_party' 的唯一字段是必需的。 在 php 中设置此字段需要以下函数:
/**
* Keys of the map determine which parties' on-ledger transactions are being queried.
* Values of the map determine which events are disclosed in the stream per party.
* At the minimum, a party needs to set an empty Filters message to receive any events.
* Required
*
* Generated from protobuf field <code>map<string, .com.digitalasset.ledger.api.v1.Filters> filters_by_party = 1;</code>
* @param array|\Google\Protobuf\Internal\MapField $var
* @return $this
*/
public function setFiltersByParty($var)
{
$arr = GPBUtil::checkMapField($var, \Google\Protobuf\Internal\GPBType::STRING, \Google\Protobuf\Internal\GPBType::MESSAGE, \Com\Digitalasset\Ledger\Api\V1\Filters::class);
$this->filters_by_party = $arr;
return $this;
}
用于在 mapFiled 对象中设置值的 php 函数是:
/**
* Assign the element at the given key.
*
* This will also be called for: $arr[$key] = $value
*
* @param object $key The key of the element to be fetched.
* @param object $value The element to be assigned.
* @return void
* @throws ErrorException Invalid type for key.
* @throws ErrorException Invalid type for value.
* @throws ErrorException Non-existing key.
*/
public function offsetSet($key, $value)
{
$this->checkKey($this->key_type, $key);
switch ($this->value_type) {
case GPBType::SFIXED32:
case GPBType::SINT32:
case GPBType::INT32:
case GPBType::ENUM:
GPBUtil::checkInt32($value);
break;
case GPBType::FIXED32:
case GPBType::UINT32:
GPBUtil::checkUint32($value);
break;
case GPBType::SFIXED64:
case GPBType::SINT64:
case GPBType::INT64:
GPBUtil::checkInt64($value);
break;
case GPBType::FIXED64:
case GPBType::UINT64:
GPBUtil::checkUint64($value);
break;
case GPBType::FLOAT:
GPBUtil::checkFloat($value);
break;
case GPBType::DOUBLE:
GPBUtil::checkDouble($value);
break;
case GPBType::BOOL:
GPBUtil::checkBool($value);
break;
case GPBType::STRING:
GPBUtil::checkString($value, true);
break;
case GPBType::MESSAGE:
if (is_null($value)) {
trigger_error("Map element cannot be null.", E_USER_ERROR);
}
GPBUtil::checkMessage($value, $this->klass);
break;
default:
break;
}
$this->container[$key] = $value;
}
例如,如何将派对名称 'dealer1' 和 'dealer2' 设置为 filters_by_party 的派对。我尝试了以下代码:
$parties= new Google\Protobuf\Internal\MapField(Google\Protobuf\Internal\GPBType::STRING,Google\Protobuf\Internal\GPBType::MESSAGE);
$parties->offsetSet(0,"dealer1");
$parties->offsetSet(1,"dealer2");
导致以下错误:
PHP Fatal error: Given value is not message. in /home/vantage/damlprojects/loaner_car/php/ledger_client.php on line 85
我不明白为什么 'message' filter_by_party 'set' 函数需要。我不知道如何将经销商名称写成 'meessage' 的形式。似乎做一件本该简单的事情却很复杂。将输入 $var 设置为 'setFiltersByParty' 函数的正确方法是什么?
也许您可以在这里找到一些信息: https://developers.google.com/protocol-buffers/docs/reference/php-generated#fields
对于 map 字段,我想应该是这样的:
$m->getFiltersByParty()["string"] = new Filters();
设置 'dealer1' 和 dealer2' 的代码应如下所示:
$parties= new Google\Protobuf\Internal\MapField(Google\Protobuf\Internal\GPBType::STRING,
Google\Protobuf\Internal\GPBType::MESSAGE, "Com\Digitalasset\Ledger\Api\V1\Filters");
$partyIdentifier = new Com\Digitalasset\Ledger\Api\V1\Identifier();
$partyIdentifier->setPackageId($the_package_id); //last one from ListPackages()
$partyIdentifier->setEntityName("<Module>"); //from DAML code
$partyIdentifier->setModuleName("<Template>"); //from DAML code
$partyInclusiveFilters = new Com\Digitalasset\Ledger\Api\V1\InclusiveFilters();
$partyInclusiveFilters->setTemplateIds(array($partyIdentifier));
$partyFilters = new Com\Digitalasset\Ledger\Api\V1\Filters();
$partyFilters->setInclusive($partyInclusiveFilters);
$parties->offsetSet("dealer1",$partyFilters);
$parties->offsetSet("dealer2",$partyFilters);
重要的是将MapField构造函数的第三个参数设置为"Com\Digitalasset\Ledger\Api\V1\Filters"。正如 protobuf 文件所建议的那样:
message TransactionFilter {
// Keys of the map determine which parties' on-ledger transactions are being queried.
// Values of the map determine which events are disclosed in the stream per party.
// At the minimum, a party needs to set an empty Filters message to receive any events.
// Required
map<string, Filters> filters_by_party = 1;
}
filters_by_party 是具有 'Filter' 的 value_type 的 MapField。 'Filter' 是一种消息类型(请参阅您的 transaction_filter.proto 文件),因此 MapField 构造函数需要知道它是什么类型的消息。
经销商名称未以 "message" 的形式写入,而是 'filters_by_party' MapField 中对应 'Filter' 的 key_value目的。 'Filter' 是一种消息。我想 'simple' 是一个相对术语。