Re: [capnproto] Dynamically reading schema const property at runtime (c++)

2022-06-08 Thread Daniel Koohmarey
Appreciate the reply Kenton, that got me to the solution I was looking for:

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

And will do on avoiding kj::_. Thanks again!


On Wednesday, June 8, 2022 at 11:13:56 AM UTC-7 ken...@cloudflare.com wrote:

> Hi Daniel,
>
> I think you can do:
>
> capnp::ConstSchema type = 
> schema.getNested("Person").getNested("type").asConst();
>
> But it's been a long time since I touched this.
>
> By the way, please do not use stuff in kj::_ directly, that is a private 
> namespace. To read a Maybe, use KJ_IF_MAYBE, KJ_ASSERT_NONNULL, 
> KJ_UNWRAP_OR, etc.
>
> -Kenton
>
> On Wed, Jun 8, 2022 at 12:33 PM Daniel Koohmarey <
> daniel.k...@braincorp.com> wrote:
>
>> 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 is not considered a field 
>> within the struct, but it does appear as a node in the proto):
>> std::string schema_filename = "test.capnp"; kj::ArrayPtr> 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?
>>
>> Any guidance on the matter is appreciated, thanks!
>>
>> -- 
>> 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/389bc2ea-3279-4ddc-b8cc-f554d636767fn%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/43e19d86-1979-4619-9e96-d8659acc07c5n%40googlegroups.com.


Re: [capnproto] Dynamically reading schema const property at runtime (c++)

2022-06-08 Thread 'Kenton Varda' via Cap'n Proto
Hi Daniel,

I think you can do:

capnp::ConstSchema type =
schema.getNested("Person").getNested("type").asConst();

But it's been a long time since I touched this.

By the way, please do not use stuff in kj::_ directly, that is a private
namespace. To read a Maybe, use KJ_IF_MAYBE, KJ_ASSERT_NONNULL,
KJ_UNWRAP_OR, etc.

-Kenton

On Wed, Jun 8, 2022 at 12:33 PM Daniel Koohmarey <
daniel.koohma...@braincorp.com> wrote:

> 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 is not considered a field
> within the struct, but it does appear as a node in the proto):
> std::string schema_filename = "test.capnp"; kj::ArrayPtr 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?
>
> Any guidance on the matter is appreciated, thanks!
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/capnproto/389bc2ea-3279-4ddc-b8cc-f554d636767fn%40googlegroups.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/CAJouXQ%3DUkTR-6f6LsPMPKhK3QhW9TDcTUR1DcrV6c5zfqSZP_Q%40mail.gmail.com.


[capnproto] Dynamically reading schema const property at runtime (c++)

2022-06-08 Thread Daniel Koohmarey


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 is not considered a field 
within the struct, but it does appear as a node in the proto):
std::string schema_filename = "test.capnp"; kj::ArrayPtr 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?

Any guidance on the matter is appreciated, thanks!

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/389bc2ea-3279-4ddc-b8cc-f554d636767fn%40googlegroups.com.