Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define cycles in protobuf?

say i have the following mode.proto file:

message EntityD {
    optional EntityE ePointer = 1;
    optional int32 dField = 2;
}

message EntityE {
    optional EntityD dPointer = 1;
    optional int32 eField = 2;
}

this has 2 entities - D and E, which allow cross-linking. my issue is that after generating java code from the above *.proto im unable to create a cycle:

public static void main(String[] args) throws Exception {
    Model.EntityD.Builder dBuilder = Model.EntityD.newBuilder();
    Model.EntityE.Builder eBuilder = Model.EntityE.newBuilder();

    dBuilder.setDField(42);
    eBuilder.setEField(7);
    dBuilder.setEPointer(eBuilder);
    eBuilder.setDPointer(dBuilder);

    Model.EntityD d = dBuilder.build();
    Model.EntityE e = eBuilder.build();

    System.err.println("same d: "+(d==e.getDPointer()));
    System.err.println("same e: "+(e==d.getEPointer()));
}

im trying to create a simple D <--> E cycle. instead i get this:

same d: false 
same e: false

enter image description here

there IS a cycle in the created model, but only starting with a certain depth.

is this an issue with probuf-generated java code? does protobuf officially support cycles in the graph ? whats the expected result for this in other protobuf "output" languages? (namely c++)

like image 706
radai Avatar asked Mar 19 '23 11:03

radai


1 Answers

The protobuf format does not include object identity, and there is no notion of reference / graph support. It is a basic tree only. Anything beyond that would be implementation specific and not part of the specification.

Basically, don't do that.

This is not uncommon: the same is true of many serializers, including XML and json serializers. And for these cases, again, some implementations may have support for a local definition of identity - but it is not guaranteed in the general case.

like image 135
Marc Gravell Avatar answered Mar 29 '23 12:03

Marc Gravell