Skip to content

Commit

Permalink
impl node props as json, impl custom tokenizer and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-880 committed Dec 9, 2024
1 parent cb95285 commit e997eee
Show file tree
Hide file tree
Showing 3 changed files with 326 additions and 166 deletions.
47 changes: 47 additions & 0 deletions raphtory/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use chrono::{DateTime, NaiveDateTime, Utc};
use itertools::Itertools;
use raphtory_api::core::storage::arc_str::ArcStr;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::{
cmp::Ordering,
collections::HashMap,
Expand Down Expand Up @@ -757,6 +758,52 @@ impl<T: Into<Prop>> IntoProp for T {
}
}

impl From<Prop> for Value {
fn from(prop: Prop) -> Self {
match prop {
Prop::Str(value) => Value::String(value.to_string()),
Prop::U8(value) => Value::Number(value.into()),
Prop::U16(value) => Value::Number(value.into()),
Prop::I32(value) => Value::Number(value.into()),
Prop::I64(value) => Value::Number(value.into()),
Prop::U32(value) => Value::Number(value.into()),
Prop::U64(value) => Value::Number(value.into()),
Prop::F32(value) => serde_json::Number::from_f64(value as f64)
.map(Value::Number)
.unwrap_or(Value::Null),
Prop::F64(value) => serde_json::Number::from_f64(value)
.map(Value::Number)
.unwrap_or(Value::Null),
Prop::Bool(value) => Value::Bool(value),
Prop::List(values) => Value::Array(values.iter().cloned().map(Value::from).collect()),
Prop::Map(map) => {
let json_map: serde_json::Map<String, Value> = map
.iter()
.map(|(k, v)| (k.to_string(), Value::from(v.clone())))
.collect();
Value::Object(json_map)
}
Prop::NDTime(value) => Value::String(value.to_string()),
Prop::DTime(value) => Value::String(value.to_string()),
Prop::Document(doc) => json!({
"content": doc.content,
"life": Value::from(doc.life),
}),
_ => Value::Null,
}
}
}

impl From<Lifespan> for Value {
fn from(lifespan: Lifespan) -> Self {
match lifespan {
Lifespan::Interval { start, end } => json!({ "start": start, "end": end }),
Lifespan::Event { time } => json!({ "time": time }),
Lifespan::Inherited => Value::String("inherited".to_string()),
}
}
}

#[cfg(feature = "io")]
mod serde_value_into_prop {
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion raphtory/src/db/api/view/internal/core_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
storage::{
locked_view::LockedView,
timeindex::{TimeIndex, TimeIndexIntoOps, TimeIndexOps, TimeIndexWindow},
timeindex::{TimeIndex, TimeIndexOps, TimeIndexWindow},
},
Prop,
},
Expand Down
Loading

0 comments on commit e997eee

Please sign in to comment.