Skip to content

Commit

Permalink
feat(cognitarium): impl map query var to msg value
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jun 5, 2023
1 parent 1066d5a commit bc8ed42
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
49 changes: 44 additions & 5 deletions contracts/okp4-cognitarium/src/querier/variable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::msg::Value;
use crate::msg::{Value, IRI};
use crate::state::{Literal, Object, Predicate, Subject};
use cosmwasm_std::{StdError, StdResult};
use cosmwasm_std::StdResult;

#[derive(Eq, PartialEq, Debug, Clone)]
pub enum ResolvedVariable {
Expand Down Expand Up @@ -48,11 +48,50 @@ impl ResolvedVariable {
})
}

pub fn as_value<F>(&self, ns_fn: F) -> StdResult<Value>
pub fn as_value<F>(&self, ns_fn: &mut F) -> StdResult<Value>
where
F: n(u128) -> StdResult<String>,
F: FnMut(u128) -> StdResult<String>,
{
Err(StdError::generic_err("mescouilles"))
Ok(match self {
ResolvedVariable::Subject(subject) => match subject {
Subject::Named(named) => named.as_iri(ns_fn).map(|iri| Value::URI {
value: IRI::Full(iri),
})?,
Subject::Blank(blank) => Value::BlankNode {
value: blank.to_string(),
},
},
ResolvedVariable::Predicate(predicate) => {
predicate.as_iri(ns_fn).map(|iri| Value::URI {
value: IRI::Full(iri),
})?
}
ResolvedVariable::Object(object) => match object {
Object::Named(named) => Value::URI {
value: IRI::Full(named.as_iri(ns_fn)?),
},
Object::Blank(blank) => Value::BlankNode {
value: blank.to_string(),
},
Object::Literal(literal) => match literal {
Literal::Simple { value } => Value::Literal {
value: value.clone(),
lang: None,
datatype: None,
},
Literal::I18NString { value, language } => Value::Literal {
value: value.clone(),
lang: Some(language.clone()),
datatype: None,
},
Literal::Typed { value, datatype } => Value::Literal {
value: value.clone(),
lang: None,
datatype: Some(datatype.as_iri(ns_fn).map(IRI::Full)?),
},
},
},
})
}
}

Expand Down
5 changes: 4 additions & 1 deletion contracts/okp4-cognitarium/src/state/namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub const NAMESPACE_KEY_INCREMENT: Item<u128> = Item::new("namespace_key");

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Namespace {
/// The namespace value.
pub value: String,

/// The unique, incremented key issues to reference this namespace from a triple IRI.
pub key: u128,

Expand All @@ -15,7 +18,7 @@ pub struct Namespace {
}

pub struct NamespaceIndexes<'a> {
key: UniqueIndex<'a, u128, Namespace, String>,
pub key: UniqueIndex<'a, u128, Namespace, String>,
}

impl IndexList<Namespace> for NamespaceIndexes<'_> {
Expand Down
8 changes: 8 additions & 0 deletions contracts/okp4-cognitarium/src/state/triples.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use blake3::Hash;
use cosmwasm_std::StdResult;
use cw_storage_plus::{Index, IndexList, IndexedMap, MultiIndex};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -127,6 +128,13 @@ impl Node {

key
}

pub fn as_iri<F>(&self, ns_fn: &mut F) -> StdResult<String>
where
F: FnMut(u128) -> StdResult<String>,
{
ns_fn(self.namespace).map(|ns| vec![ns.as_str(), self.value.as_str()].join(""))
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
Expand Down
5 changes: 3 additions & 2 deletions contracts/okp4-cognitarium/src/storer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'a> TripleStorer<'a> {
}
None => {
let mut namespace = match namespaces().load(self.storage, ns_str.clone()) {
Err(StdError::NotFound { .. }) => Ok(self.allocate_namespace()),
Err(StdError::NotFound { .. }) => Ok(self.allocate_namespace(ns_str.clone())),
Ok(n) => Ok(n),
Err(e) => Err(e),
}?;
Expand All @@ -121,10 +121,11 @@ impl<'a> TripleStorer<'a> {
}
}

fn allocate_namespace(&mut self) -> Namespace {
fn allocate_namespace(&mut self, value: String) -> Namespace {
self.store.stat.namespace_count += Uint128::one();

let ns = Namespace {
value,
key: self.ns_key_inc_offset,
counter: 0u128,
};
Expand Down

0 comments on commit bc8ed42

Please sign in to comment.