Skip to content

Commit

Permalink
Dev #100
Browse files Browse the repository at this point in the history
  • Loading branch information
vulogov committed Oct 29, 2024
1 parent fe47549 commit 7adbcef
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rust_dynamic"
description = "Support for dynamically-typed values in run-time"
version = "0.32.0"
version = "0.33.0"
edition = "2021"
license-file = "LICENSE"
repository = "https://github.com/vulogov/rust_dynamic"
Expand Down
4 changes: 4 additions & 0 deletions Documentation/RELEASE_NOTES_0.33.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# What's new in 0.33.0

* Fix dict and list allocation in Value::cast_json_to_value()
* Add JSON Array support for Value::fmap
4 changes: 2 additions & 2 deletions src/cast_json_to_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Value {
let jv = Value::json(v.clone());
match jv.cast_json_to_value() {
Ok(ja_val) => {
res.push(ja_val);
res = res.push(ja_val);
}
Err(err) => {
return Err(format!("JSON returns error during casting: {}", err).into());
Expand All @@ -79,7 +79,7 @@ impl Value {
let jv = Value::json(j_val_value);
match jv.cast_json_to_value() {
Ok(ja_val) => {
res.set(n, ja_val);
res = res.set(n, ja_val);
}
Err(err) => {
return Err(format!("JSON returns error during casting: {}", err).into());
Expand Down
21 changes: 21 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ use crate::types::*;
impl Value {
pub fn fmap(&mut self, appfn: AppFn) -> Self {
match self.dt {
JSON => {
let mut data: Vec<Value> = Vec::new();
match &self.data {
Val::Json(v) => {
if v.is_array() {
for n in v.as_array().unwrap() {
match Value::json(n.clone()).cast_json_to_value() {
Ok(value) => {
data.push(appfn(value));
}
Err(_) => {
continue;
}
}
}
}
}
_ => {},
}
return Value::from_list(data);
}
LIST | RESULT => {
let mut data: Vec<Value> = Vec::new();
match &self.data {
Expand Down
18 changes: 18 additions & 0 deletions tests/json-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,22 @@ mod tests {
let v = Value::pair(Value::from_int(1), Value::from_int(2)).as_json_value();
assert_eq!(v.as_array().unwrap()[0].as_i64().unwrap(), 1);
}

#[test]
fn test_list_to_from_json1() {
let mut v = Value::list();
v = v.push(Value::from(42.0).unwrap());
let j_value = v.cast_value_to_json().unwrap();
assert_eq!(j_value.as_array().unwrap().len(), 1);
}

#[test]
fn test_list_to_from_json2() {
let mut v = Value::list();
v = v.push(Value::from(42.0).unwrap());
let j_value = v.cast_value_to_json().unwrap();
let j_object = Value::json(j_value);
let j_value2 = j_object.cast_json_to_value().unwrap();
assert_eq!(j_value2.cast_list().unwrap().len(), 1);
}
}
17 changes: 17 additions & 0 deletions tests/map-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ mod tests {
v = v.fmap(comp_sin);
assert_eq!(v.cast_float().unwrap(), f64::sin(42.0));
}

#[test]
fn test_map_json_array_value() {
fn comp_sin(value: Value) -> Value {
match value.data {
Val::F64(f_val) => {
return Value::from_float(f64::sin(f_val));
}
_ => return value,
}
}
let mut v = Value::json(serde_json::json!([42.0]));
let v_json_array = v.fmap(comp_sin);
let v_list = v_json_array.cast_list().unwrap();
assert_eq!(v_list[0].cast_float().unwrap(), f64::sin(42.0));
}

#[test]
fn test_map_float_map() {
fn comp_sin(value: Value) -> Value {
Expand Down

0 comments on commit 7adbcef

Please sign in to comment.