8000 Extract ProtobufConvert derive to separate crate by pavel-mukhanov · Pull Request #1501 · exonum/exonum · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Extract ProtobufConvert derive to separate crate #1501

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

Merged
merged 37 commits into from
Oct 24, 2019

Conversation

pavel-mukhanov
Copy link
Contributor
@pavel-mukhanov pavel-mukhanov commented Oct 16, 2019
  • Derive macros ProtobufConvert is moved to separate crate.
  • Added derive macros BinaryValue and ObjectHash to exonum-derive.

@@ -65,8 +65,7 @@ impl ProtobufConvert for ValidatorId {
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= u32::from(u16::max_value()),
"{} is out of range for valid ValidatorId",
pb
"u32 is out of range for valid ValidatorId"
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merge artifact, sorry

@codecov
Copy link
codecov bot commented Oct 16, 2019

Codecov Report

Merging #1501 into dynamic_services will decrease coverage by <.01%.
The diff coverage is 95.5%.

Impacted file tree graph

@@                 Coverage Diff                  @@
##           dynamic_services    #1501      +/-   ##
====================================================
- Coverage              93.4%   93.39%   -0.01%     
====================================================
  Files                   198      198              
  Lines                 28381    28408      +27     
====================================================
+ Hits                  26510    26533      +23     
- Misses                 1871     1875       +4
Impacted Files Coverage Δ
testkit/src/lib.rs 94.27% <ø> (ø) ⬆️
components/proto/src/lib.rs 97.01% <ø> (ø) ⬆️
...ples/cryptocurrency-advanced/backend/src/wallet.rs 100% <100%> (ø) ⬆️
testkit/tests/service_hooks/hooks.rs 100% <100%> (ø) ⬆️
testkit/src/simple_supervisor/proto/mod.rs 100% <100%> (ø) ⬆️
examples/timestamping/backend/src/schema.rs 97.36% <100%> (ø) ⬆️
exonum/src/runtime/rust/tests.rs 95.45% <100%> (ø) ⬆️
examples/timestamping/backend/src/transactions.rs 90% <100%> (ø) ⬆️
services/supervisor/src/proto_structures.rs 91.66% <100%> (ø) ⬆️
examples/cryptocurrency/src/lib.rs 91.34% <100%> (ø) ⬆️
... and 19 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
P 8000 owered by Codecov. Last update 47114e3...f8629b6. Read the comment docs.

Copy link
Contributor
@alekseysidorov alekseysidorov left a comment

Choose a reason for hiding this comment

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

So we can replace
[exonum(pb = "")] by the [protobuf_convert(source = "")] or even [protobuf_convert(source = foo::path)]

@@ -44,7 +45,7 @@ pub enum Error {
}

/// Transfer `amount` of the currency from one wallet to another.
#[derive(Clone, Debug, ProtobufConvert)]
#[derive(Clone, Debug, ProtobufConvert, BinaryValue, ObjectHash)]
#[exonum(pb = "proto::Transfer", serde_pb_convert)]
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 we should avoid of using exonum for attribute name here, and also is there any need to have serde_pb_convert attribute that looks very strange and unclear.

@@ -55,6 +57,7 @@ extern crate log;
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate exonum_proto;
Copy link
Contributor

Choose a reason for hiding this comment

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

Needless ?

CHANGELOG.md Outdated

#### exonum-proto-derive

- Introduced new crate `exonum-proto-derive`. Derive macro `ProtobufConvert` is
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Introduced new crate `exonum-proto-derive`. Derive macro `ProtobufConvert` is
- Introduced a new crate `exonum-proto-derive`. Derive macro `ProtobufConvert` is

@@ -20,56 +20,58 @@

extern crate proc_macro;
Copy link
Contributor

Choose a reason for hiding this comment

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

Needless ?

@@ -473,14 +429,13 @@ impl ToTokens for ProtobufConvert {
};

let expanded = quote! {
mod #mod_name {
mod #mod_name {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this padding was added?

CHANGELOG.md Outdated
@@ -39,8 +39,17 @@ The project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)

#### exonum-proto

- Introduced new crate `exonum-proto`. Trait `ProtobufConvert` is moved to
`exonum-proto` crate. (#1496)
- Introduced a new crate `exonum-proto`. Trait `ProtobufConvert` changed to is moved
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

Copy link
Contributor
@slowli slowli left a comment

Choose a reason for hiding this comment

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

Comments as of cb81870.

/// Implement `serde::{Serialize, Deserialize}` using structs that were generated with
/// protobuf.
/// For example, it should be used if you want json representation of your struct
/// to be compatible with protobuf representation (including proper nesting of fields).
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit / off-topic: it's not clear what "compatible" means in this context. E.g., bytes should be base64-encoded according to Protobuf 3 spec, but the example below shows that it is serialized as an array.

/// (de)serializer will be represented as
/// StructName {
/// "hash": {
/// data: [1, 2, ...]
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: "data" should be quoted?


#[derive(Debug, FromMeta, PartialEq, Eq)]
#[darling(default)]
struct CratePath(syn::Path);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this struct necessary?

/// // ...
/// }
/// ```
#[proc_macro_attribute]
Copy link
Contributor

Choose a reason for hiding this comment

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

Am I correct that protobuf_convert is not a derive macro? Why? It derives a specific trait, so a derive macro would be more appropriate. IMO, a proc macro attribute implies a deeper level of transform of the attached TokenStream.

@@ -20,6 +20,7 @@ exonum-derive = { version = "0.12.0", path = "../../../components/derive" }
exonum-merkledb = { version = "0.12.0", path = "../../../components/merkledb" }
exonum-crypto = { version = "0.12.0", path = "../../../components/crypto"}
exonum-proto = { version = "0.12.0", path = "../../../components/proto" }
exonum-proto-derive = { version = "0.12.0", path = "../../../components/proto-derive"}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could it be possible to bundle exonum-proto-derive with exonum-proto as in structopt and recent versions of serde?

Copy link
Contributor

Choose a reason for hiding this comment

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

Bumping the request. This is not a serious concern; still, it improves UX.

use quote::ToTokens;
use syn::{Attribute, NestedMeta};

/// ProtobufConvert attribute macros.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: why plural (macros)? Also, an example or two would be great, especially for enums.

@aleksuss aleksuss requested a review from vitvakatu October 18, 2019 14:21
Copy link
Contributor
@slowli slowli left a comment

Choose a reason for hiding this comment

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

Looks mergeable as of ee261f0.

/// #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, ProtobufConvert)]
/// #[protobuf_convert(source = "schema::runtime::ConfigChange", oneof_field = "message")]
/// pub enum ConfigChange {
/// /// New consensus config.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: bogus indentation (3 instead of 4).

/// Corresponding proto file:
/// ```text
/// message Wallet {
/// // Public key of the wallet owner.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: bogus indentation (1).

/// ```
///
/// Corresponding proto file:
/// ```test
Copy link
Contributor

Choose a reason for hiding this comment

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

text? Also, maybe use proto for syntax highlighting?

/// Corresponding proto file:
/// ```test
/// message ConfigChange {
/// oneof message {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Bogus indentation (1).

/// ```
///
/// This macro can also be applied to enums. In proto files enums are represented
/// by `oneof` field. You can specify `oneof` field name, default is "message".
Copy link
Contributor

Choose a reason for hiding this comment

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

Do I understand correctly that:

  • The protobuf message must contain only this oneof field group (additional fields are not allowed)?
  • The Rust enum should have variants with a single unnamed field? (Or are zero-field variants allowed too?)

It could be worth specifying these details.

@@ -20,6 +20,7 @@ exonum-derive = { version = "0.12.0", path = "../../../components/derive" }
exonum-merkledb = { version = "0.12.0", path = "../../../components/merkledb" }
exonum-crypto = { version = "0.12.0", path = "../../../components/crypto"}
exonum-proto = { version = "0.12.0", path = "../../../components/proto" }
exonum-proto-derive = { version = "0.12.0", path = "../../../components/proto-derive"}
Copy link
Contributor

Choose a reason for hiding this comment

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

Bumping the request. This is not a serious concern; still, it improves UX.

@aleksuss aleksuss merged commit dd87b91 into exonum:dynamic_services Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0