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

ToCamelCase(input, true) converts first char to upper case, but
ToCamelCase(input, false) keeps the case of the first char. We are
changing its behavior to force a lower case.

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Paulo Pinheiro
2023-03-15 02:09:24 +01:00
committed by GitHub
parent d4d355d883
commit d3d7e2ef99
4 changed files with 16 additions and 9 deletions

View File

@@ -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,
@@ -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);

View File

@@ -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

View File

@@ -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]
}

View File

@@ -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,