Skip to content

Commit

Permalink
chore(idl): Check for case insensitive keywords
Browse files Browse the repository at this point in the history
Most languages are not affected by this change. In PHP, some names such
as Bool cannot be used because it is a reserved keyword for to the bool
data type. The field `keywords_casing` in the configs enables checking
all characters in lowercase against every keyword. This should be safe
as flatbuffers does not allow non-ASCII characters in its grammar.
  • Loading branch information
Razvan Smadu authored and razvanalex committed Oct 8, 2024
1 parent 2f59a03 commit 397926d
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 1 deletion.
17 changes: 16 additions & 1 deletion include/codegen/namer.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ class Namer {
std::string keyword_prefix;
// Suffix used to escape keywords. It is usually "_".
std::string keyword_suffix;
// The casing used for keywords when escaping. For most languages, keywords
// are case sensitive. PHP is an instance where some keywords are case
// insensitive.
enum KeywordsCasing {
CASE_SENSITIVE,
CASE_INSENSITIVE,
};
KeywordsCasing keywords_casing;

// Files.

Expand Down Expand Up @@ -205,7 +213,14 @@ class Namer {
}

virtual std::string EscapeKeyword(const std::string &name) const {
if (keywords_.find(name) == keywords_.end()) {
std::string cased_name(name);

if (config_.keywords_casing == Config::KeywordsCasing::CASE_INSENSITIVE) {
for (auto chr = cased_name.begin(); chr != cased_name.end(); chr++) {
*chr = CharToLower(*chr);
}
}
if (keywords_.find(cased_name) == keywords_.end()) {
return name;
} else {
return config_.keyword_prefix + name + config_.keyword_suffix;
Expand Down
2 changes: 2 additions & 0 deletions include/codegen/python.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static const Namer::Config kConfig = {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand All @@ -49,6 +50,7 @@ static const Namer::Config kStubConfig = {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/bfbs_gen_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Namer::Config LuaDefaultConfig() {
/*object_suffix=*/"",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/bfbs_gen_nim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Namer::Config NimDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/idl_gen_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static Namer::Config DartDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"$",
/*keyword_suffix=*/"",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/idl_gen_go.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static Namer::Config GoDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/idl_gen_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static Namer::Config JavaDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/idl_gen_kotlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static Namer::Config KotlinDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/idl_gen_kotlin_kmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static Namer::Config KotlinDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"E",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kUpperCamel,
/*directories=*/Case::kLowerCamel,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/idl_gen_rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static Namer::Config RustDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kSnake,
/*directories=*/Case::kSnake,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/idl_gen_swift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static Namer::Config SwiftDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
1 change: 1 addition & 0 deletions src/idl_gen_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Namer::Config TypeScriptDefaultConfig() {
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*keywords_casing=*/Namer::Config::KeywordsCasing::CASE_SENSITIVE,
/*filenames=*/Case::kDasher,
/*directories=*/Case::kDasher,
/*output_path=*/"",
Expand Down

0 comments on commit 397926d

Please sign in to comment.