Skip to content

Commit

Permalink
Fix oneof codegen when bytes feature enabled
Browse files Browse the repository at this point in the history
Issue #362
  • Loading branch information
stepancheg committed Dec 26, 2018
1 parent 269f235 commit 9b6bf64
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 15 additions & 5 deletions protobuf-codegen/src/field.rs
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
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;
}
}

0 comments on commit 9b6bf64

Please sign in to comment.