Skip to content

Commit

Permalink
Merge pull request #11 from skshetry/try-fixing-tree-load
Browse files Browse the repository at this point in the history
  • Loading branch information
skshetry authored Aug 8, 2024
2 parents 4be7028 + 24b49f4 commit 0d2934b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/objects.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::hash::md5;
use crate::json_format;
use itertools::Itertools;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize, Serializer};
use std::convert::From;
use std::error::Error;
use std::fs::File;
Expand All @@ -18,7 +18,6 @@ pub type HashFile = String;

#[derive(Deserialize, Clone, PartialEq, Debug, PartialOrd, Ord, Eq)]
pub struct TreeEntry {
#[serde(deserialize_with = "posixstr_to_ospath")]
pub relpath: PathBuf,
#[serde(rename = "md5")]
pub oid: String,
Expand Down Expand Up @@ -64,14 +63,6 @@ where
s.serialize_str(&x.iter().map(|p| p.to_str().unwrap()).join("/"))
}

fn posixstr_to_ospath<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
Ok(s.split('/').collect())
}

impl Tree {
pub fn serialize(&self) -> serde_json::Result<String> {
// make it compatible with `json.dumps()` separator
Expand Down
33 changes: 33 additions & 0 deletions tests/test_load.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use dvc_data::objects::TreeEntry;
use dvc_data::Tree;
use std::fs;
use std::path::PathBuf;
use tempfile::tempdir;

mod utils;

use utils::write_to_temp_file;
#[test]
pub fn test_tree_load() {
let dir = t!(tempdir());
t!(fs::create_dir(dir.path().join("test")));
let test_dir = dir.path().join("test");

let contents = r#"[{"md5": "e5a81dd70644b5534aae9f7c32055ec3", "relpath": "data/bar"}, {"md5": "eceec35e3f3dd774244de59b1094cc59", "relpath": "data/foo/baz"}]"#;
write_to_temp_file(&test_dir, "tree", contents);

let t = Tree::load_from(&test_dir.join("tree")).unwrap();
assert_eq!(
t.entries,
vec![
TreeEntry {
relpath: PathBuf::from("data/bar"),
oid: "e5a81dd70644b5534aae9f7c32055ec3".to_string()
},
TreeEntry {
relpath: PathBuf::from("data/foo/baz"),
oid: "eceec35e3f3dd774244de59b1094cc59".to_string()
},
]
);
}

0 comments on commit 0d2934b

Please sign in to comment.