Working with a message having repeated field

protobuf iterate over repeated field c++
protobuf repeated field
protobuf repeated field example c#
protobuf parsefromstring
protobuf unknown fields
protobuf/any
proto3 default value
jspb message

My Proto file looks like this -

message Todos {
  repeated Todo todos = 1;
}

message Todo {
  int32 id = 1;
  string name = 2;
  bool done = 3;
}

It comes works fine when I send {todos: [...]} from the server, but gets an empty object {} when directly sending an array.

Server

getAll(_, callback) {
   console.log(todos);
   return callback(null, { todos });
}

Client

 client.getAll({}, function (err, res) {
    if (err) {
      return console.log(err);
    }
    console.log('todos: ');
    return console.log(res);
 });

Versions -

  • @grpc/proto-loader - ^0.1.0
  • grpc - ^1.13.0

In my case I was trying to return an array and it seems you always have to return an object....

hero.proto

syntax = "proto3";

package hero;

service HeroService {
  rpc GetHeroById(HeroById) returns (Hero) {}
  rpc ListHeroesById(HeroById) returns (HeroList) {}
}

message HeroById {
  int32 id = 1;
}

message Hero {
  int32 id = 1;
  string name = 2;
}

message HeroList {
  repeated Hero heroes = 1;
}

hero.controller.ts

@GrpcMethod('HeroService')
listHeroesById(data: HeroById, metadata: any): object {
  const items = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Doe' },
    { id: 3, name: 'Billy' },
  ];
  // make sure you return an object, even if you want an array!
  return { heroes: items.filter(({ id }) => id === data.id) };
}

Check out my example TypeScript project here:

https://github.com/kmturley/angular-nest-grpc

Protocol Buffer Basics: C++ | Protocol Buffers, This tutorial provides a basic C++ programmer's introduction to working with protocol buffers. Use the C++ protocol buffer API to write and read messages. Repeated fields also have some special methods – if you look at the methods for  Authors simply double-click the text field to open the Text Form Field Options dialog, enter the text they want to repeat in the "Default text" box, and click OK.

If I understand correctly, you are having problems if you send just the array todos, instead of an object containing that array. Sending just the array is simply an invalid use of the API. Protobuf services always send protobuf messages, so you have to pass an actual message object, and not a single field of that object.

Protocol Buffer Basics: Java | Protocol Buffers, This tutorial provides a basic Java programmer's introduction to working with protocol buffers. Use the Java protocol buffer API to write and read messages. Repeated fields have some extra methods – a Count method (which is just  RepeatedField is used to represent repeated fields of a primitive type (in other words, everything except strings and nested Messages). Most users will not ever use a RepeatedField directly; they

If you use grpc.load then you can send back an array:

callback(null, todos);

If you use protoLoader.loadSync and grpc.loadPackageDefinition, then you need to send back:

callback(null, { todos: todos });

Working with messages, As Protocol Buffers is a data interchange format, messages and message However, writing a repeated field or submessage will always create (append) a new  Our users want the project title to repeat on subsequent pages but they want the logo on page 1 only. There are several fields that will expand depending on how much information is keyed because the users don't want a limit on the number of characters that can be entered, so the location of this field may not be on the top of the form.

Annual Reports of the Department of the Interior [with , N 0 response having been made, the surveys were formally rejected and the the director transmitted to this oflice duplicate transcripts of the field notes and an opportunity to revise and correct the work before a final rejection was ordered. Thus, you will avoid mistakes in this repeated data and save your time! To create a document with fields for repeated information, do the following: 1. Create a bookmark. 1.1. Select the text that should be repeated. Instead of the text, you can select a place where you would like to add some text in future.

The Assembly Herald, In all the proEvangelist Nakamura and his wife who have vince of nearly a is there so inviting a field as ian Church shall know that with all the noble Toyama . that they are sent to year we have repeated the request for suffi - their work . the message of the Mayor of Sendai to a special account of his work at Sendai . repeated: the field may be repeated any number of times (including zero). The order of the repeated values will be preserved in the protocol buffer. Think of repeated fields as dynamically sized

Efficiently filter repeated message fields in python · Issue #4396 , I have a repeated message field and I would like to remove certain all approaches I can think of either don't work or are not optimal (involv Simply copy the text you want repeated and paste it in the repeated location use menu sequence Edit>Paste Special. Paste as Formatted or Unformatted text and select the radio button "Paste link." This automatically creates a bookmark at the source location and creates a Link field at the destination locations. 5.

Comments
  • Are you saying that you're getting different results if you pass {todos} instead of {todos: todos}, or is there some other difference between the two situations? Is todos an empty array, and if so have you tried following the suggestions in this GitHub issue?
  • @murgatroid99 Thanks for the suggestion, but I've checked my server and todos is an non-empty array. When sending { todos } or { todos : todos }, my client gets an object. But when I send todos ( as a direct array ) it gets an empty Object.
  • Can this be because of how I've define the proto?
  • This is important information here. To return the array only, it must be return { heroes }; Thanks @Kim
  • Awesome! I figured that out later. Thanks @murgatroid99 for the help.