camelcase structs returned over RPC (#1507)

This commit is contained in:
Tomasz Drwięga
2019-01-21 19:25:58 +01:00
committed by Gav Wood
parent 8ec759d32e
commit e48291acd0
7 changed files with 81 additions and 2 deletions
+1
View File
@@ -3000,6 +3000,7 @@ dependencies = [
name = "sr-version"
version = "0.1.0"
dependencies = [
"impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
+1
View File
@@ -32,6 +32,7 @@ pub struct StorageData(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec
/// Storage change set
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug, PartialEq, Eq))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct StorageChangeSet<Hash> {
/// Block hash
pub block: Hash,
+5
View File
@@ -220,6 +220,11 @@ fn should_return_runtime_version() {
api.runtime_version(None.into()),
Ok(ref ver) if ver == &runtime::VERSION
);
assert_eq!(
::serde_json::to_string(&api.runtime_version(None.into()).unwrap()).unwrap(),
r#"{"specName":"test","implName":"parity-test","authoringVersion":1,"specVersion":1,"implVersion":1,"apis":[["0xdf6acb689907609b",1],["0x37e397fc7c91f5e4",1],["0xd2bc9897eed08f15",1],["0x40fe3ad401f8959a",1],["0xc6e9a76309f39b09",1],["0xdd718d5cc53262d4",1]]}"#
);
}
#[test]
+36
View File
@@ -38,17 +38,21 @@ pub struct SystemInfo {
/// Health struct returned by the RPC
#[derive(Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Health {
/// Number of connected peers
pub peers: usize,
/// Is the node syncing
pub is_syncing: bool,
/// Should this node have any peers
///
/// Might be false for local chains or when running without discovery.
pub should_have_peers: bool,
}
/// Network Peer information
#[derive(Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PeerInfo<Hash, Number> {
/// Peer Node Index
pub index: usize,
@@ -71,3 +75,35 @@ impl fmt::Display for Health {
} else { "idle" })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_serialize_health() {
assert_eq!(
::serde_json::to_string(&Health {
peers: 1,
is_syncing: false,
should_have_peers: true,
}).unwrap(),
r#"{"peers":1,"isSyncing":false,"shouldHavePeers":true}"#,
);
}
#[test]
fn should_serialize_peer_info() {
assert_eq!(
::serde_json::to_string(&PeerInfo {
index: 1,
peer_id: "2".into(),
roles: "a".into(),
protocol_version: 2,
best_hash: 5u32,
best_number: 6u32,
}).unwrap(),
r#"{"index":1,"peerId":"2","roles":"a","protocolVersion":2,"bestHash":5,"bestNumber":6}"#,
);
}
}
-1
View File
@@ -19,7 +19,6 @@ use super::*;
use network::{self, SyncState, SyncStatus, ProtocolStatus, NodeIndex, PeerId, PeerInfo as NetworkPeerInfo, PublicKey};
use network::config::Roles;
use test_client::runtime::Block;
use primitives::H256;
#[derive(Default)]
struct Status {
+2
View File
@@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
impl-serde = { version = "0.1", optional = true }
serde = { version = "1.0", default-features = false }
serde_derive = { version = "1.0", optional = true }
parity-codec = { version = "2.2", default-features = false }
@@ -14,6 +15,7 @@ sr-primitives = { path = "../sr-primitives", default-features = false }
[features]
default = ["std"]
std = [
"impl-serde",
"serde/std",
"serde_derive",
"parity-codec/std",
+36 -1
View File
@@ -71,7 +71,8 @@ macro_rules! create_apis_vec {
/// In particular: bug fixes should result in an increment of `spec_version` and possibly `authoring_version`,
/// absolutely not `impl_version` since they change the semantics of the runtime.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize, Decode))]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Decode))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct RuntimeVersion {
/// Identifies the different Substrate runtimes. There'll be at least polkadot and node.
/// A different on-chain spec_name to that of the native runtime would normally result
@@ -103,6 +104,7 @@ pub struct RuntimeVersion {
pub impl_version: u32,
/// List of supported API "features" along with their versions.
#[cfg_attr(feature = "std", serde(serialize_with = "apis_serialize::serialize"))]
pub apis: ApisVec,
}
@@ -154,3 +156,36 @@ impl NativeVersion {
self.can_author_with.contains(&other.authoring_version))
}
}
#[cfg(feature = "std")]
mod apis_serialize {
extern crate impl_serde;
extern crate serde;
use super::*;
use self::impl_serde::serialize as bytes;
use self::serde::{Serializer, ser::SerializeTuple};
#[derive(Serialize)]
struct ApiId<'a>(
#[serde(serialize_with="serialize_bytesref")] &'a super::ApiId,
&'a u32,
);
pub fn serialize<S>(apis: &ApisVec, ser: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{
let len = apis.len();
let mut seq = ser.serialize_tuple(len)?;
for (api, ver) in &**apis {
seq.serialize_element(&ApiId(api, ver))?;
}
seq.end()
}
pub fn serialize_bytesref<S>(apis: &&super::ApiId, ser: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{
bytes::serialize(*apis, ser)
}
}