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

Auto capitalize enums name in Ruby #10454

Merged
merged 21 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 7 additions & 2 deletions ruby/ext/google/protobuf_c/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,15 +1263,20 @@ VALUE build_module_from_enumdesc(VALUE _enumdesc) {
int n = upb_EnumDef_ValueCount(e);
for (int i = 0; i < n; i++) {
const upb_EnumValueDef* ev = upb_EnumDef_Value(e, i);
const char* name = upb_EnumValueDef_Name(ev);
char* name = strdup(upb_EnumValueDef_Name(ev));
int32_t value = upb_EnumValueDef_Number(ev);
if (name[0] < 'A' || name[0] > 'Z') {
rb_warn(
if (name[0] >= 'a' && name[0] <= 'z') {
name[0] -= 32; // auto capitalize
} else {
rb_warn(
"Enum value '%s' does not start with an uppercase letter "
"as is required for Ruby constants.",
name);
}
}
rb_define_const(mod, name, INT2NUM(value));
free(name);
}

rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,15 @@ private RubyModule buildModuleFromDescriptor(ThreadContext context) {
descriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3;
for (EnumValueDescriptor value : descriptor.getValues()) {
String name = value.getName();
// Make sure its a valid constant name before trying to create it
if (Character.isUpperCase(name.codePointAt(0))) {
// Make sure it's a valid constant name before trying to create it
int ch = name.codePointAt(0);
if (Character.isUpperCase(ch)) {
enumModule.defineConstant(name, runtime.newFixnum(value.getNumber()));
} else if (ch >= 'a' && ch <= 'z') {
// Protobuf enums can start with lowercase letters, while Ruby's symbol should
// always start with uppercase letters. We tolerate this case by capitalizing
// the first character if possible.
name = Character.toUpperCase(ch) + name.substring(1);
enumModule.defineConstant(name, runtime.newFixnum(value.getNumber()));
} else {
runtime
Expand Down
10 changes: 7 additions & 3 deletions ruby/tests/common_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,16 @@ def test_rptfield_enum
l.push :A
l.push :B
l.push :C
assert l.count == 3
l.push :V0
assert l.count == 4
assert_raise RangeError do
l.push :D
end
assert l[0] == :A
assert l[3] == :V0

l.push 4
assert l[3] == 4
l.push 5
assert l[4] == 5
end

def test_rptfield_initialize
Expand Down Expand Up @@ -713,6 +715,8 @@ def test_enum_lookup
assert proto_module::TestEnum::B == 2
assert proto_module::TestEnum::C == 3

assert proto_module::TestEnum::V0 == 4

assert proto_module::TestEnum::lookup(1) == :A
assert proto_module::TestEnum::lookup(2) == :B
assert proto_module::TestEnum::lookup(3) == :C
Expand Down
2 changes: 2 additions & 0 deletions ruby/tests/generated_code.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ enum TestEnum {
A = 1;
B = 2;
C = 3;

v0 = 4;
}

message testLowercaseNested {
Expand Down
2 changes: 2 additions & 0 deletions ruby/tests/generated_code_proto2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ enum TestEnum {
A = 1;
B = 2;
C = 3;

v0 = 4;
}

message TestUnknown {
Expand Down
1 change: 1 addition & 0 deletions ruby/tests/repeated_field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ def fill_test_msg(test_msg)
value :A, 1
value :B, 2
value :C, 3
value :V0, 4
end
end

Expand Down