From 845a5329532364b5fcbed829ef1352eaa2d38a4f Mon Sep 17 00:00:00 2001 From: Marcin Olichwiruk <21108638+olichwiruk@users.noreply.github.com> Date: Sat, 20 Jan 2024 14:07:57 +0100 Subject: [PATCH] feat: add bindings for check_condition function --- .../test/checking_attribute_condition.test.ts | 19 +++++++ .../test/parsing-attribute-types.test.ts | 6 +-- js/example/yarn.lock | 2 +- js/wasm/Cargo.lock | 10 ++-- js/wasm/Cargo.toml | 6 +-- js/wasm/src/lib.rs | 50 ++++++++++++++++--- 6 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 js/example/test/checking_attribute_condition.test.ts diff --git a/js/example/test/checking_attribute_condition.test.ts b/js/example/test/checking_attribute_condition.test.ts new file mode 100644 index 0000000..746fc40 --- /dev/null +++ b/js/example/test/checking_attribute_condition.test.ts @@ -0,0 +1,19 @@ +import { expect } from "chai"; +import { Attribute, OCABox, create_nested_attr_type_from_js } from "oca.js"; + +describe("Attribute with condition is built", () => { + try { + const textTypeJs = create_nested_attr_type_from_js("Text"); + + const attribute = new Attribute("name") + .setAttributeType(textTypeJs) + .setCondition("${age} > 18"); + + it("check condition", () => { + expect(attribute.checkCondition({ age: 20 })).to.be.true; + expect(attribute.checkCondition({ age: 18 })).to.be.false; + }); + } catch (error) { + console.error("Error checking attribute condition", error); + } +}); diff --git a/js/example/test/parsing-attribute-types.test.ts b/js/example/test/parsing-attribute-types.test.ts index c44e6eb..999d1af 100644 --- a/js/example/test/parsing-attribute-types.test.ts +++ b/js/example/test/parsing-attribute-types.test.ts @@ -13,10 +13,8 @@ describe('Parsing attribute types', () => { }), it("shouldn't be parsed", () => { - expect( () => create_nested_attr_type_from_js("Wrong")).to.throw("Can't parse attribute type: Wrong") - // expect( () => create_nested_attr_type_from_js("Wrong")).to.throw("Attribute type Wrong doesn't exist") + expect( () => create_nested_attr_type_from_js("Wrong")).to.throw("Attribute type Wrong doesn't exist") const reference = "refs:not_said"; - expect( () => create_nested_attr_type_from_js(reference)).to.throw("Can't parse attribute type: refs:not_said") - // expect( () => create_nested_attr_type_from_js(reference)).to.throw("Invalid said: Unknown code") + expect( () => create_nested_attr_type_from_js(reference)).to.throw("Invalid said: Unknown code") }) }) \ No newline at end of file diff --git a/js/example/yarn.lock b/js/example/yarn.lock index 113a825..4297938 100644 --- a/js/example/yarn.lock +++ b/js/example/yarn.lock @@ -2583,7 +2583,7 @@ object.pick@^1.3.0: isobject "^3.0.1" "oca.js@file:../wasm/pkg": - version "0.3.7" + version "0.4.1-rc.6" once@^1.3.0, once@^1.3.1, once@^1.4.0: version "1.4.0" diff --git a/js/wasm/Cargo.lock b/js/wasm/Cargo.lock index 34b2a06..919a5e4 100644 --- a/js/wasm/Cargo.lock +++ b/js/wasm/Cargo.lock @@ -667,9 +667,9 @@ dependencies = [ [[package]] name = "oca-ast" -version = "0.4.1-rc.2" +version = "0.4.1-rc.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15bae864eef893df8cd7428ae413d30e433356b8938a1f916c8eb38d8bd67431" +checksum = "88774a19419e192a93077aa97f955417d819b4ee16f1b8e9c3e28bd10c157a4c" dependencies = [ "env_logger", "indexmap 1.9.3", @@ -687,9 +687,9 @@ dependencies = [ [[package]] name = "oca-bundle" -version = "0.4.1-rc.2" +version = "0.4.1-rc.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8bfde45621e900c8d9b2c3fb45917e0ce3274f68d6266bc0bf39312bcb3f5e4" +checksum = "5e1535446dcf0129f15a91095ae00a4f60b8f355ef061c171f6d83bced3bd792" dependencies = [ "cascade", "convert_case", @@ -716,7 +716,7 @@ dependencies = [ [[package]] name = "oca-js" -version = "0.4.1-rc.2" +version = "0.4.1-rc.6" dependencies = [ "isolang", "oca-ast", diff --git a/js/wasm/Cargo.toml b/js/wasm/Cargo.toml index cc38bdb..ca18b7b 100644 --- a/js/wasm/Cargo.toml +++ b/js/wasm/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "oca-js" description = "Bindings for the OCA in JavaScript" -version = "0.4.1-rc.2" +version = "0.4.1-rc.6" license = "EUPL-1.2" edition = "2021" authors = [ @@ -25,8 +25,8 @@ strip = "debuginfo" [dependencies] isolang = { version = "2.3.0", features = ["serde"] } -oca-bundle = { version = "0.4.1-rc.2", features = ["format_overlay"] } -oca-ast = { version = "0.4.1-rc.2" } +oca-bundle = { version = "0.4.1-rc.6", features = ["format_overlay"] } +oca-ast = { version = "0.4.1-rc.6" } serde = "1.0.130" serde-wasm-bindgen = "0.5.0" serde_json = "1.0.105" diff --git a/js/wasm/src/lib.rs b/js/wasm/src/lib.rs index aecd0e6..5d3c2d7 100644 --- a/js/wasm/src/lib.rs +++ b/js/wasm/src/lib.rs @@ -22,7 +22,7 @@ use oca_bundle::state::{ }; use oca_bundle::Encode; use serde::Serialize; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -49,6 +49,8 @@ extern "C" { pub type EntryCodesMapping; #[wasm_bindgen(typescript_type = "string[]")] pub type Dependencies; + #[wasm_bindgen(typescript_type = "string | IAttribute")] + pub type AttributeConstructor; } #[wasm_bindgen] @@ -255,17 +257,25 @@ pub struct Attribute { } #[wasm_bindgen] -pub fn create_nested_attr_type_from_js(value: JsValue) -> Result { - NestedAttrType::from_js_value(value).and_then(|attr_type| attr_type.to_js_value()) +pub fn create_nested_attr_type_from_js( + value: JsValue, +) -> Result { + NestedAttrType::from_js_value(value) + .and_then(|attr_type| attr_type.to_js_value()) } #[wasm_bindgen] impl Attribute { #[wasm_bindgen(constructor)] - pub fn new(name: String) -> Self { - Self { - raw: AttributeRaw::new(name), - } + pub fn new(name_or_object: AttributeConstructor) -> Self { + let raw = if name_or_object.is_string() { + let name = name_or_object.as_string().unwrap(); + AttributeRaw::new(name) + } else { + serde_wasm_bindgen::from_value(name_or_object.into()).unwrap() + }; + + Self { raw } } #[wasm_bindgen(js_name = "setAttributeType")] @@ -312,6 +322,32 @@ impl Attribute { self } + #[wasm_bindgen(js_name = "checkCondition")] + pub fn check_condition(&self, data: JsValue) -> Result { + let data_raw_value: serde_json::Value = + serde_wasm_bindgen::from_value(data) + .map_err(|err| JsValue::from(format!("{:?}", err)))?; + + let data_raw_o = data_raw_value + .as_object() + .ok_or(JsValue::from("data must be an object"))?; + let data_raw: BTreeMap> = + data_raw_o + .iter() + .map(|(key, value)| { + ( + key.clone(), + Box::new(value.clone()) + as Box, + ) + }) + .collect(); + + self.raw + .check_condition(data_raw) + .map_err(|err| JsValue::from(serde_json::to_string(&err).unwrap())) + } + #[wasm_bindgen(js_name = "setConformance")] pub fn set_conformance(mut self, conformance: ConformanceOptions) -> Self { let conformance_raw: String = serde_wasm_bindgen::from_value(conformance.into()).unwrap();