-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add v1-v2 release conversion #29
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use super::*; | ||
use std::path::PathBuf; | ||
use std::{fs::File, path::PathBuf}; | ||
|
||
#[test] | ||
fn test_v1_v2_common() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use super::Release; | ||
use crate::meta::v1 as dist; | ||
use serde_json::{json, Value}; | ||
use std::error::Error; | ||
|
||
/// to_v2 parses v1, which contains PGXN v1 release metadata, into a JSON | ||
/// object containing valid PGXN v2 release metadata. | ||
pub fn to_v2(v1: &Value) -> Result<Value, Box<dyn Error>> { | ||
let mut v2_val = dist::to_v2(v1)?; | ||
let v2 = v2_val | ||
.as_object_mut() | ||
.ok_or("Date returned from v1::to_v2 is not an object")?; | ||
|
||
// Convert release. | ||
v2.insert("release".to_string(), v1_to_v2_release(v1)?); | ||
|
||
Ok(v2_val) | ||
} | ||
|
||
/// from_value parses v1, which contains PGXN v1 metadata, into a | ||
/// [`Release`] object containing valid PGXN v2 metadata. | ||
pub fn from_value(v1: Value) -> Result<Release, Box<dyn Error>> { | ||
to_v2(&v1)?.try_into() | ||
} | ||
|
||
/// v1_to_v2_release clones release metadata from v1 to the v2 format. The | ||
/// included signature information will be random and un-verifiable, but | ||
/// adequate for v2 JSON Schema validation. | ||
fn v1_to_v2_release(v1: &Value) -> Result<Value, Box<dyn Error>> { | ||
use rand::distributions::{Alphanumeric, DistString}; | ||
let mut field = "user"; | ||
if let Some(Value::String(user)) = v1.get(field) { | ||
field = "date"; | ||
if let Some(Value::String(date)) = v1.get(field) { | ||
field = "sha1"; | ||
if let Some(Value::String(sha1)) = v1.get(field) { | ||
field = "name"; | ||
if let Some(Value::String(name)) = v1.get(field) { | ||
field = "version"; | ||
if let Some(Value::String(version)) = v1.get(field) { | ||
let uri = | ||
format!("dist/{1}/{0}/{1}-{0}.zip", version.as_str(), name.as_str()); | ||
// Generate random base64-ish values to mock headers | ||
// and signatures. | ||
let mut rng = rand::thread_rng(); | ||
return Ok(json!({ | ||
"headers": [format!("eyJ{}", Alphanumeric.sample_string(&mut rng, 13))], | ||
"signatures": [Alphanumeric.sample_string(&mut rng, 32)], | ||
"payload": { | ||
"user": user, | ||
"date": date, | ||
"uri": uri, | ||
"digests": {"sha1": sha1}, | ||
} | ||
})); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Err(Box::from(format!("missing release property {:?}", field))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this I believe you could skip the
.as_slice()
belowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly that doesn't work because the third item in the list has two items in the vector, not one, so it complains with