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

Add serde support for Value #16

Merged
merged 3 commits into from
Nov 27, 2021
Merged

Conversation

XAMPPRocky
Copy link
Contributor

@XAMPPRocky XAMPPRocky commented Nov 20, 2021

This PR adds serde support for google.protobuf.Value.

closes #5
Depends on #14

Copy link
Contributor

@tustvold tustvold left a 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.

pbjson-types/src/struct.rs Outdated Show resolved Hide resolved
}
}

struct KindVisitor;
Copy link
Contributor

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

pbjson-types/src/value.rs Outdated Show resolved Hide resolved
#[cfg(test)]
mod tests {
#[test]
fn float_special_cases() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👌

pbjson-types/src/value.rs Show resolved Hide resolved
ser, Deserialize, Deserializer, Serialize, Serializer,
};

macro_rules! from {
Copy link
Contributor

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;
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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

pbjson-types/src/value.rs Show resolved Hide resolved
pbjson-types/src/struct.rs Outdated Show resolved Hide resolved
pbjson-types/src/list_value.rs Outdated Show resolved Hide resolved
@XAMPPRocky
Copy link
Contributor Author

@tustvold This should be ready for a re-review.

Copy link
Contributor

@tustvold tustvold left a 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;
Copy link
Contributor

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?

Copy link
Contributor Author

@XAMPPRocky XAMPPRocky Nov 27, 2021

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.

Copy link
Contributor

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() {
Copy link
Contributor

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

Copy link
Contributor Author

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;
Copy link
Contributor

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

@tustvold tustvold added automerge Put in the merge queue kodiak: merge.method = 'squash' Instruct kodiak to perform a squash merge labels Nov 27, 2021
@kodiakhq kodiakhq bot merged commit f6492f0 into influxdata:main Nov 27, 2021
@tustvold
Copy link
Contributor

🎉 Thanks again 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Put in the merge queue kodiak: merge.method = 'squash' Instruct kodiak to perform a squash merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Correct Serialization of google::protobuf::Value
2 participants