-
Notifications
You must be signed in to change notification settings - Fork 107
Adding read/write json methods to entity #924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ use miette::Diagnostic; | |
use ref_cast::RefCast; | ||
use smol_str::SmolStr; | ||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; | ||
use std::io::Read; | ||
use std::str::FromStr; | ||
|
||
/// Extended functionality for `Entities` struct | ||
|
@@ -260,6 +261,84 @@ impl Entity { | |
ancestors.into_iter().map(EntityUid::new).collect(), | ||
) | ||
} | ||
|
||
/// Parse an entity from an in-memory JSON value | ||
/// If a schema is provided, it is handled identically to | ||
/// [Entities](https://docs.rs/cedar-policy/latest/cedar_policy/struct.Entities.html#method.from_json_str) | ||
|
||
pub fn from_json_value( | ||
value: serde_json::Value, | ||
schema: Option<&Schema>, | ||
) -> Result<Self, EntitiesError> { | ||
let schema = schema.map(|s| cedar_policy_validator::CoreSchema::new(&s.0)); | ||
let eparser = cedar_policy_core::entities::EntityJsonParser::new( | ||
schema.as_ref(), | ||
Extensions::all_available(), | ||
cedar_policy_core::entities::TCComputation::ComputeNow, | ||
); | ||
eparser.single_from_json_value(value).map(Self) | ||
} | ||
|
||
/// Parse an entity from a JSON string | ||
/// If a schema is provided, it is handled identically to | ||
/// [Entities](https://docs.rs/cedar-policy/latest/cedar_policy/struct.Entities.html#method.from_json_str) | ||
pub fn from_json_str( | ||
src: impl AsRef<str>, | ||
schema: Option<&Schema>, | ||
) -> Result<Self, EntitiesError> { | ||
let schema = schema.map(|s| cedar_policy_validator::CoreSchema::new(&s.0)); | ||
let eparser = cedar_policy_core::entities::EntityJsonParser::new( | ||
schema.as_ref(), | ||
Extensions::all_available(), | ||
cedar_policy_core::entities::TCComputation::ComputeNow, | ||
); | ||
eparser.single_from_json_str(src).map(Self) | ||
} | ||
|
||
/// Parse an entity from a JSON reader | ||
/// If a schema is provided, it is handled identically to | ||
/// [Entities](https://docs.rs/cedar-policy/latest/cedar_policy/struct.Entities.html#method.from_json_str) | ||
pub fn from_json_file(f: impl Read, schema: Option<&Schema>) -> Result<Self, EntitiesError> { | ||
let schema = schema.map(|s| cedar_policy_validator::CoreSchema::new(&s.0)); | ||
let eparser = cedar_policy_core::entities::EntityJsonParser::new( | ||
schema.as_ref(), | ||
Extensions::all_available(), | ||
cedar_policy_core::entities::TCComputation::ComputeNow, | ||
); | ||
eparser.single_from_json_file(f).map(Self) | ||
} | ||
|
||
/// Dump an `Entity` object into an entity JSON file. | ||
/// | ||
/// The resulting JSON will be suitable for parsing in via | ||
/// `from_json_*`, and will be parse-able even with no [`Schema`]. | ||
/// | ||
/// To read an `Entity` object from JSON , use | ||
/// [`from_json_file`], [`from_json_value`], or [`from_json_str`]. | ||
pub fn write_to_json(&self, f: impl std::io::Write) -> Result<(), EntitiesError> { | ||
self.0.write_to_json(f) | ||
} | ||
aaronjeline marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Dump an `Entity` object into an in-memory JSON object. | ||
/// | ||
/// The resulting JSON will be suitable for parsing in via | ||
/// `from_json_*`, and will be parse-able even with no `Schema`. | ||
/// | ||
/// To read an `Entity` object from JSON , use | ||
/// [`from_json_file`], [`from_json_value`], or [`from_json_str`]. | ||
pub fn to_json_value(&self) -> Result<serde_json::Value, EntitiesError> { | ||
self.0.to_json_value() | ||
} | ||
|
||
/// Dump an `Entity` object into a JSON string. | ||
/// | ||
/// The resulting JSON will be suitable for parsing in via | ||
/// `from_json_*`, and will be parse-able even with no `Schema`. | ||
/// | ||
/// To read an `Entity` object from JSON , use | ||
/// [`from_json_file`], [`from_json_value`], or [`from_json_str`]. | ||
pub fn to_json_string(&self) -> Result<String, EntitiesError> { | ||
self.0.to_json_string() | ||
} | ||
} | ||
|
||
impl std::fmt::Display for Entity { | ||
|
Uh oh!
There was an error while loading. Please reload this page.