Skip to content

Commit

Permalink
fix: respect fractional part when needed only (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez authored Jun 13, 2024
1 parent 7c1766e commit d4541b1
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions integrationos-domain/src/domain/schema/json_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,31 @@ pub fn map_data_by_schema(
}

if let Some(FieldDefault { value, .. }) = default {
let default_num = value
.as_deref()
.unwrap_or("0")
.parse::<f64>()
.map(serde_json::Number::from_f64);
let num_as_str = value.as_deref().ok_or_else(|| {
InternalError::configuration_error(
&format!("Invalid default value for number field: {}", key),
None,
)
})?;

match default_num {
Ok(Some(num)) => return Ok(Value::Number(num)),
_ => {
return Err(InternalError::configuration_error(
&format!("Invalid default value for number field: {}", key),
None,
));
if num_as_str.contains('.') {
let num_f64 = num_as_str.parse::<f64>().map(Value::from);

if let Ok(num) = num_f64 {
return Ok(num);
}
} else {
let num_i64 = num_as_str.parse::<i64>().map(Value::from);

if let Ok(num) = num_i64 {
return Ok(num);
}
}

return Err(InternalError::configuration_error(
&format!("Invalid default value for number field: {}", key),
None,
));
}

if required {
Expand Down Expand Up @@ -660,7 +670,7 @@ mod tests {
"products_list": [
{
"name": "Product1",
"value": 0.0,
"value": 0,
"id": "789"
},
{
Expand Down

0 comments on commit d4541b1

Please sign in to comment.