Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support more extension types in codegen
Pure rust codegen doesn't support enum or message extensions
- Loading branch information
1 parent
a8c66d6
commit 0837187
Showing
3 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use protobuf::prelude::*; | ||
use protobuf::Message; | ||
|
||
use super::test_ext_pb::*; | ||
|
||
#[test] | ||
fn test_get() { | ||
let descriptor = MyMessage::descriptor_static(); | ||
let message = descriptor.get_proto().options.get_message(); | ||
assert_eq!(10.5, exts::double_field.get(message).unwrap_or_default()); | ||
assert_eq!(-8.5, exts::float_field.get(message).unwrap_or_default()); | ||
assert_eq!(-3, exts::int32_field.get(message).unwrap_or_default()); | ||
assert_eq!(-13, exts::int64_field.get(message).unwrap_or_default()); | ||
assert_eq!(-4, exts::sint32_field.get(message).unwrap_or_default()); | ||
assert_eq!(-14, exts::sint64_field.get(message).unwrap_or_default()); | ||
assert_eq!(5, exts::uint32_field.get(message).unwrap_or_default()); | ||
assert_eq!(15, exts::uint64_field.get(message).unwrap_or_default()); | ||
assert_eq!(6, exts::fixed32_field.get(message).unwrap_or_default()); | ||
assert_eq!(16, exts::fixed64_field.get(message).unwrap_or_default()); | ||
assert_eq!(7, exts::sfixed32_field.get(message).unwrap_or_default()); | ||
assert_eq!(-17, exts::sfixed64_field.get(message).unwrap_or_default()); | ||
assert_eq!(true, exts::bool_field.get(message).unwrap_or_default()); | ||
assert_eq!("Hello world!", exts::string_field.get(message).unwrap_or_default()); | ||
if false { | ||
// TODO: only implemented in `protoc`-based codegen | ||
assert_eq!(TestEnum::RED, exts::enum_field.get(message).unwrap_or_default()); | ||
assert_eq!(22, exts::message_field.get(message).unwrap().get_n()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
syntax = "proto2"; | ||
|
||
package test_ext; | ||
|
||
import "google/protobuf/descriptor.proto"; | ||
import "rustproto.proto"; | ||
option (rustproto.generate_accessors_all) = true; | ||
|
||
|
||
message SubM { | ||
optional int32 n = 1; | ||
} | ||
|
||
enum TestEnum { | ||
UNDEFINED = 0; | ||
RED = 1; | ||
BLUE = 2; | ||
GREEN = 3; | ||
} | ||
|
||
|
||
extend google.protobuf.MessageOptions { | ||
optional double double_field = 50001; | ||
optional float float_field = 50002; | ||
optional int32 int32_field = 50003; | ||
optional int64 int64_field = 50004; | ||
optional uint32 uint32_field = 50005; | ||
optional uint64 uint64_field = 50006; | ||
optional sint32 sint32_field = 50007; | ||
optional sint64 sint64_field = 50008; | ||
optional fixed32 fixed32_field = 50009; | ||
optional fixed64 fixed64_field = 50010; | ||
optional sfixed32 sfixed32_field = 50011; | ||
optional sfixed64 sfixed64_field = 50012; | ||
optional bool bool_field = 50013; | ||
optional string string_field = 50014; | ||
optional bytes bytes_field = 50015; | ||
optional TestEnum enum_field = 50016; | ||
optional SubM message_field = 50017; | ||
} | ||
|
||
message MyMessage { | ||
option (double_field) = 10.5; | ||
option (float_field) = -8.5; | ||
option (int32_field) = -3; | ||
option (int64_field) = -13; | ||
option (sint32_field) = -4; | ||
option (sint64_field) = -14; | ||
option (uint32_field) = 5; | ||
option (uint64_field) = 15; | ||
option (fixed32_field) = 6; | ||
option (fixed64_field) = 16; | ||
option (sfixed32_field) = 7; | ||
option (sfixed64_field) = -17; | ||
option (bool_field) = true; | ||
option (string_field) = "Hello world!"; | ||
option (bytes_field) = "bytes"; | ||
option (enum_field) = RED; | ||
option (message_field) = { n: 22 }; | ||
} |