Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion typify-impl/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl TypeEntry {
if value == 0.0 {
Ok(DefaultKind::Intrinsic)
} else {
Ok(DefaultKind::Generic(DefaultImpl::I64))
Ok(DefaultKind::Specific)
}
} else {
Err(Error::invalid_value())
Expand Down Expand Up @@ -794,6 +794,24 @@ mod tests {
));
}

#[test]
fn test_default_float() {
let (type_space, type_id) = get_type::<f64>();
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();

assert!(type_entry
.validate_value(&type_space, &json!(true))
.is_err());
assert!(matches!(
type_entry.validate_value(&type_space, &json!(0.0)),
Ok(DefaultKind::Intrinsic),
));
assert!(matches!(
type_entry.validate_value(&type_space, &json!(0.1)),
Ok(DefaultKind::Specific),
));
}

#[test]
fn test_struct_simple() {
#[derive(JsonSchema)]
Expand Down
4 changes: 2 additions & 2 deletions typify-impl/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,12 @@ fn has_default(
}
// Default specified is the same as the implicit default: 0
(Some(TypeEntryDetails::Integer(_)), Some(serde_json::Value::Number(n)))
if n.as_u64() == Some(0) =>
if n.as_f64() == Some(0.0) =>
{
StructPropertyState::Optional
}
// Default specified is the same as the implicit default: 0.0
(Some(TypeEntryDetails::Integer(_)), Some(serde_json::Value::Number(n)))
(Some(TypeEntryDetails::Float(_)), Some(serde_json::Value::Number(n)))
if n.as_f64() == Some(0.0) =>
{
StructPropertyState::Optional
Expand Down
7 changes: 0 additions & 7 deletions typify-impl/tests/vega.out
Original file line number Diff line number Diff line change
Expand Up @@ -106275,13 +106275,6 @@ pub mod defaults {
pub(super) fn default_bool<const V: bool>() -> bool {
V
}
pub(super) fn default_i64<T, const V: i64>() -> T
where
T: ::std::convert::TryFrom<i64>,
<T as ::std::convert::TryFrom<i64>>::Error: ::std::fmt::Debug,
{
T::try_from(V).unwrap()
}
pub(super) fn aggregate_transform_drop() -> super::AggregateTransformDrop {
super::AggregateTransformDrop::Boolean(true)
}
Expand Down
13 changes: 13 additions & 0 deletions typify/tests/schemas/types-with-defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@
}
}
},
"MrDefaultFloats": {
"type": "object",
"properties": {
"frequency": {
"type": "number",
"default": 0.1
},
"zfreq": {
"type": "number",
"default": 0.0
}
}
},
"UInt": {
"type": "integer"
},
Expand Down
97 changes: 97 additions & 0 deletions typify/tests/schemas/types-with-defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,46 @@ impl Doodad {
Default::default()
}
}
#[doc = "`MrDefaultFloats`"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"type\": \"object\","]
#[doc = " \"properties\": {"]
#[doc = " \"frequency\": {"]
#[doc = " \"default\": 0.1,"]
#[doc = " \"type\": \"number\""]
#[doc = " },"]
#[doc = " \"zfreq\": {"]
#[doc = " \"default\": 0.0,"]
#[doc = " \"type\": \"number\""]
#[doc = " }"]
#[doc = " }"]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct MrDefaultFloats {
#[serde(default = "defaults::mr_default_floats_frequency")]
pub frequency: f64,
#[serde(default)]
pub zfreq: f64,
}
impl ::std::default::Default for MrDefaultFloats {
fn default() -> Self {
Self {
frequency: defaults::mr_default_floats_frequency(),
zfreq: Default::default(),
}
}
}
impl MrDefaultFloats {
pub fn builder() -> builder::MrDefaultFloats {
Default::default()
}
}
#[doc = "`MrDefaultNumbers`"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
Expand Down Expand Up @@ -391,6 +431,60 @@ pub mod builder {
}
}
#[derive(Clone, Debug)]
pub struct MrDefaultFloats {
frequency: ::std::result::Result<f64, ::std::string::String>,
zfreq: ::std::result::Result<f64, ::std::string::String>,
}
impl ::std::default::Default for MrDefaultFloats {
fn default() -> Self {
Self {
frequency: Ok(super::defaults::mr_default_floats_frequency()),
zfreq: Ok(Default::default()),
}
}
}
impl MrDefaultFloats {
pub fn frequency<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<f64>,
T::Error: ::std::fmt::Display,
{
self.frequency = value
.try_into()
.map_err(|e| format!("error converting supplied value for frequency: {e}"));
self
}
pub fn zfreq<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<f64>,
T::Error: ::std::fmt::Display,
{
self.zfreq = value
.try_into()
.map_err(|e| format!("error converting supplied value for zfreq: {e}"));
self
}
}
impl ::std::convert::TryFrom<MrDefaultFloats> for super::MrDefaultFloats {
type Error = super::error::ConversionError;
fn try_from(
value: MrDefaultFloats,
) -> ::std::result::Result<Self, super::error::ConversionError> {
Ok(Self {
frequency: value.frequency?,
zfreq: value.zfreq?,
})
}
}
impl ::std::convert::From<super::MrDefaultFloats> for MrDefaultFloats {
fn from(value: super::MrDefaultFloats) -> Self {
Self {
frequency: Ok(value.frequency),
zfreq: Ok(value.zfreq),
}
}
}
#[derive(Clone, Debug)]
pub struct MrDefaultNumbers {
big_nullable: ::std::result::Result<
::std::option::Option<::std::num::NonZeroU64>,
Expand Down Expand Up @@ -672,6 +766,9 @@ pub mod defaults {
)
.unwrap()
}
pub(super) fn mr_default_floats_frequency() -> f64 {
0.1_f64
}
pub(super) fn mr_default_numbers_big_nullable() -> ::std::option::Option<::std::num::NonZeroU64>
{
::std::option::Option::Some(::std::num::NonZeroU64::new(1).unwrap())
Expand Down