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 oneof codegen when bytes feature enabled #363

Merged
merged 1 commit into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions protobuf-codegen/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ fn type_is_copy(field_type: FieldDescriptorProto_Type) -> bool {
}

trait FieldDescriptorProtoTypeExt {
fn read(&self, is: &str) -> String;
fn read(&self, is: &str, primitive_type_variant: PrimitiveTypeVariant) -> String;
fn is_s_varint(&self) -> bool;
}

impl FieldDescriptorProtoTypeExt for FieldDescriptorProto_Type {
fn read(&self, is: &str) -> String {
format!("{}.read_{}()", is, protobuf_name(*self))
fn read(&self, is: &str, primitive_type_variant: PrimitiveTypeVariant) -> String {
match primitive_type_variant {
PrimitiveTypeVariant::Default => format!("{}.read_{}()", is, protobuf_name(*self)),
PrimitiveTypeVariant::Carllerche => {
let protobuf_name = match self {
FieldDescriptorProto_Type::TYPE_STRING => "chars",
_ => protobuf_name(*self),
};
format!("{}.read_carllerche_{}()", is, protobuf_name)
}
}

}

/// True if self is signed integer with zigzag encoding
Expand Down Expand Up @@ -1620,7 +1630,7 @@ impl<'a> FieldGen<'a> {
self.write_assert_wire_type(wire_type_var, w);

let typed = RustValueTyped {
value: format!("{}?", self.proto_type.read("is")),
value: format!("{}?", self.proto_type.read("is", f.elem.primitive_type_variant())),
rust_type: self.full_storage_iter_elem_type(),
};

Expand Down Expand Up @@ -1674,7 +1684,7 @@ impl<'a> FieldGen<'a> {
));
}
_ => {
let read_proc = format!("{}?", self.proto_type.read("is"));
let read_proc = format!("{}?", self.proto_type.read("is", s.elem.primitive_type_variant()));

self.write_assert_wire_type(wire_type_var, w);
w.write_line(&format!("let tmp = {};", read_proc));
Expand Down
4 changes: 4 additions & 0 deletions protobuf-test/src/common/v2/test_carllerche_bytes_pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ message TestCarllercheBytes {
optional string s1 = 2;
repeated bytes br = 3;
repeated string sr = 4;
oneof one {
bytes ob = 11;
string os = 12;
}
}