Open demosdemon opened 10 months ago
{ "definitions": { "Data": { "type": "object", "properties": { "frequency": { "type": "number", "default": 0.1 }, "amount": { "type": "integer", "default": 42 }, "reason": { "type": "string", "default": "I'm a teapot" } } } } }
$ cargo typify --no-builder example.json
#![allow(clippy::redundant_closure_call)] #![allow(clippy::needless_lifetimes)] #![allow(clippy::match_single_binding)] #![allow(clippy::clone_on_copy)] use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Data { #[serde(default = "defaults::default_u64::<i64, 42>")] pub amount: i64, #[serde(default, skip_serializing_if = "Option::is_none")] pub frequency: Option<f64>, #[serde(default = "defaults::data_reason")] pub reason: String, } impl From<&Data> for Data { fn from(value: &Data) -> Self { value.clone() } } pub mod defaults { pub(super) fn default_u64<T, const V: u64>() -> T where T: std::convert::TryFrom<u64>, <T as std::convert::TryFrom<u64>>::Error: std::fmt::Debug, { T::try_from(V).unwrap() } pub(super) fn data_reason() -> String { "I'm a teapot".to_string() } }
frequency is expected to not be optional and use a default function like reason.
frequency
reason
$ cargo typify --version cargo-typify 0.0.14
Real version is: c9d6453fc3cf69726d539925b838b267f886cb53 (main)
Thanks for this. Our handling of floats is pretty lousy right now since they don't come up much for our own uses. In the spirit of transparency, it's probably going to be a low priority for us to fix this.
Example
Schema
Invocation
Output
Expected
frequency
is expected to not be optional and use a default function likereason
.Meta
Real version is: c9d6453fc3cf69726d539925b838b267f886cb53 (main)