-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: path and deserialize document to hook
Signed-off-by: David Dal Busco <[email protected]>
- Loading branch information
1 parent
bdfd6eb
commit f55023e
Showing
8 changed files
with
117 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
use crate::types::hooks::DocHooks; | ||
use crate::db::types::state::Doc; | ||
use crate::types::hooks::SatelliteHooks; | ||
use lazy_static::lazy_static; | ||
use std::sync::Mutex; | ||
|
||
lazy_static! { | ||
static ref HOOKS: Mutex<Option<Box<dyn DocHooks>>> = Mutex::new(None); | ||
static ref HOOKS: Mutex<Option<Box<dyn SatelliteHooks>>> = Mutex::new(None); | ||
} | ||
|
||
pub fn register_hooks(hooks: Box<dyn DocHooks>) { | ||
pub fn register_hooks(hooks: Box<dyn SatelliteHooks>) { | ||
let mut heap_hooks = HOOKS.lock().unwrap(); | ||
*heap_hooks = Some(hooks); | ||
} | ||
|
||
pub fn invoke_hook() { | ||
pub fn invoke_hook(doc: Doc) { | ||
let hook = HOOKS.lock().unwrap(); | ||
|
||
if let Some(ref hook) = *hook { | ||
hook.on_set_doc(); | ||
hook.on_set_doc(doc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,73 @@ | ||
use ic_cdk::print; | ||
use ic_cdk_macros::export_candid; | ||
use junobuild_macros::init_satellite; | ||
use junobuild_satellite::include_satellite; | ||
use junobuild_satellite::types::hooks::DocHooks; | ||
use junobuild_satellite::db::types::state::Doc; | ||
use junobuild_satellite::types::hooks::SatelliteHooks; | ||
use junobuild_utils::serializers::types::json::Principal; | ||
use junobuild_utils::serializers::types::BigInt; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{from_slice, from_str, Value}; | ||
|
||
struct MyDocHooks; | ||
struct MyHooks; | ||
|
||
impl DocHooks for MyDocHooks { | ||
fn on_set_doc(&self) { | ||
#[derive(Serialize, Deserialize)] | ||
struct Person { | ||
yolo: bool, | ||
hello: String, | ||
value: BigInt, | ||
user: Principal, | ||
} | ||
|
||
impl SatelliteHooks for MyHooks { | ||
fn on_set_doc(&self, doc: Doc) { | ||
print("On set doc called!"); | ||
|
||
match from_slice::<Person>(&doc.data) { | ||
Ok(data) => print(format!( | ||
"Str -> JSON OK {} {}", | ||
data.value, | ||
data.user.value.to_text() | ||
)), | ||
Err(err) => print(format!("Error str -> JSON {}", err)), | ||
} | ||
|
||
match from_slice::<Value>(&doc.data) { | ||
Ok(data) => { | ||
let d = data.get("__bigint__"); | ||
|
||
match d { | ||
Some(d) => print(format!("Str -> JSON OK bigint {}", d)), | ||
None => print("No JSON bigint"), | ||
} | ||
} | ||
Err(err) => print(format!("Error str -> JSON {}", err)), | ||
} | ||
|
||
if let Ok(v) = from_slice::<Value>(&doc.data) { | ||
print(format!("ça marche {}", v["user"])); | ||
} | ||
|
||
// Some JSON input data as a &str. Maybe this comes from the user. | ||
let data = r#" | ||
{ | ||
"name": "John Doe", | ||
"age": 43, | ||
"phones": [ | ||
"+44 1234567", | ||
"+44 2345678" | ||
] | ||
}"#; | ||
|
||
// Parse the string of data into serde_json::Value. | ||
if let Ok(v) = from_str::<Value>(data) { | ||
print(format!( | ||
"Please call {} at the number {}", | ||
v["name"], v["phones"][0] | ||
)); | ||
} | ||
} | ||
} | ||
|
||
#[init_satellite] | ||
fn init() -> Box<dyn DocHooks> { | ||
print("Crate register"); | ||
Box::new(MyDocHooks) | ||
fn init() -> Box<dyn SatelliteHooks> { | ||
Box::new(MyHooks) | ||
} | ||
|
||
include_satellite!(); | ||
|
||
export_candid!(); |