Skip to content

Commit

Permalink
Address code review
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee committed Aug 16, 2023
1 parent 5995922 commit ef7de69
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
14 changes: 0 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions did_doc_sov/src/extra_fields/aip1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(deny_unknown_fields)]
pub struct ExtraFieldsAIP1 {}
1 change: 1 addition & 0 deletions did_doc_sov/src/extra_fields/didcommv1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{AcceptType, KeyKind};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct ExtraFieldsDidCommV1 {
priority: u32,
recipient_keys: Vec<KeyKind>,
Expand Down
1 change: 1 addition & 0 deletions did_doc_sov/src/extra_fields/didcommv2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{AcceptType, KeyKind};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct ExtraFieldsDidCommV2 {
routing_keys: Vec<KeyKind>,
#[serde(default)]
Expand Down
7 changes: 6 additions & 1 deletion did_doc_sov/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ impl Serialize for DidDocumentSov {

for service_sov in &self.services {
let service: Service<ExtraFieldsSov> = service_sov.clone().try_into().map_err(serde::ser::Error::custom)?;
builder = builder.add_service(service);
// Not very efficient, but
// * we don't expect many services
// * does not require allowing to remove services from existing DDO or builder
if !self.did_doc.service().iter().any(|s| s.id() == service.id()) {
builder = builder.add_service(service);
}
}

builder.build().serialize(serializer)
Expand Down
10 changes: 9 additions & 1 deletion did_doc_sov/tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const DID_DOC_DATA: &'static str = r#"
"#;

#[test]
fn test_serde() {
fn test_deserialization() {
let did_doc = serde_json::from_str::<DidDocumentSov>(DID_DOC_DATA).unwrap();
assert_eq!(did_doc.id().to_string(), "did:sov:HR6vs6GEZ8rHaVgjg2WodM");
assert_eq!(did_doc.verification_method().len(), 2);
Expand Down Expand Up @@ -136,3 +136,11 @@ fn test_serde() {
panic!("Expected reference key kind");
}
}

#[test]
fn test_deserialization_and_serialization() {
let did_doc_1 = serde_json::from_str::<DidDocumentSov>(DID_DOC_DATA).unwrap();
let serialized = serde_json::to_string_pretty(&did_doc_1).unwrap();
let did_doc_2 = serde_json::from_str::<DidDocumentSov>(&serialized).unwrap();
assert_eq!(did_doc_1, did_doc_2);
}

0 comments on commit ef7de69

Please sign in to comment.