-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Currently, the various code generators reinvent their own ways to escape keywords and recase identifiers to fit with local style.
DRY style concerns aside, the manual approach might not be working so well given #7137, #7138, #7139, #7140, and #7141. #7111 discusses overriding our casing conventions which would require touching almost everywhere, but would be easy given centralized naming.
In #7111 (comment) I outline a class that can be used to centralize naming conventions. I think something like:
class CaseManager { // Feel free to bikeshed this name
public:
struct Config {
Case types;
Case constants;
Case methods;
Case functions;
Case fields;
Case variants;
Case namespaces;
Case file_nodes;
// most languages escpae keywords by appending "_" while csharp prepends "@"
std::string keyword_prefix;
std::string keyword_suffix;
std::string object_prefix;
std::string object_suffix;
std::string namespace_seperator;
};
CaseManager(Config config, std::set<std::string> keywords)
: config_(config), keywords_(std::move(keywords)) {}
// Formats `d.name` as a type, then escapes it if its a keyword.
std::string Type(const Definition& d) const;
// Formats `d.name` as a constant, then escapes it if its a keyword.
std::string Constant(const Definition& d) const;
// analogous const methods for Method, Function, Field, Variant, etc
// Formats `d.name` as a type, then adds prefix and suffix, then escapes it if its a keyword.
std::string ObjectType(const Definition& d) const;
private:
Config config_;
std::set<std::string> keywords_;
};I think of it similarly to data access objects (DAOs) except its for applying policies to our identifiers. This would be a pretty substantial change as it raises the abstraction level from "string manipulation code in every code generator" to "configuration per code generator", but I think it will help simplify our systems and make them more maintainable. What do we think?
@aardappel @dbaileychess @vglavnyy @krojew (is there an @all-maintainers?)
TODO:
Convert code generators
- idl_gen_cpp
- idl_gen_csharp
- idl_gen_dart
- idl_gen_go
- idl_gen_grpc
- idl_gen_java
- idl_gen_kotlin #7198
- idl_gen_lobster
- idl_gen_lua
- idl_gen_php
- idl_gen_python
- idl_gen_rust
- idl_gen_swift
- idl_gen_ts
- idl_gen_json_schema
- idl_gen_fbs
- idl_gen_text
other stuff
- Incorporate CommentConfig
- Deprecate BaseGenerator::Namespace