Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cedar-policy-core/src/ast/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,18 @@ impl Entity {
serde_json::to_writer_pretty(f, &ejson).map_err(JsonSerializationError::from)?;
Ok(())
}

pub fn to_json_value(&self) -> Result<serde_json::Value, EntitiesError> {
let ejson = EntityJson::from_entity(self)?;
let v = serde_json::to_value(ejson).map_err(JsonSerializationError::from)?;
Ok(v)
}

pub fn to_json_string(&self) -> Result<String, EntitiesError> {
let ejson = EntityJson::from_entity(self)?;
let string = serde_json::to_string(&ejson).map_err(JsonSerializationError::from)?;
Ok(string)
}
}

impl PartialEq for Entity {
Expand Down
37 changes: 19 additions & 18 deletions cedar-policy-core/src/entities/json/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::{
use crate::ast::{
BorrowedRestrictedExpr, Entity, EntityType, EntityUID, PartialValue, RestrictedExpr,
};
use crate::entities::conformance::EntitySchemaConformanceChecker;
use crate::entities::{
conformance::err::{EntitySchemaConformanceError, UnexpectedEntityTypeError},
schematype_of_partialvalue, Entities, EntitiesError, GetSchemaTypeError, TCComputation,
Expand Down Expand Up @@ -218,32 +219,32 @@ impl<'e, 's, S: Schema> EntityJsonParser<'e, 's, S> {
&self,
value: serde_json::Value,
) -> Result<Entity, EntitiesError> {
let ejson: EntityJson =
serde_json::from_value(value).map_err(JsonDeserializationError::from)?;
let entity = self.parse_ejson(ejson)?;
match self.schema {
Some(schema) => {
let checker = crate::entities::conformance::EntitySchemaConformanceChecker::new(
schema,
self.extensions,
);
checker.validate_entity(&entity)?;
Ok(entity)
}
None => Ok(entity),
}
let ejson = serde_json::from_value(value).map_err(JsonDeserializationError::from)?;
self.single_from_ejson(ejson)
}

/// Parse a single entity from a JSON string
pub fn single_from_json_str(&self, src: impl AsRef<str>) -> Result<Entity, EntitiesError> {
let v = serde_json::from_str(src.as_ref()).map_err(JsonDeserializationError::from)?;
self.single_from_json_value(v)
let ejson = serde_json::from_str(src.as_ref()).map_err(JsonDeserializationError::from)?;
self.single_from_ejson(ejson)
}

/// Parse a single entity from a JSON reader
pub fn single_from_json_file(&self, r: impl Read) -> Result<Entity, EntitiesError> {
let v = serde_json::from_reader(r).map_err(JsonDeserializationError::from)?;
self.single_from_json_value(v)
let ejson = serde_json::from_reader(r).map_err(JsonDeserializationError::from)?;
self.single_from_ejson(ejson)
}

fn single_from_ejson(&self, ejson: EntityJson) -> Result<Entity, EntitiesError> {
let entity = self.parse_ejson(ejson)?;
match self.schema {
None => Ok(entity),
Some(schema) => {
let checker = EntitySchemaConformanceChecker::new(schema, self.extensions);
checker.validate_entity(&entity)?;
Ok(entity)
}
}
}

/// Internal function that creates an [`Entities`] from a stream of [`EntityJson`].
Expand Down
32 changes: 30 additions & 2 deletions cedar-policy/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ impl Entity {
}

/// 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)
Copy link
Contributor

@john-h-kastner-aws john-h-kastner-aws May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just the auto doc link?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you auto-doc link to a specific method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Entities::from_json_str] i think

pub fn from_json_value(
value: serde_json::Value,
schema: Option<&Schema>,
Expand All @@ -277,6 +279,8 @@ impl Entity {
}

/// 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>,
Expand All @@ -291,6 +295,8 @@ impl Entity {
}

/// 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(
Expand All @@ -306,11 +312,33 @@ impl Entity {
/// 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 an entities JSON file, use
/// [`Entity::from_json_file`].
/// 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)
}

/// 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 {
Expand Down