仅从原型中提取重复的场元素

syntax = "proto3";


package TestServer;


service RelaySrv{

    rpc UpdateGroupDetails (Group) returns (Response);

}


message Person

{

    int64 id = 1;

    string name = 2;

}


message Group{


    repeated Person persons = 1;

}


Go code:

    var buf bytes.Buffer

    m := jsonpb.Marshaler{}

    err := m.Marshal(&buf, Group)


在组 protobuf 消息 buf 上执行封送后,buf 变量将具有:


{ “persons” : [{“id”:“1”,“name”:“sun”}, {“id”:“2”,“name”:“sam”}] }


如何提取


[{“id”:“1”,“name”:“sun”}, {“id”:“2”,“name”:“sam”}]


从 buf 没有清空它??


慕桂英3389331
浏览 107回答 2
2回答

烙印99

如果我理解正确,这就是你想要的。    const b = `[{"id":1,"Name":"sun"}, {"id":2,"Name":"sam"}]`    persons := []*pb.Person{}    err := json.Unmarshal([]byte(b), &persons)    if err != nil {        panic(err.Error())    }    log.Println(persons)    // 2021/03/06 22:34:15 [id:1 name:"sun"  id:2 name:"sam" ]

狐的传说

不确定是否有更好的方法,但它没有封送到json结构中,而是使用编码/解码到json。ingest.stream不再抱怨了,我可以在azure-data-explorer中看到数据&nbsp; &nbsp; //have a json struct for Group and Person to match the protbuf message posted in the question&nbsp; &nbsp; var g Group&nbsp; &nbsp; json.NewDecoder(&buf).Decode(&g)&nbsp; &nbsp; var b bytes.Buffer&nbsp; &nbsp; for i := 0; i < len(g.Persons); i++ {&nbsp; &nbsp; &nbsp; &nbsp; e := json.NewEncoder(&b).Encode(&g.Persons[i])&nbsp; &nbsp; &nbsp; &nbsp; if e != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic("issue marshalling protobuf")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go