Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToCamelCase() when kLowerCamel now converts first char to lower. #7838

Merged
merged 2 commits into from
Mar 15, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/idl_gen_kotlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static Namer::Config KotlinDefaultConfig() {
/*functions=*/Case::kKeep,
/*fields=*/Case::kLowerCamel,
/*variables=*/Case::kLowerCamel,
/*variants=*/Case::kLowerCamel,
/*variants=*/Case::kKeep,
/*enum_variant_seperator=*/"", // I.e. Concatenate.
/*escape_keywords=*/Namer::Config::Escape::BeforeConvertingCase,
/*namespaces=*/Case::kKeep,
Expand Down Expand Up @@ -301,7 +301,7 @@ class KotlinGenerator : public BaseGenerator {
auto field_type = GenTypeBasic(enum_def.underlying_type.base_type);
auto val = enum_def.ToString(ev);
auto suffix = LiteralSuffix(enum_def.underlying_type.base_type);
writer.SetValue("name", namer_.LegacyKotlinVariant(ev));
writer.SetValue("name", namer_.Variant(ev.name));
writer.SetValue("type", field_type);
writer.SetValue("val", val + suffix);
GenerateComment(ev.doc_comment, writer, &comment_config);
Expand Down
13 changes: 10 additions & 3 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,18 @@ static bool LoadFileRaw(const char *name, bool binary, std::string *buf) {
LoadFileFunction g_load_file_function = LoadFileRaw;
FileExistsFunction g_file_exists_function = FileExistsRaw;

static std::string ToCamelCase(const std::string &input, bool first) {
static std::string ToCamelCase(const std::string &input, bool is_upper) {
std::string s;
for (size_t i = 0; i < input.length(); i++) {
if (!i && first)
s += CharToUpper(input[i]);
if (!i && input[i] == '_') {
s += input[i];
// we ignore leading underscore but make following
// alphabet char upper.
if (i + 1 < input.length() && is_alpha(input[i + 1]))
s += CharToUpper(input[++i]);
}
else if (!i)
s += is_upper ? CharToUpper(input[i]) : CharToLower(input[i]);
else if (input[i] == '_' && i + 1 < input.length())
s += CharToUpper(input[++i]);
else
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/Any.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Any_ private constructor() {
const val NONE: UByte = 0u
const val Monster: UByte = 1u
const val TestSimpleTableWithEnum: UByte = 2u
const val MyGameExample2Monster: UByte = 3u
const val MyGame_Example2_Monster: UByte = 3u
val names : Array<String> = arrayOf("NONE", "Monster", "TestSimpleTableWithEnum", "MyGame_Example2_Monster")
fun name(e: Int) : String = names[e]
}
Expand Down
6 changes: 3 additions & 3 deletions tests/util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ void UtilConvertCase() {
// missing.
cases.push_back({ "single", flatbuffers::Case::kUpperCamel, "Single" });
cases.push_back({ "Single", flatbuffers::Case::kUpperCamel, "Single" });
cases.push_back({ "_leading", flatbuffers::Case::kUpperCamel, "_leading" });
cases.push_back({ "_leading", flatbuffers::Case::kUpperCamel, "_Leading" });
cases.push_back(
{ "trailing_", flatbuffers::Case::kUpperCamel, "Trailing_" });
cases.push_back({ "double__underscore", flatbuffers::Case::kUpperCamel,
"Double_underscore" });
cases.push_back({ "single", flatbuffers::Case::kLowerCamel, "single" });
cases.push_back({ "Single", flatbuffers::Case::kLowerCamel, "Single" });
cases.push_back({ "_leading", flatbuffers::Case::kLowerCamel, "Leading" });
cases.push_back({ "Single", flatbuffers::Case::kLowerCamel, "single" });
cases.push_back({ "_leading", flatbuffers::Case::kLowerCamel, "_Leading" });
cases.push_back(
{ "trailing_", flatbuffers::Case::kLowerCamel, "trailing_" });
cases.push_back({ "double__underscore", flatbuffers::Case::kLowerCamel,
Expand Down