Skip to content

Commit 4891e79

Browse files
authored
Merge pull request #1560 from CosmWasm/contruct-trhough-default
Add QueryResponseType trait and supply Default implementations
2 parents abc9bcc + 03f803d commit 4891e79

File tree

7 files changed

+43
-22
lines changed

7 files changed

+43
-22
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ and this project adheres to
2323
- cosmwasm-std: Upgrade `serde-json-wasm` dependency to 0.5.0 which adds map
2424
support to `to_vec`/`to_binary` and friends.
2525
- cosmwasm-std: Implement `AsRef<[u8]>` for `Binary` and `HexBinary` ([#1550]).
26-
- cosmwasm-std: Add constructor `SupplyResponse::new` ([#1552]).
26+
- cosmwasm-std: Allow constructing `SupplyResponse` via a `Default`
27+
implementation ([#1552], [#1560]).
2728

2829
[#1436]: https://github.com/CosmWasm/cosmwasm/issues/1436
2930
[#1437]: https://github.com/CosmWasm/cosmwasm/issues/1437
@@ -33,6 +34,7 @@ and this project adheres to
3334
[#1550]: https://github.com/CosmWasm/cosmwasm/issues/1550
3435
[#1552]: https://github.com/CosmWasm/cosmwasm/pull/1552
3536
[#1554]: https://github.com/CosmWasm/cosmwasm/pull/1554
37+
[#1560]: https://github.com/CosmWasm/cosmwasm/pull/1560
3638

3739
### Changed
3840

contracts/reflect/schema/raw/query.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@
547547
"additionalProperties": false
548548
},
549549
{
550-
"description": "returns a ContractInfoResponse with metadata on the contract from the runtime",
550+
"description": "Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime",
551551
"type": "object",
552552
"required": [
553553
"contract_info"

contracts/reflect/schema/reflect.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@
14111411
"additionalProperties": false
14121412
},
14131413
{
1414-
"description": "returns a ContractInfoResponse with metadata on the contract from the runtime",
1414+
"description": "Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime",
14151415
"type": "object",
14161416
"required": [
14171417
"contract_info"

packages/std/src/query/bank.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};
33

44
use crate::Coin;
55

6+
use super::query_response::QueryResponseType;
7+
68
#[non_exhaustive]
79
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
810
#[serde(rename_all = "snake_case")]
@@ -22,7 +24,7 @@ pub enum BankQuery {
2224
}
2325

2426
#[cfg(feature = "cosmwasm_1_1")]
25-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
27+
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
2628
#[serde(rename_all = "snake_case")]
2729
#[non_exhaustive]
2830
pub struct SupplyResponse {
@@ -32,28 +34,23 @@ pub struct SupplyResponse {
3234
}
3335

3436
#[cfg(feature = "cosmwasm_1_1")]
35-
impl SupplyResponse {
36-
/// Constructor for testing frameworks such as cw-multi-test.
37-
/// This is required because query response types should be #[non_exhaustive].
38-
/// As a contract developer you should not need this constructor since
39-
/// query responses are constructed for you via deserialization.
40-
#[doc(hidden)]
41-
pub fn new(amount: Coin) -> Self {
42-
Self { amount }
43-
}
44-
}
37+
impl QueryResponseType for SupplyResponse {}
4538

46-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
39+
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
4740
#[serde(rename_all = "snake_case")]
4841
pub struct BalanceResponse {
4942
/// Always returns a Coin with the requested denom.
5043
/// This may be of 0 amount if no such funds.
5144
pub amount: Coin,
5245
}
5346

54-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
47+
impl QueryResponseType for BalanceResponse {}
48+
49+
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
5550
#[serde(rename_all = "snake_case")]
5651
pub struct AllBalanceResponse {
5752
/// Returns all non-zero coins held by this account.
5853
pub amount: Vec<Coin>,
5954
}
55+
56+
impl QueryResponseType for AllBalanceResponse {}

packages/std/src/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::Empty;
77

88
mod bank;
99
mod ibc;
10+
mod query_response;
1011
mod staking;
1112
mod wasm;
1213

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use serde::de::DeserializeOwned;
2+
3+
/// A marker trait for query response types.
4+
///
5+
/// Those types have in common that they should be `#[non_exhaustive]` in order
6+
/// to allow adding fields in a backwards compatible way. In contracts they are
7+
/// only constructed through deserialization. We want to make it hard for
8+
/// contract developers to construct those types themselves as this is most likely
9+
/// not what they should do.
10+
///
11+
/// In hosts they are constructed as follows:
12+
/// - wasmvm: Go types with the same JSON layout
13+
/// - multi-test/cw-sdk: create a default instance and mutate the fields
14+
///
15+
/// This trait is crate-internal and can change any time.
16+
pub(crate) trait QueryResponseType: Default + DeserializeOwned {}

packages/std/src/query/wasm.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};
33

44
use crate::Binary;
55

6+
use super::query_response::QueryResponseType;
7+
68
#[non_exhaustive]
79
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
810
#[serde(rename_all = "snake_case")]
@@ -22,12 +24,12 @@ pub enum WasmQuery {
2224
/// Key is the raw key used in the contracts Storage
2325
key: Binary,
2426
},
25-
/// returns a ContractInfoResponse with metadata on the contract from the runtime
27+
/// Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime
2628
ContractInfo { contract_addr: String },
2729
}
2830

2931
#[non_exhaustive]
30-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
32+
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
3133
pub struct ContractInfoResponse {
3234
pub code_id: u64,
3335
/// address that instantiated this contract
@@ -40,19 +42,22 @@ pub struct ContractInfoResponse {
4042
pub ibc_port: Option<String>,
4143
}
4244

45+
impl QueryResponseType for ContractInfoResponse {}
46+
4347
impl ContractInfoResponse {
4448
/// Constructor for testing frameworks such as cw-multi-test.
4549
/// This is required because query response types should be #[non_exhaustive].
4650
/// As a contract developer you should not need this constructor since
4751
/// query responses are constructed for you via deserialization.
4852
#[doc(hidden)]
53+
#[deprecated(
54+
note = "Use ContractInfoResponse::default() and mutate the fields you want to set."
55+
)]
4956
pub fn new(code_id: u64, creator: impl Into<String>) -> Self {
50-
Self {
57+
ContractInfoResponse {
5158
code_id,
5259
creator: creator.into(),
53-
admin: None,
54-
pinned: false,
55-
ibc_port: None,
60+
..Default::default()
5661
}
5762
}
5863
}

0 commit comments

Comments
 (0)