Add support for .meta.json files affecting .csv LocalizationTables

This commit is contained in:
Lucien Greathouse
2019-10-08 16:48:49 -07:00
parent 479476561e
commit 2393a1a114
3 changed files with 59 additions and 5 deletions

View File

@@ -5,11 +5,14 @@ use rbx_dom_weak::{RbxId, RbxTree, RbxValue};
use serde::Serialize; use serde::Serialize;
use crate::{ use crate::{
imfs::{Imfs, ImfsEntry, ImfsFetcher}, imfs::{FsResultExt, Imfs, ImfsEntry, ImfsFetcher},
snapshot::{InstanceMetadata, InstanceSnapshot}, snapshot::{InstanceMetadata, InstanceSnapshot},
}; };
use super::middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}; use super::{
meta_file::AdjacentMetadata,
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
};
pub struct SnapshotCsv; pub struct SnapshotCsv;
@@ -35,13 +38,17 @@ impl SnapshotMiddleware for SnapshotCsv {
.to_string_lossy() .to_string_lossy()
.to_string(); .to_string();
let meta_path = entry
.path()
.with_file_name(format!("{}.meta.json", instance_name));
let table_contents = convert_localization_csv(entry.contents(imfs)?); let table_contents = convert_localization_csv(entry.contents(imfs)?);
Ok(Some(InstanceSnapshot { let mut snapshot = InstanceSnapshot {
snapshot_id: None, snapshot_id: None,
metadata: InstanceMetadata { metadata: InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()), instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf()], relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()],
..Default::default() ..Default::default()
}, },
name: Cow::Owned(instance_name), name: Cow::Owned(instance_name),
@@ -52,7 +59,15 @@ impl SnapshotMiddleware for SnapshotCsv {
}, },
}, },
children: Vec::new(), children: Vec::new(),
})) };
if let Some(meta_entry) = imfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(imfs)?;
let mut metadata = AdjacentMetadata::from_slice(meta_contents);
metadata.apply_all(&mut snapshot);
}
Ok(Some(snapshot))
} }
fn from_instance(_tree: &RbxTree, _id: RbxId) -> SnapshotFileResult { fn from_instance(_tree: &RbxTree, _id: RbxId) -> SnapshotFileResult {
@@ -159,4 +174,23 @@ Ack,Ack!,,An exclamation of despair,¡Ay!"#,
assert_yaml_snapshot!(instance_snapshot); assert_yaml_snapshot!(instance_snapshot);
} }
#[test]
fn csv_with_meta() {
let mut imfs = Imfs::new(NoopFetcher);
let file = ImfsSnapshot::file(
r#"
Key,Source,Context,Example,es
Ack,Ack!,,An exclamation of despair,¡Ay!"#,
);
let meta = ImfsSnapshot::file(r#"{ "ignoreUnknownInstances": true }"#);
imfs.debug_load_snapshot("/foo.csv", file);
imfs.debug_load_snapshot("/foo.meta.json", meta);
let entry = imfs.get("/foo.csv").unwrap();
let instance_snapshot = SnapshotCsv::from_imfs(&mut imfs, &entry).unwrap().unwrap();
assert_yaml_snapshot!(instance_snapshot);
}
} }

View File

@@ -9,6 +9,7 @@ metadata:
Path: /foo.csv Path: /foo.csv
relevant_paths: relevant_paths:
- /foo.csv - /foo.csv
- /foo.meta.json
name: foo name: foo
class_name: LocalizationTable class_name: LocalizationTable
properties: properties:

View File

@@ -0,0 +1,19 @@
---
source: src/snapshot_middleware/csv.rs
expression: instance_snapshot
---
snapshot_id: ~
metadata:
ignore_unknown_instances: true
instigating_source:
Path: /foo.csv
relevant_paths:
- /foo.csv
- /foo.meta.json
name: foo
class_name: LocalizationTable
properties:
Contents:
Type: String
Value: "[{\"key\":\"Ack\",\"example\":\"An exclamation of despair\",\"source\":\"Ack!\",\"values\":{\"es\":\"¡Ay!\"}}]"
children: []