Skip to content

Commit

Permalink
Use skip_serializing_none
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored and canonbrother committed Oct 3, 2024
1 parent edc5b0c commit 64d367c
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 37 deletions.
34 changes: 1 addition & 33 deletions lib/ain-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse_macro_input, Data, DeriveInput, Field, Fields, FieldsNamed, ItemFn, ItemStruct,
parse_macro_input, Data, DeriveInput, Fields, ItemFn,
ReturnType, Type,
};

Expand Down Expand Up @@ -185,35 +185,3 @@ pub fn test_dftx_serialization(_attr: TokenStream, item: TokenStream) -> TokenSt

TokenStream::from(output)
}

#[proc_macro_attribute]
pub fn skip_serialize_none(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(item as ItemStruct);

if let Fields::Named(FieldsNamed { named, .. }) = &mut input.fields {
for field in named.iter_mut() {
if is_option(&field.ty) {
add_skip_serialize_if_attribute(field);
}
}
}

quote!(#input).into()
}

fn is_option(ty: &syn::Type) -> bool {
if let syn::Type::Path(typepath) = ty {
typepath
.path
.segments
.last()
.map_or(false, |seg| seg.ident == "Option")
} else {
false
}
}

fn add_skip_serialize_if_attribute(field: &mut Field) {
let attr = syn::parse_quote!(#[serde(skip_serializing_if = "Option::is_none")]);
field.attrs.push(attr);
}
2 changes: 2 additions & 0 deletions lib/ain-ocean/src/api/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct History {
txno: u32,
}

#[skip_serializing_none]
#[derive(Debug, Serialize)]
struct AddressHistory {
owner: String,
Expand Down Expand Up @@ -426,6 +427,7 @@ pub struct ScriptUnspentScriptResponse {
pub hex: String,
}

#[skip_serializing_none]
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptUnspentVoutResponse {
Expand Down
2 changes: 2 additions & 0 deletions lib/ain-ocean/src/api/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use axum::{routing::get, Extension, Router};
use bitcoin::Txid;
use defichain_rpc::{json::governance::*, GovernanceRPC};
use serde::Deserialize;
use serde_with::skip_serializing_none;

use super::{
path::Path,
Expand All @@ -14,6 +15,7 @@ use super::{
};
use crate::{error::ApiError, model::ApiProposalInfo, Result};

#[skip_serializing_none]
#[derive(Deserialize, Default)]
pub struct GovernanceQuery {
#[serde(flatten)]
Expand Down
6 changes: 6 additions & 0 deletions lib/ain-ocean/src/api/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use defichain_rpc::{
use futures::future::try_join_all;
use log::trace;
use serde::{Serialize, Serializer};
use serde_with::skip_serializing_none;
use snafu::OptionExt;

use super::{
Expand Down Expand Up @@ -95,6 +96,7 @@ async fn get_scheme(
Ok(Response::new(scheme.into()))
}

#[skip_serializing_none]
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CollateralToken {
Expand Down Expand Up @@ -219,6 +221,7 @@ async fn get_collateral_token(
)))
}

#[skip_serializing_none]
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LoanToken {
Expand Down Expand Up @@ -571,13 +574,15 @@ pub struct VaultLiquidatedResponse {
pub batches: Vec<VaultLiquidatedBatchResponse>,
}

#[skip_serializing_none]
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct HighestBidResponse {
pub owner: String,
pub amount: Option<VaultTokenAmountResponse>,
}

#[skip_serializing_none]
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct VaultLiquidatedBatchResponse {
Expand All @@ -588,6 +593,7 @@ pub struct VaultLiquidatedBatchResponse {
froms: Vec<String>,
}

#[skip_serializing_none]
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct VaultTokenAmountResponse {
Expand Down
2 changes: 2 additions & 0 deletions lib/ain-ocean/src/api/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bitcoin::{hashes::Hash, Txid};
use indexmap::IndexSet;
use rust_decimal::{prelude::ToPrimitive, Decimal};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use snafu::OptionExt;

use super::{
Expand Down Expand Up @@ -352,6 +353,7 @@ async fn get_feed_with_interval(
}))
}

#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PriceOracleResponse {
Expand Down
3 changes: 2 additions & 1 deletion lib/ain-ocean/src/api/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use defichain_rpc::{
};
use serde::Serialize;
use serde_json::json;
use serde_with::{serde_as, DisplayFromStr};
use serde_with::{serde_as, skip_serializing_none, DisplayFromStr};

use super::{
common::parse_display_symbol,
Expand All @@ -28,6 +28,7 @@ pub struct TxHeight {
height: i64,
}

#[skip_serializing_none]
#[serde_as]
#[derive(Serialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
Expand Down
6 changes: 4 additions & 2 deletions lib/ain-ocean/src/api/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::sync::Arc;

use ain_macros::{ocean_endpoint, skip_serialize_none};
use ain_macros::ocean_endpoint;
use axum::{extract::Query, routing::get, Extension, Router};
use bitcoin::Txid;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use super::{path::Path, query::PaginationQuery, response::ApiPagedResponse, AppContext};
use crate::{
Expand Down Expand Up @@ -33,7 +34,7 @@ async fn get_transaction(
Ok(Response::new(transactions))
}

#[skip_serialize_none]
#[skip_serializing_none]
#[derive(Debug, Serialize)]
struct TransactionVinResponse {
pub id: String,
Expand Down Expand Up @@ -97,6 +98,7 @@ async fn get_vins(
}))
}

#[skip_serializing_none]
#[derive(Debug, Serialize)]
struct TransactionVoutResponse {
pub id: String,
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/model/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub struct ApiProposalInfo {
pub vote_info: ApiProposalVoteInfo,
}

#[skip_serializing_none]
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiProposalConfidenceVote {
pub amount: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payout_address: Option<String>,
}

Expand Down

0 comments on commit 64d367c

Please sign in to comment.