Skip to content

Commit

Permalink
feat: add bindings for check_condition function
Browse files Browse the repository at this point in the history
  • Loading branch information
olichwiruk committed Jan 20, 2024
1 parent c20f883 commit 845a532
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 20 deletions.
19 changes: 19 additions & 0 deletions js/example/test/checking_attribute_condition.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
6 changes: 2 additions & 4 deletions js/example/test/parsing-attribute-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})
2 changes: 1 addition & 1 deletion js/example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions js/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions js/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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"
Expand Down
50 changes: 43 additions & 7 deletions js/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -255,17 +257,25 @@ pub struct Attribute {
}

#[wasm_bindgen]
pub fn create_nested_attr_type_from_js(value: JsValue) -> Result<JsValue, JsValue> {
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<JsValue, JsValue> {
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")]
Expand Down Expand Up @@ -312,6 +322,32 @@ impl Attribute {
self
}

#[wasm_bindgen(js_name = "checkCondition")]
pub fn check_condition(&self, data: JsValue) -> Result<bool, JsValue> {
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<String, Box<dyn std::fmt::Display + 'static>> =
data_raw_o
.iter()
.map(|(key, value)| {
(
key.clone(),
Box::new(value.clone())
as Box<dyn std::fmt::Display + 'static>,
)
})
.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();
Expand Down

0 comments on commit 845a532

Please sign in to comment.