Skip to content

Commit

Permalink
feat: add serialization to JSON (ongoing) (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
subotic committed Dec 1, 2023
1 parent f78f94c commit fc9481f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
13 changes: 10 additions & 3 deletions dsp-meta/src/api/convert/axum/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use axum::response::{IntoResponse, Json, Response};
use dsp_domain::metadata::entity::project_metadata::ProjectMetadata;
use serde::Serialize;

use crate::api::model::project_metadata_dto::ProjectMetadataGraphResult;
use crate::api::model::project_metadata_dto::ProjectMetadataGraphDto;
use crate::error::DspMetaError;

#[derive(Debug, Default, Clone, PartialEq, Serialize)]
Expand All @@ -13,14 +13,21 @@ pub struct ProjectMetadataDto(pub Option<ProjectMetadata>);
impl IntoResponse for ProjectMetadataDto {
fn into_response(self) -> Response {
match self.0 {
Some(pm) => (StatusCode::OK, Json(serde_json::to_value(pm).unwrap())).into_response(),
Some(metadata) => (
StatusCode::OK,
Json(
serde_json::to_value(metadata)
.expect("project metadata should convert to JSON."),
),
)
.into_response(),
None => (StatusCode::NOT_FOUND).into_response(),
}
}
}

/// Convert `ProjectMetadataGraph` into a response.
impl IntoResponse for ProjectMetadataGraphResult {
impl IntoResponse for ProjectMetadataGraphDto {
fn into_response(self) -> Response {
match self.0 {
Some(metadata_graph) => {
Expand Down
6 changes: 3 additions & 3 deletions dsp-meta/src/api/convert/rdf/project_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ impl ProjectMetadataGraph {
}

/// A wrapper around an optional ProjectMetadata.
pub struct ProjectMetadataGraphDto(pub ProjectMetadata);
pub struct ProjectMetadataGraphWrapper(pub ProjectMetadata);

/// Convert a `ProjectMetadataGraphDto` into a `ProjectMetadataGraph`.
///
/// The underlying graph implementation is a `LightGraph` (in contrast to FastGraph) which is a
/// simple in-memory graph graph implementation with a low memory footprint, without indexing,
/// thus fast to build but slow to query. Since we are only interested in building the graph and
/// immediately serializing it, this is the better choice (supported by benchmarking results).
impl From<ProjectMetadataGraphDto> for ProjectMetadataGraph {
fn from(value: ProjectMetadataGraphDto) -> ProjectMetadataGraph {
impl From<ProjectMetadataGraphWrapper> for ProjectMetadataGraph {
fn from(value: ProjectMetadataGraphWrapper) -> ProjectMetadataGraph {
trace!("entered ProjectMetadataGraph::from()");
let _dsp = Namespace::new_unchecked(DSP_NAMESPACE_STRING);

Expand Down
10 changes: 5 additions & 5 deletions dsp-meta/src/api/handler/project_metadata_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use tracing::trace;

use crate::api::convert::axum::responses::ProjectMetadataDto;
use crate::api::convert::hcl::hcl_body::HclBody;
use crate::api::convert::rdf::project_metadata::ProjectMetadataGraphDto;
use crate::api::model::project_metadata_dto::ProjectMetadataGraphResult;
use crate::api::convert::rdf::project_metadata::ProjectMetadataGraphWrapper;
use crate::api::model::project_metadata_dto::ProjectMetadataGraphDto;
use crate::app_state::AppState;
use crate::domain::service::project_metadata_api_contract::ProjectMetadataApiContract;
use crate::error::DspMetaError;
Expand All @@ -36,13 +36,13 @@ pub async fn get_project_metadata_by_shortcode(
pub async fn get_project_metadata_by_shortcode_as_rdf(
Path(shortcode): Path<String>,
State(state): State<Arc<AppState>>,
) -> Result<ProjectMetadataGraphResult, DspMetaError> {
) -> Result<ProjectMetadataGraphDto, DspMetaError> {
trace!("entered get_project_metadata_by_shortcode_as_rdf()");
state
.project_metadata_service
.find_by_id(Shortcode(shortcode))
.map(|metadata| metadata.map(|m| ProjectMetadataGraphDto(m).into()))
.map(ProjectMetadataGraphResult)
.map(|metadata| metadata.map(|m| ProjectMetadataGraphWrapper(m).into()))
.map(ProjectMetadataGraphDto)
}

pub async fn get_all_project_metadata(State(state): State<Arc<AppState>>) -> Json<Value> {
Expand Down
2 changes: 1 addition & 1 deletion dsp-meta/src/api/model/project_metadata_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ use crate::api::convert::rdf::project_metadata::ProjectMetadataGraph;
#[derive(Debug, Default, Clone, PartialEq, Serialize)]
pub struct ProjectMetadataDto(pub Option<ProjectMetadata>);

pub struct ProjectMetadataGraphResult(pub Option<ProjectMetadataGraph>);
pub struct ProjectMetadataGraphDto(pub Option<ProjectMetadataGraph>);

0 comments on commit fc9481f

Please sign in to comment.