Skip to content

Need more advanced examples #132

@JeffWScott

Description

@JeffWScott

The address book example doesn't cover my structure and I'm not able to figure out the correct way to implement this. Can someone please point me in the right direction?

Here are the structures.

//transaction.capnp
using V = import "values.capnp";
struct TransactionPayload {
    kwargs @6 :V.Map(Text, V.Value);
}

//values.capnp
struct Map(Key, Value) {
  entries @0 :List(Entry);
  struct Entry {
    key @0 :Key;
    value @1 :Value;
  }
}

The result should look like this:

    kwargs = (
      entries = [
        (key = "foo", value = (text = "bar")),
        (key = "foofoo", value =  (text = "barbar"))
      ])

The value portion I can get to work, I cannot for the life of me figure out how to set the Key.

This is probably too much info, but this is the compiled struct javascript:

export class Map_Entry extends __S {
    adoptKey(value) { __S.adopt(value, __S.getPointer(0, this)); }
    disownKey() { return __S.disown(this.getKey()); }
    getKey() { return __S.getPointer(0, this); }
    hasKey() { return !__S.isNull(__S.getPointer(0, this)); }
    setKey(value) { __S.copyFrom(value, __S.getPointer(0, this)); }
    adoptValue(value) { __S.adopt(value, __S.getPointer(1, this)); }
    disownValue() { return __S.disown(this.getValue()); }
    getValue() { return __S.getPointer(1, this); }
    hasValue() { return !__S.isNull(__S.getPointer(1, this)); }
    setValue(value) { __S.copyFrom(value, __S.getPointer(1, this)); }
    toString() { return "Map_Entry_" + super.toString(); }
}
Map_Entry._capnp = { displayName: "Entry", id: "e243243ac908507f", size: new __O(0, 2) };
export class Map extends __S {
    adoptEntries(value) { __S.adopt(value, __S.getPointer(0, this)); }
    disownEntries() { return __S.disown(this.getEntries()); }
    getEntries() { return __S.getList(0, Map._Entries, this); }
    hasEntries() { return !__S.isNull(__S.getPointer(0, this)); }
    initEntries(length) { return __S.initList(0, Map._Entries, length, this); }
    setEntries(value) { __S.copyFrom(value, __S.getPointer(0, this)); }
    toString() { return "Map_" + super.toString(); }
}

Ideally I would just want to do this:

        this.kwargsMap = this.payload.initKwargs();
        this.kwargEntries = this.kwargsMap.initEntries(1)
        this.kwargEntries.get(0).setKey('foo')
        
        let valuePointer = new capnp.Message().initRoot(capnpValues.Value);
        valuePointer.setText('bar');
        this.kwargEntries.get(0).setValue(valuePointer)

The second part does work, the value is set (The values structure exists I just didn't share it),
But the setKey() method throws this error

TypeError: Cannot read property 'isWordZero' of undefined

Which I deduced means it's looking for a pointer object. For the life of me I can't figure out how to create that "Key" pointer.

Any help would be greatly appreciated!

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