-
Notifications
You must be signed in to change notification settings - Fork 44
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
Add serde support for Value #16
Conversation
9521c97
to
2ad717e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice work 😄
Some minor nits, but I would like to see a few more tests to merge. In particular I think the serde encoding of Struct, ListValue and NullValue is currently incorrect which will render the encoding of Value incorrect by proxy.
} | ||
} | ||
|
||
struct KindVisitor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you might be able to avoid defining this visitor manually by using untagged. This is how numbers are deserialized from either strings or numbers in pbjson. This way is fine also though
FYI there is an open serde issue to expose the plumbing that serde-derive is using - serde-rs/serde#1947
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn float_special_cases() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👌
ser, Deserialize, Deserializer, Serialize, Serializer, | ||
}; | ||
|
||
macro_rules! from { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very neat
@@ -0,0 +1,317 @@ | |||
pub use crate::pb::google::protobuf::value::Kind; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm mistaken this module is crate-private and so the pub use
is really pub(crate) use
. Is this intentional?
I guess I'm just wondering why this isn't just use crate::value::Kind
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was not intentional for this to be private, this is intended to replace the value
module that is in google::protobuf
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above, I think I would prefer to keep the module topology flat unless there is a compelling reason to not do so - e.g. new public types
2ad717e
to
6497ac2
Compare
@tustvold This should be ready for a re-review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some very minor nits, and then this is good to go. Really nice work! 😄
mod timestamp; | ||
pub mod value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would prefer to keep the module topology flat, currently all inner modules are private and the types are re-exported by the pub use pb::google::protobuf::*
a few lines down
value
does not appear to define any new public types, but just define some trait impls for crate::Value
. Is there a particular reason you wish to have a value module?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason you wish to have a value module?
If I was looking for the implementation of Value
, intuitively I'd look for src/value.rs
, the topology isn't flat because pb::google::protobuf::*
already exports a value
module. So this makes so the actual code structure reflect what people see in the documentation, and removes the conflicts that arise from using glob exports when it is private. mod value
overrides the value
from pb::google::protobuf::*
, but this wouldn't be obvious to you unless you removed the pub mod
and pub use pb::protobuf::google::protobuf::Kind
and then tried to use crate::value::Kind
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes prost is generating a value
namespace within pb::google::protobuf
because Kind
is defined within the Value
message. Gotcha, that is definitely confusing but makes sense 👍
use crate::Value; | ||
|
||
#[test] | ||
fn list_value() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we get a test with some mixed types, e.g. something like
assert_eq!(
serde_json::to_value(Value::from([true.into(), "HELLO".into(), false.into()])).unwrap(),
serde_json::json!([serde_json::json!(true), serde_json::json!("HELLO"), serde_json::json!(false)])
);
Or even some nested maps if you're feeling fancy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the mixed types test, nested maps is already tested in the struct::it_works
test. Since ListValue
is just using Value
's serde impl for items, there isn't a chance that ListValue
would produce a different value for nested maps than Struct
.
@@ -0,0 +1,317 @@ | |||
pub use crate::pb::google::protobuf::value::Kind; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above, I think I would prefer to keep the module topology flat unless there is a compelling reason to not do so - e.g. new public types
🎉 Thanks again 😄 |
This PR adds
serde
support forgoogle.protobuf.Value
.closes #5
Depends on #14