Skip to content

Commit

Permalink
Escape extension field names
Browse files Browse the repository at this point in the history
Fixes #371
  • Loading branch information
stepancheg committed Jan 20, 2019
1 parent 7f52de4 commit 5162d0f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion protobuf-codegen/src/extensions.rs
Expand Up @@ -52,7 +52,7 @@ impl<'a> ExtGen<'a> {
};
let field_type = format!("::protobuf::ext::ExtField{}", suffix);
w.pub_const(
self.field.get_name(), // TODO: escape
&self.field.rust_name(),
&format!(
"{}<{}, {}>",
field_type,
Expand Down
2 changes: 1 addition & 1 deletion protobuf-codegen/src/field.rs
Expand Up @@ -606,7 +606,7 @@ impl<'a> FieldGen<'a> {
FieldGen {
root_scope,
syntax: field.message.get_scope().file_scope.syntax(),
rust_name: RustIdent(field.rust_name()),
rust_name: RustIdent(field.field.rust_name()),
proto_type: field.field.get_field_type(),
wire_type: field_type_wire_type(field.field.get_field_type()),
enum_default_value,
Expand Down
6 changes: 6 additions & 0 deletions protobuf-test/src/common/v2/test_ident_pb.proto
@@ -1,5 +1,7 @@
syntax = "proto2";

import "google/protobuf/descriptor.proto";

package test_ident;

// rust types defined in prelude
Expand Down Expand Up @@ -65,3 +67,7 @@ message MessageReferencesEnumWithDefaultValueReserved {
EnumWithDefaultValueReserved e4 = 4;
}
}

extend google.protobuf.EnumValueOptions {
optional string type = 51234;
}
21 changes: 16 additions & 5 deletions protobuf/src/descriptorx.rs
Expand Up @@ -465,6 +465,20 @@ impl<'a> WithScope<'a> for MessageOrEnumWithScope<'a> {
}
}

pub trait FieldDescriptorProtoExt {
fn rust_name(&self) -> String;
}

impl FieldDescriptorProtoExt for FieldDescriptorProto {
fn rust_name(&self) -> String {
if rust::is_rust_keyword(self.get_name()) {
format!("field_{}", self.get_name())
} else {
self.get_name().to_string()
}
}
}

#[derive(Clone)]
pub struct FieldWithContext<'a> {
pub field: &'a FieldDescriptorProto,
Expand Down Expand Up @@ -497,12 +511,9 @@ impl<'a> FieldWithContext<'a> {
}

// field name in generated code
#[deprecated]
pub fn rust_name(&self) -> String {
if rust::is_rust_keyword(self.field.get_name()) {
format!("field_{}", self.field.get_name())
} else {
self.field.get_name().to_string()
}
self.field.rust_name()
}

// From field to file root
Expand Down

0 comments on commit 5162d0f

Please sign in to comment.