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 81559f9 commit 3097d62
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion protobuf-codegen/src/extensions.rs
Expand Up @@ -53,7 +53,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 @@ -474,7 +474,7 @@ impl<'a> FieldGen<'a> {
FieldGen {
root_scope: root_scope,
syntax: field.message.get_scope().file_scope.syntax(),
rust_name: field.rust_name(),
rust_name: 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: 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;
}
20 changes: 15 additions & 5 deletions protobuf/src/descriptorx.rs
Expand Up @@ -491,6 +491,19 @@ 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> {
Expand Down Expand Up @@ -524,12 +537,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 3097d62

Please sign in to comment.