Skip to content

Commit 3960ade

Browse files
authored
fix: assertion metadata localizations support was missing. CAI-9444 (#1390)
* fix: assertion metadata localizations support was missing. CAI-9444
1 parent 990788a commit 3960ade

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

sdk/src/assertions/assertion_metadata.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// specific language governing permissions and limitations under
1212
// each license.
1313

14+
use std::collections::HashMap;
15+
1416
use chrono::{SecondsFormat, Utc};
1517
#[cfg(feature = "json_schema")]
1618
use schemars::JsonSchema;
@@ -38,6 +40,8 @@ pub struct AssertionMetadata {
3840
reference: Option<HashedUri>,
3941
#[serde(rename = "dataSource", skip_serializing_if = "Option::is_none")]
4042
data_source: Option<DataSource>,
43+
#[serde(skip_serializing_if = "Option::is_none")]
44+
localizations: Option<Vec<HashMap<String, HashMap<String, String>>>>, // not implemented
4145
#[serde(rename = "regionOfInterest", skip_serializing_if = "Option::is_none")]
4246
region_of_interest: Option<RegionOfInterest>,
4347
}
@@ -56,6 +60,7 @@ impl AssertionMetadata {
5660
)),
5761
reference: None,
5862
data_source: None,
63+
localizations: None,
5964
region_of_interest: None,
6065
}
6166
}
@@ -70,6 +75,11 @@ impl AssertionMetadata {
7075
self.date_time.as_deref()
7176
}
7277

78+
/// Returns the vec of localizations maps if they exist.
79+
pub fn localizations(&self) -> Option<&Vec<HashMap<String, HashMap<String, String>>>> {
80+
self.localizations.as_ref()
81+
}
82+
7383
/// Returns the [`DataSource`] for this assertion if it exists.
7484
pub fn data_source(&self) -> Option<&DataSource> {
7585
self.data_source.as_ref()
@@ -121,6 +131,15 @@ impl AssertionMetadata {
121131
self.region_of_interest = Some(region_of_interest);
122132
self
123133
}
134+
135+
/// Sets all localizations, replacing any existing ones
136+
pub fn set_localizations(
137+
mut self,
138+
localizations: Vec<HashMap<String, HashMap<String, String>>>,
139+
) -> Self {
140+
self.localizations = Some(localizations);
141+
self
142+
}
124143
}
125144

126145
impl Default for AssertionMetadata {
@@ -312,8 +331,15 @@ pub mod tests {
312331
#[test]
313332
fn assertion_metadata() {
314333
let review = ReviewRating::new("foo", Some("bar".to_owned()), 3);
334+
let mut translations = HashMap::new();
335+
translations.insert("en-US".to_owned(), "Kevin's Five Cats".to_owned());
336+
translations.insert("es-MX".to_owned(), "Los Cinco Gatos de Kevin".to_owned());
337+
let mut localizations = HashMap::new();
338+
localizations.insert("dc:title".to_owned(), translations);
339+
315340
let original = AssertionMetadata::new()
316341
.add_review(review)
342+
.set_localizations(vec![localizations])
317343
.set_region_of_interest(RegionOfInterest {
318344
region: vec![Range {
319345
range_type: RangeType::Temporal,
@@ -326,6 +352,7 @@ pub mod tests {
326352
}],
327353
..Default::default()
328354
});
355+
329356
println!("{:}", &original);
330357
let assertion = original.to_assertion().expect("build_assertion");
331358
assert_eq!(assertion.mime_type(), "application/cbor");
@@ -334,10 +361,25 @@ pub mod tests {
334361
println!("{:?}", serde_json::to_string(&result));
335362
assert_eq!(original.date_time, result.date_time);
336363
assert_eq!(original.reviews, result.reviews);
364+
let localizations = result.localizations.as_ref().unwrap();
365+
assert_eq!(
366+
localizations[0]
367+
.get("dc:title")
368+
.unwrap()
369+
.get("en-US")
370+
.unwrap(),
371+
"Kevin's Five Cats"
372+
);
337373
assert_eq!(
338374
original.region_of_interest.as_ref(),
339375
result.region_of_interest()
340-
)
341-
//assert_eq!(original.reviews.unwrap().len(), 1);
376+
);
377+
378+
// Test round-trip serialization
379+
let assertion = original.to_assertion().expect("build_assertion");
380+
let result = AssertionMetadata::from_assertion(&assertion).expect("extract_assertion");
381+
382+
assert_eq!(original.localizations, result.localizations);
383+
assert_eq!(original.reviews.unwrap().len(), 1);
342384
}
343385
}

0 commit comments

Comments
 (0)