From 70bc687e3fec738dbbfb632cb2be22cd5802b891 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 14 Jan 2019 19:03:57 -0800 Subject: add interstitial JSON serde model to citation internment --- rust/src/database_models.rs | 92 ++++++++++++++++++++++++++++++++++++++++----- rust/src/entity_crud.rs | 9 +++-- 2 files changed, 87 insertions(+), 14 deletions(-) (limited to 'rust/src') diff --git a/rust/src/database_models.rs b/rust/src/database_models.rs index 47fd062d..b76b469a 100644 --- a/rust/src/database_models.rs +++ b/rust/src/database_models.rs @@ -4,7 +4,7 @@ use crate::database_schema::*; use crate::errors::*; use crate::identifiers::uuid2fcid; use chrono; -use fatcat_api_spec::models::{ChangelogEntry, Editgroup, EditgroupAnnotation, Editor, EntityEdit}; +use fatcat_api_spec::models::{ChangelogEntry, Editgroup, EditgroupAnnotation, Editor, EntityEdit, ReleaseRef}; use serde_json; use uuid::Uuid; @@ -501,15 +501,6 @@ pub struct ReleaseRefRow { pub target_release_ident_id: Uuid, } -/* These fields now interned in JSON blob - pub key: Option, - pub extra_json: Option, - pub container_name: Option, - pub year: Option, - pub title: Option, - pub locator: Option, -*/ - #[derive(Debug, Queryable, Insertable, Associations, AsChangeset)] #[table_name = "refs_blob"] pub struct RefsBlobRow { @@ -517,6 +508,87 @@ pub struct RefsBlobRow { pub refs_json: serde_json::Value, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +/// This model is a stable representation of what goes in a RefsBlobRow `refs_json` field (an array +/// of this model). We could rely on the `ReleaseRef` API spec model directly, but that would lock +/// the database contents to the API spec rigidly; by defining this struct independently, we can +/// migrate the schemas. To start, this is a direct copy of the `ReleaseRef` model. +pub struct RefsBlobJson { + #[serde(rename = "index")] + #[serde(skip_serializing_if = "Option::is_none")] + pub index: Option, + + /// base32-encoded unique identifier + #[serde(rename = "target_release_id")] + #[serde(skip_serializing_if = "Option::is_none")] + pub target_release_id: Option, + + #[serde(rename = "extra")] + #[serde(skip_serializing_if = "Option::is_none")] + pub extra: Option, + + #[serde(rename = "key")] + #[serde(skip_serializing_if = "Option::is_none")] + pub key: Option, + + #[serde(rename = "year")] + #[serde(skip_serializing_if = "Option::is_none")] + pub year: Option, + + #[serde(rename = "container_name")] + #[serde(skip_serializing_if = "Option::is_none")] + pub container_name: Option, + + #[serde(rename = "title")] + #[serde(skip_serializing_if = "Option::is_none")] + pub title: Option, + + #[serde(rename = "locator")] + #[serde(skip_serializing_if = "Option::is_none")] + pub locator: Option, +} + +impl RefsBlobJson { + pub fn into_model(self) -> ReleaseRef { + ReleaseRef { + index: self.index, + target_release_id: self.target_release_id, + extra: self.extra, + key: self.key, + year: self.year, + container_name: self.container_name, + title: self.title, + locator: self.locator, + } + } + + pub fn to_model(&self) -> ReleaseRef { + ReleaseRef { + index: self.index, + target_release_id: self.target_release_id.clone(), + extra: self.extra.clone(), + key: self.key.clone(), + year: self.year, + container_name: self.container_name.clone(), + title: self.title.clone(), + locator: self.locator.clone(), + } + } + + pub fn from_model(model: &ReleaseRef) -> RefsBlobJson { + RefsBlobJson { + index: model.index, + target_release_id: model.target_release_id.clone(), + extra: model.extra.clone(), + key: model.key.clone(), + year: model.year, + container_name: model.container_name.clone(), + title: model.title.clone(), + locator: model.locator.clone(), + } + } +} + #[derive(Debug, Queryable, Insertable, Associations, AsChangeset)] #[table_name = "file_rev_release"] pub struct FileRevReleaseRow { diff --git a/rust/src/entity_crud.rs b/rust/src/entity_crud.rs index 1147e117..09ce9542 100644 --- a/rust/src/entity_crud.rs +++ b/rust/src/entity_crud.rs @@ -1819,7 +1819,8 @@ impl EntityCrud for ReleaseEntity { let refs_blob: RefsBlobRow = refs_blob::table .find(sha1) // checked in match .get_result(conn)?; - let mut refs: Vec = serde_json::from_value(refs_blob.refs_json)?; + let refs: Vec = serde_json::from_value(refs_blob.refs_json)?; + let mut refs: Vec = refs.into_iter().map(|j| j.into_model()).collect(); let ref_rows: Vec = release_ref::table .filter(release_ref::release_rev.eq(rev_row.id)) .order(release_ref::index_val.asc()) @@ -1967,10 +1968,10 @@ impl EntityCrud for ReleaseEntity { } // Have to strip out target refs and indexes, or hashing won't work well when // these change - let ref_list: Vec = ref_list + let ref_list: Vec = ref_list .iter() - .map(|r| { - let mut r = r.clone(); + .map(|r: &ReleaseRef| { + let mut r = RefsBlobJson::from_model(r); r.target_release_id = None; r.index = None; r -- cgit v1.2.3