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
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++)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With