rongH chen
rongH chen

Reputation: 11

How to define an array in protobuf?

my server need an array input, like

[
  {
    "name":"ada",
    "age":"20"
  }
]

and I have no idea to define the protobuf, thank you for your consideration

Upvotes: 1

Views: 2831

Answers (2)

Omkesh Sajjanwar
Omkesh Sajjanwar

Reputation: 863

In my case i have ListProduct api which is returning slice.

Product struct

type Product struct {
     ID int32
     Name String 
     Price int32
     Category string
}

ListProduct Resp

[]Product

Answer : product.proto file

syntax = "proto3";
package product;
option go_package = "product/grpc";
message Product {
    int32 id = 1;
    string name = 2; 
    int32 price = 3;
    int32 category = 4;
    
}
message ListProductResponse{ repeated Product products = 1; }

If you want map[int32]Product

message ListProductResponse{ map< int32, Product> products = 1; }

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064044

Arrays aren't a concept in protobuf; there is repeated, but how that is interpreted is implementation specific; it could be an array, but it doesn't need to be; as for the .proto:

syntax = "proto3";
message SomeWrapper {
    repeated SomeInner items = 1;
}
message SomeInner {
    string name = 1;
    int32 age = 2;
}

Upvotes: 4

Related Questions