如何将多个键值添加到Protobuf?

时间:2016-01-26 15:20:01

标签: node.js protocol-buffers

我的goptions reset=all cback=white border htext=10pt htitle=12pt; ods html nogtitle; *ods html gtitle; /* Create data to plot */ data samples; input year mn n k; datalines; 2008 0.19 45 10.6 2009 0.25 54 9.2 2010 0.52 35 11.0 2011 0.15 48 7.2 2012 0.38 29 8.1 ; run; /* Create a format to display the data values with percent signs */ proc format; picture pctfmt low - high = '009.9%'; run; /* Define the title */ title1 "Sample Analysis"; /* The FOOTNOTE statement creates a legend */ footnote1 height=9pt 'N=Nitrogen K=Potassium Mn=Manganese'; /* Create the AXIS definitions that draw a thick border around the */ /* plot area and display multiple scales on the vertical axes */ axis1 order=(2008 to 2012 by 1) value=(font='Arial/bold' height=11pt) offset=(4,4) width=2 label=none major=none minor=none; axis2 order=(0 to .6 by .2, 3, 6 to 12 by 2, 13, 15 to 60 by 15) label=(angle=90 'Concentration') value=(tick=5 ' ' tick=10 ' ') offset=(2,2); axis3 order=(0 to .6 by .2, 3, 6 to 12 by 2, 13, 15 to 60 by 15) label=none value=(tick=5 ' ' tick=10 ' ') offset=(2,2); /* Define the symbol characteristics */ /* SYMBOL1-SYMBOL3 draw a dot at each point */ /* and connect the points with a line. */ symbol1 interpol=join width=2 color=vligb value=dot height=6; symbol2 interpol=join width=2 color=salmon value=dot height=6; symbol3 interpol=join width=2 color=vibg value=dot height=6; /* SYMBOL4-SYMBOL6 are used with the PLOT2 statement and display the */ /* character symbol of the corresponding element for each point */ symbol4 interpol=none value='Mn' font='Arial/bold' color=black height=12pt; symbol5 interpol=none value='K' font='Arial/bold' color=black height=12pt; symbol6 interpol=none value='N' font='Arial/bold' color=black height=12pt; /* Generate the graph */ proc gplot data=samples; plot (mn k n)*year / overlay haxis=axis1 vaxis=axis2 vref=3 13 cframe=grayee; plot2 (mn k n)*year / overlay vaxis=axis3; format mn k n pctfmt.; run; quit;

example.proto

我正在使用的图书馆:https://github.com/dcodeIO/ProtoBuf.js/

我的代码:

message Message {
    repeated string text = 1;
}

输出:

var ProtoBuf = require("protobufjs");

var builder = ProtoBuf.loadProtoFile("example.proto"),
    Message = builder.build("Message");



 var myMessage = new Message({
     text: 'I like muffins and cupcakes1',
     text: 'I like muffins and cupcakes2',
 });


 var byteBuffer = myMessage.encode();

 console.log(byteBuffer);

 var decodedMsg = Message.decode(byteBuffer);

 console.log(decodedMsg);

一切正常,但重复的字符串没有重复。它只抓取最后{ buffer: <Buffer 0a 1c 49 20 6c 69 6b 65 20 6d 75 66 66 69 6e 73 20 61 6e 64 20 63 75 70 63 61 6b 65 73 32 00 00>, offset: 0, markedOffset: -1, limit: 30, littleEndian: false, noAssert: false } { text: [ 'I like muffins and cupcakes2' ] } 个键值。我正在尝试存储多个密钥,以便稍后可以动态地向该对象添加另一个密钥。这在Protobuf中是否可行,或者我是否采取了错误的方式?

1 个答案:

答案 0 :(得分:0)

var myMessage = new Message({
  text: 'I like muffins and cupcakes1',
  text: 'I like muffins and cupcakes2',
});

在这里,您已指定了一个Javascript对象,其中包含两个名为text的字段。事实证明,由于某种原因,这不被视为错误,但Javascript将很乐意抛弃第一个值并仅保留第二个值。您可以在浏览器Javascript控制台中测试:

> console.log({x: 2, x: 3})
Object {x: 3}

Yay Javascript。

我还没有亲自使用过ProtoBuf.js,但我的猜测是它希望将重复的字段表示为列表。在这种情况下,这应该工作:

var myMessage = new Message({
  text: [ 'I like muffins and cupcakes1',
          'I like muffins and cupcakes2' ]
});