facebook / fbthrift

Facebook's branch of Apache Thrift, including a new C++ server.
Apache License 2.0
2.55k stars 608 forks source link

feature-request: compile-time name to id function #518

Closed alex-700 closed 1 year ago

alex-700 commented 1 year ago

Hello, fbthrift team!

During integration with fbthrift in one of our inhouse projects, we need to have compile-time function from field name (e.g. std::string_view) to field id. It looks that it can not be done (without fatal) in the current version of fbthrift.

@vitaut removed constexpr from arrays in commit, to improve build times. If behavior of the code before this commit could be used (under some program flag), the required compile-time function can be easily implemented. Or maybe fbthrift could deliver some other way of having this required function.

Thanks!

vitaut commented 1 year ago

I think you should be able to use Thrift reflection for this.

alex-700 commented 1 year ago

IIUC Thrift reflection depends on fatal. Currently, we do not depend on fatal, and want to continue to not depend on it.

vitaut commented 1 year ago

Could you elaborate why you want to avoid fatal?

alex-700 commented 1 year ago

We are using specific custom method of delivery dependencies, which are OSS parts of Meta-monolith, to our codebase. It is hard-maintainable, and adding a new OSS part of Meta-monolith as a dependency costs a lot.

vitaut commented 1 year ago

I don't think we should be adding another API just to avoid a dependency. However, you can use the thrift compiler to emit this constexpr mapping yourself.

alex-700 commented 1 year ago

However, you can use the thrift compiler to emit this constexpr mapping yourself.

What do you mean? Does fbthrift compiler have plugin system? Or do you mean patching of compiler?

vitaut commented 1 year ago

The compiler is available as a library: https://github.com/facebook/fbthrift/blob/main/thrift/compiler/compiler.h. So you can parse .thrift file(s), iterate over all the structs in AST and generate header(s) containing constexpr name to field ID mappings.