How to read capnp schema const value at runtime using SchemaParser?

151 Views Asked by At

given the following dummy schema test.capnp:

struct Person {
  const type :Text = "person.type.example";
  version @0 :UInt32;
  name @1 :Text;
}

At runtime, how can we read the default const value from a schema struct (i.e the "person.type.example" above? type does not show up as a field:

std::string schema_filename = "test.capnp";
kj::ArrayPtr<const kj::StringPtr> import_paths;
capnp::SchemaParser parser;
capnp::ParsedSchema schema = parser.parseDiskFile(
    schema_filename.c_str(), schema_filename.c_str(), import_paths);

auto person_maybe = schema.findNested("Person");
auto* person_schema = kj::_::readMaybe(person_maybe);

// how can I read "person.type.example" out of the Person struct?
1

There are 1 best solutions below

0
On

We need to go down a nested level with getNexted and then reinterpret the const type as text:

std::string type = person_schema->getNested("type").asConst().as<capnp::Text>().cStr();