zig-protobuf icon indicating copy to clipboard operation
zig-protobuf copied to clipboard

Self referential types in `oneOf` results in error

Open vkcku opened this issue 1 year ago • 1 comments

If there are self referential types in oneOf, then it results in the following error from Zig: struct 'proto.foobar.pb.Node' depends on itself.

Here's an example proto file:

syntax = "proto3";

package foobar;

message Result {
  int32 version = 1;
  Node node = 2;
}

message Node {
  oneof node {
    SubNode sub_node = 1;
    string some_string = 2;
  }
}

message SubNode {
  Node sub = 1;
  string another_string = 2;
}

The corresponding main file:

const std = @import("std");
const protobuf = @import("protobuf");
const proto = @import("./proto/foobar.pb.zig");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    var arena = std.heap.ArenaAllocator.init(gpa.allocator());
    defer arena.deinit();

    const foo = try protobuf.pb_decode(proto.Result, "", arena.allocator());
    _ = foo;
}

vkcku avatar Sep 22 '24 00:09 vkcku

Hello, thanks for your issue.

Zig doesn't allow structures referencing itself, as it tries to calculate the size of the structure at compile time.

I'll look into how we can handle this properly, might have to go through fun pointers with it.

Arwalk avatar Sep 22 '24 07:09 Arwalk

hello @guacs , the branch 69-self-referential-types-in-oneof-resul should have a working solution using a "ManagedStruct" approach (like ManagedString). Would you like to test it?

Sorry for the long time it took to get into it.

Arwalk avatar Feb 27 '25 18:02 Arwalk