Search code examples
capnproto

Is it possible to extend a tagged union with more variants in Cap'n proto while being binary-compatible with any old data?


I'm looking into protocol schema languages, and it seems like Cap'n'proto will suit my needs, but there's one critical feature I need which I cannot find in the docs:

Let's say I have this tagged union with two struct members in C-like syntax:

struct taggedUnion {
    int tag;
    union {
        struct a {
           int x;
        }

        struct b {
            float x;
        }
    }
}

Can I then in the future add another struct to the tagged union, while still being able to read the old data?

struct taggedUnion {
    int tag;
    union {
        struct a {
           int x;
        }

        struct b {
            float y;
        }

        struct c {
            int z;
            bool b;
        }
    }
}

It feels like it should be doable, but I can't find anything in the docs saying that it is. There's a note on groups being extensible without breaking wire-compatibility (new fields are zeroed out for old data).

If it's possible, how would I declare this change in cap'n proto schema syntax? A before/after example would be great!


Solution

  • Found it. ... new fields may be added to existing groups and unions seems like it could be an answer to this question.