How do I serialize/deserialize part of a message?

37 views
Skip to first unread message

Filip Haglund

unread,
May 6, 2020, 5:37:57 PM5/6/20
to Cap'n Proto
I have an api that speaks capnp. I want to store a subset of the incoming message (a specific field) in a db and pass it onwards inside other messages later on. How can I do this?

Here's a rough sketch of events in order:

1. incoming request:

struct IncomingRequest {
    dbid @0 :Text;
    msg @1 :Msg;
}

2. store that `Msg` field in a db

3. read the `Msg` field back from the db and wrap it in this type and send it on:

struct OutgoingRequest {
    action @0 :Text;
    critical @1 :Bool;
    msg @2 :Msg;
}

I'm using the go v2 and d libraries, in case that matters.

Filip Haglund

unread,
May 7, 2020, 4:52:57 AM5/7/20
to Cap'n Proto
Is the pragmatic approach to store one capnp msg inside a `Data` field of another here?

Kenton Varda

unread,
May 7, 2020, 11:49:05 AM5/7/20
to Filip Haglund, Cap'n Proto
Hi Filip,

You can copy a struct from one message to another like:

    outgoingRequestBuilder.setMsg(incomingRequestReader.getMsg());

There isn't a way to do this without a copy, since the contents of the specific struct may be interleaved with other data in the message.

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/39efb5e7-f91b-4156-b5ed-5320fa8c90c2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages