Skip to content

How to use the AnyPointer type? #120

@jonathan-roy

Description

@jonathan-roy

Hi,

I have a Request struct with the following schema:

struct Request {
	timestamp @0: UInt64;
	payload @1: AnyPointer;
}

The payload can either be a capnp.Data struct filled from a Node.js Buffer, or another user-defined struct (let's say a Todo struct for example). How am I supposed to create a message with such payload?

I tried the following:

const msg = new capnp.Message();
const req = msg.initRoot(Request);
req.setTimestamp(capnp.Uint64.fromNumber(0));

// With a capnp.Data struct:
const payload = capnp.Struct.initStructAt(0, capnp.Data, req);
payload.copyBuffer(Buffer.from("coucou"));
req.setPayload(payload);
// or with a Todo struct:
const payload = capnp.Struct.initStrutAt(0, Todo, req);
payload.setText("Masterize capnp-ts");
payload.setDone(false);
res.setPayload(payload);

The payload is transmitted but the timestamp field seems corrupted when I set the payload. So, what is the correct way to fill this AnyPointer payload field?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions