Skip to content

Commit

Permalink
feat: path and deserialize document to hook
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker committed Jan 19, 2024
1 parent bdfd6eb commit f55023e
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 31 deletions.
42 changes: 35 additions & 7 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ members = [
"src/observatory",
"src/orbiter",
"src/libs/satellite",
"src/libs/macros"
"src/libs/macros",
"src/libs/utils"
]
resolver = "2"

Expand Down
11 changes: 6 additions & 5 deletions src/libs/satellite/src/hooks.rs
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);
}
}
4 changes: 2 additions & 2 deletions src/libs/satellite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod assert;
mod controllers;
mod db;
pub mod db;
mod guards;
pub mod hooks;
mod impls;
Expand Down Expand Up @@ -147,7 +147,7 @@ pub fn set_doc(collection: CollectionKey, key: Key, doc: SetDoc) -> Doc {

match result {
Ok(doc) => {
invoke_hook();
invoke_hook(doc.clone());

doc
}
Expand Down
6 changes: 4 additions & 2 deletions src/libs/satellite/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ pub mod memory {
}

pub mod hooks {
pub trait DocHooks: Send {
fn on_set_doc(&self);
use crate::db::types::state::Doc;

pub trait SatelliteHooks: Send {
fn on_set_doc(&self, doc: Doc);
}
}
2 changes: 1 addition & 1 deletion src/libs/utils/src/serializers/principal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::serializers::types::json::Principal;
use candid::Principal as CandidPrincipal;
use serde::de::{self, MapAccess, Visitor};
use serde::ser::SerializeStruct;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use candid::{Principal as CandidPrincipal};

impl Serialize for Principal {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down
4 changes: 4 additions & 0 deletions src/satellite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ crate-type = ["cdylib"]
candid.workspace = true
ic-cdk.workspace = true
ic-cdk-macros.workspace = true
serde.workspace = true
serde_json = "1.0.111"
junobuild-satellite = { path = "../libs/satellite" }
junobuild-macros = { path = "../libs/macros" }
junobuild-utils = { path = "../libs/utils" }
shared = { path = "../shared" }

76 changes: 63 additions & 13 deletions src/satellite/src/lib.rs
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!();

0 comments on commit f55023e

Please sign in to comment.