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

Fix JS Generator Identifier when name is camel case #1227

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ target
.project
.classpath
.settings
.idea
92 changes: 81 additions & 11 deletions javascript/net/grpc/web/generator/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,76 @@ string AsObjectFieldType(const FieldDescriptor* desc,
return field_type;
}


bool IsUpperCamel(const std::string& input) {
if (input.empty()) {
return false;
}

if (input.find('_') != string::npos) {
return false;
}

if (input[0] < 'A' || input[0] > 'Z') {
return false;
}

for (int i = 0; i < input.size(); i++) {
if (input[i] >= 'A' && input[i] <= 'Z') {
return true;
}
}

return false;
}

bool IsLowerCamel(const std::string& input) {
if (input.empty()) {
return false;
}

if (input.find('_') != string::npos) {
return false;
}

if (input[0] >= 'A' && input[0] <= 'Z') {
return false;
}

for (int i = 0; i < input.size(); i++) {
if (i != 0 && input[i] >= 'A' && input[i] <= 'Z') {
return true;
}
}

return false;
}


// Like ToUpperCamel except the first letter is not converted.
string ToCamelCase(const std::vector<string>& words) {
if (words.empty()) {
return "";
}
string result = words[0];
return result + ToUpperCamel(std::vector<string>(
words.begin() + 1, words.begin() + words.size()));
}

string JSElementName(const FieldDescriptor* desc) {
string js_field_name;
if (IsUpperCamel(desc->name())) {
return desc->name();
}

if (IsLowerCamel(desc->name())) {
string name = desc->name();

name[0] = ::toupper(name[0]);

return name;
}

return ToUpperCamel(ParseLowerUnderscore(desc->name()));
}

Expand All @@ -378,19 +447,20 @@ string JSFieldName(const FieldDescriptor* desc) {
return js_field_name;
}

// Like ToUpperCamel except the first letter is not converted.
string ToCamelCase(const std::vector<string>& words) {
if (words.empty()) {
return "";
}
string result = words[0];
return result + ToUpperCamel(std::vector<string>(
words.begin() + 1, words.begin() + words.size()));
}

// Like JSFieldName, but with first letter not uppercased
string CamelCaseJSFieldName(const FieldDescriptor* desc) {
string js_field_name = ToCamelCase(ParseLowerUnderscore(desc->name()));
string js_field_name;
if (IsUpperCamel(desc->name())) {
string name = desc->name();
name[0] = ::tolower(name[0]);

js_field_name = name;
} else if (IsLowerCamel(desc->name())) {
js_field_name = desc->name();
} else {
js_field_name = ToCamelCase(ParseLowerUnderscore(desc->name()));
}

if (desc->is_map()) {
js_field_name += "Map";
} else if (desc->is_repeated()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/grpc-web/test/tsc-tests/client01.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import {MessageOuter} from './generated/test01_pb';
let inner1 = new MessageOuter.MessageInner();
inner1.setValue(123);
let msgOuter = new MessageOuter();
msgOuter.setSomepropList([inner1]);
msgOuter.setSomePropList([inner1]);

export {msgOuter}