簡體   English   中英

試圖在 Typescript 中設置 protobuf Map 條目

[英]Trying to set protobuf Map entries in Typescript

我有以下 protobuf 消息:

syntax = "proto3";

message MyMessage {
  string id = 1;
  map<string, Any> attributeChanges = 2;
}

在 typescript 中,我試圖根據 typescript Z46F3EA056CAA31026CZF1 設置 protobuf 消息的 map 條目,看起來像這樣。

type Nullable<T> = T | undefined | null;

export interface IAuditChanges {
  currentValue: Nullable<string>;
  previousValue: Nullable<string>;
  fields?: Map<string, IAuditChanges>
}

我嘗試遍歷 map object 中的條目並以這種方式設置 map 條目。

var attributesMap = message.getAttributechangesMap();
for (let key of Array.from(mappedChanges.keys())) {
  const mapEntry: any = mappedChanges.get(key);
  attributesMap.set(key, mapEntry);
}

我收到錯誤“TypeError:b.toArray 不是 function。” 知道如何轉換/序列化/打包每個 typescript map 條目以便我在 protobuf 中設置它嗎? 我在幾個地方找了一些沒有運氣的例子。

https://developers.google.com/protocol-buffers/docs/proto3#anyhttps://developers.google.com/protocol-buffers/docs/proto3#maps

謝謝你。

Protobuf Any與 TypeScript any非常不同。 TypeScript any允許任何可能的 JavaScript 值。

另一方面,Protobuf Any是一條包含兩個字段的消息:

  1. value:一個protobuf消息,以二進制格式編碼

  2. type_url:protobuf 消息類型的標識符

這意味着您不能只在Any字段中放置任何內容。 它必須是一個 protobuf 消息,打包到一個 protobuf Any中。

JavaScript 參考實際上有一個例子: https://developers.google.com/protocol-buffers/docs/reference/javascript-generated#any

我建議為“IAuditChanges”編寫一個 protobuf 消息。 您很可能根本不需要Any

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM