0

I am migrating schemas from proto2 to proto3 syntax. I want to eliminate extensions as they are not supported. Is it possible to get an object using a field name in proto3, similar to what MutableExtension does in proto2.

For example,

Schema in proto2 syntax

message Foo {
  message Bar {
    unint32 a = 1;
  }
  extend Foo {
    Bar b = 1;
  }
}

C++

Foo::Bar b_val = foo.MutableExtension(Foo::b);

Now in proto3, I could do this:

syntax="proto3";
message Foo {
  message Bar {
    unint32 a = 1;
  }
  Bar b = 1;
}

C++ code:

Foo::Bar b_val = foo.mutable_b();

However, I want to use the name Foo::b to get a Foo::Bar object. Is there a way to do this?

Mihir
  • 3
  • 2
  • Could you perhaps explain why the obvious `Foo::Bar b_val = foo.b();` is not an option here? –  Nov 11 '21 at 03:19
  • The reason I am trying to do this is because I am working on a very large codebase and there's a little templated function which can return fieldnames, such as, Foo:a, Foo::b, etc. The current code using proto2 can use MutableExtension(fun()), but I could not find a good way to avoid hundreds of if-else's in proto3. – Mihir Nov 11 '21 at 04:34

1 Answers1

0

It's not clear why you need this but what you are asking for is kinda feasible.

Foo::b is a garden variety member function, which means that &Foo::b is a regular pointer-to-member-function.

So you can use it as such using the regular c++ syntax for these entities:

auto b_ref = &Foo::b;

Foo::Bar b_val = (foo.*b_ref)();
  • I am getting an error in the first line using C++11: `error: call to non-static member function without an object argument` – Mihir Nov 11 '21 at 04:43
  • @Mihir Sorry, there was a typo. It's fixed now. –  Nov 11 '21 at 04:45