facebook/fbthrift

feature-request: compile-time name to id function

alex-700 opened this issue · 7 comments

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!

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

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

Could you elaborate why you want to avoid fatal?

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.

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.

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?

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.