Skip to content

Commit

Permalink
fix: add target simple or id mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Sep 30, 2024
1 parent 7236bf6 commit 5a8ffd4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
37 changes: 33 additions & 4 deletions edc-connector-client/src/types/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub struct Policy {
#[serde(alias = "odrl:assigner")]
assigner: Option<String>,
#[serde(alias = "odrl:target")]
target: Option<String>,
target: Option<Target>,
#[serde_as(deserialize_as = "OneOrMany<_, PreferMany>")]
#[serde(rename = "permission", alias = "odrl:permission", default)]
permissions: Vec<Permission>,
Expand Down Expand Up @@ -196,7 +196,7 @@ impl Policy {
self.assigner.as_ref()
}

pub fn target(&self) -> Option<&String> {
pub fn target(&self) -> Option<&Target> {
self.target.as_ref()
}

Expand Down Expand Up @@ -226,8 +226,8 @@ impl PolicyBuilder {
self
}

pub fn target(mut self, target: &str) -> Self {
self.0.target = Some(target.to_string());
pub fn target(mut self, target: Target) -> Self {
self.0.target = Some(target);
self
}

Expand Down Expand Up @@ -421,6 +421,35 @@ pub enum Action {
},
}

#[derive(Debug, Serialize, PartialEq, Clone, Deserialize)]
#[serde(untagged)]
pub enum Target {
Simple(String),
Id {
#[serde(rename = "@id")]
id: String,
},
}

impl Target {
pub fn simple(target: &str) -> Target {
Target::Simple(target.to_string())
}

pub fn id(target: &str) -> Target {
Target::Id {
id: target.to_string(),
}
}

pub fn get_id(&self) -> &str {
match self {
Target::Simple(target) => target,
Target::Id { id } => id,
}
}
}

impl Default for Action {
fn default() -> Self {
Action::new("http://www.w3.org/ns/odrl/2/use".to_string())
Expand Down
4 changes: 2 additions & 2 deletions edc-connector-client/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use edc_connector_client::{
contract_definition::NewContractDefinition,
contract_negotiation::{ContractNegotiationState, ContractRequest},
data_address::DataAddress,
policy::{NewPolicyDefinition, Policy, PolicyKind},
policy::{NewPolicyDefinition, Policy, PolicyKind, Target},
query::Criterion,
transfer_process::{TransferProcessState, TransferRequest},
},
Expand Down Expand Up @@ -112,7 +112,7 @@ pub async fn seed_contract_negotiation(
.id(&offer_id)
.kind(PolicyKind::Offer)
.assigner(PROVIDER_ID)
.target(&asset_id)
.target(Target::id(&asset_id))
.build(),
)
.build()
Expand Down
6 changes: 3 additions & 3 deletions edc-connector-client/tests/contract-negotiation-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod initiate {
types::{
catalog::DatasetRequest,
contract_negotiation::ContractRequest,
policy::{Policy, PolicyKind},
policy::{Policy, PolicyKind, Target},
},
Error, ManagementApiError, ManagementApiErrorDetailKind,
};
Expand Down Expand Up @@ -44,7 +44,7 @@ mod initiate {
.kind(PolicyKind::Offer)
.id(&offer_id)
.assigner(PROVIDER_ID)
.target(&asset_id)
.target(Target::id(&asset_id))
.build(),
)
.build()
Expand Down Expand Up @@ -87,7 +87,7 @@ mod initiate {
Policy::builder()
.id(&offer_id)
.assigner(PROVIDER_ID)
.target(&asset_id)
.target(Target::id(&asset_id))
.build(),
)
.build()
Expand Down

0 comments on commit 5a8ffd4

Please sign in to comment.