Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/codegen/idl_namer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ class IdlNamer : public Namer {
return ConvertCase(d.name, config_.fields, Case::kLowerCamel) + "_type";
}

// Php method for structs are a mix of keep case and lower camel case.
std::string LegacyPhpMethod(const StructDef& d,
const std::string& suffix) const {
return Type(d.name) + suffix;
}
std::string LegacyPhpMethod(const std::string& prefix,
const StructDef& d) const {
return prefix + Type(d.name);
}
std::string LegacyPhpField(const std::string& prefix,
const FieldDef& field) const {
return prefix + ConvertCase(field.name, Case::kUpperCamel);
}

private:
std::string NamespacedString(const struct Namespace* ns,
const std::string& str) const {
Expand Down
18 changes: 17 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 class KeywordsCasing {
CaseSensitive,
CaseInsensitive,
};
KeywordsCasing keywords_casing;

// Files.

Expand Down Expand Up @@ -204,8 +212,16 @@ class Namer {
return result;
}

virtual std::string NormalizeKeywordCase(const std::string& name) const {
if (config_.keywords_casing == Config::KeywordsCasing::CaseInsensitive) {
return ConvertCase(name, Case::kAllLower);
} else {
return name;
}
}

virtual std::string EscapeKeyword(const std::string& name) const {
if (keywords_.find(name) == keywords_.end()) {
if (keywords_.find(NormalizeKeywordCase(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::CaseSensitive,
/*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::CaseSensitive,
/*filenames=*/Case::kKeep,
/*directories=*/Case::kKeep,
/*output_path=*/"",
Expand Down
6 changes: 6 additions & 0 deletions scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ def glob(path, pattern):
schema="union_vector/union_vector.fbs",
)

flatc(
PHP_OPTS,
prefix="PhpKeywordTest",
schema="php_keyword_test.fbs",
)

flatc(
BASE_OPTS + TS_OPTS,
cwd=ts_code_gen,
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::CaseSensitive,
/*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::CaseSensitive,
/*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::CaseSensitive,
/*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::CaseSensitive,
/*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::CaseSensitive,
/*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::CaseSensitive,
/*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::CaseSensitive,
/*filenames=*/Case::kUpperCamel,
/*directories=*/Case::kLowerCamel,
/*output_path=*/"",
Expand Down
Loading
Loading