2

How do I define a parentS <-> childrenS relationship in proto

syntax = "proto3";

message Root {
     repeated Category category = 2;  
}

message Category {
     string name = 2;
     repeated Category parent = 3; 
}

The key here is that I want to be able to summon the children

MamyCategoryInstance
|
|--- FooCategoryInstance
...//

PapaCategoryInstance
|
|---- FooCategoryInstance
|---- BarCategoryInstance

Thank you

MUH Mobile Inc.
  • 1,462
  • 1
  • 16
  • 25

1 Answers1

2

You cannot. Protocol buffers is a "tree"-based serializer, not a "graph"-based serializer. As such, objects only have a single semantic parent, and this is implicit - not explicit. Meaning:

message Root {
     repeated Category category = 2;  
}

message Category {
     string name = 2;
}

The parent of each category is simply: which node was above it in the tree. If you try to create an explicit parent relationship, serialization will fail due to recursion.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900