Skip to content

Commit

Permalink
fix(pact_models): Provider state values may be passed in under a "pro…
Browse files Browse the repository at this point in the history
…viderState" key
  • Loading branch information
rholshausen committed Aug 8, 2024
1 parent 80b7bb5 commit 50fc022
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions rust/pact_models/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rand::prelude::*;
#[cfg(target_family = "wasm")] use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use serde_json::Value::Object;
use tracing::{debug, instrument, trace, warn};
use uuid::Uuid;

Expand Down Expand Up @@ -825,11 +826,21 @@ impl GenerateValue<u16> for Generator {
) -> anyhow::Result<u16> {
match self {
&Generator::RandomInt(min, max) => Ok(rand::thread_rng().gen_range(min as u16..(max as u16).saturating_add(1))),
&Generator::ProviderStateGenerator(ref exp, ref dt) =>
match generate_value_from_context(exp, context, dt) {
&Generator::ProviderStateGenerator(ref exp, ref dt) => {
// Provider state values may come under a "providerState" key
let provider_state_config = if let Some(Object(psc)) = context.get("providerState") {
psc
.iter()
.map(|(k, v)| (k.as_str(), v.clone()))
.collect()
} else {
context.clone()
};
match generate_value_from_context(exp, &provider_state_config, dt) {
Ok(val) => u16::try_from(val),
Err(err) => Err(err)
},
}
}
_ => Err(anyhow!("Could not generate a u16 value from {} using {:?}", value, self))
}
}
Expand Down Expand Up @@ -973,8 +984,18 @@ impl GenerateValue<String> for Generator {
}
}
Generator::RandomBoolean => Ok(format!("{}", rnd.gen::<bool>())),
Generator::ProviderStateGenerator(ref exp, ref dt) =>
generate_value_from_context(exp, context, dt).map(|val| val.to_string()),
Generator::ProviderStateGenerator(ref exp, ref dt) => {
// Provider state values may come under a "providerState" key
let provider_state_config = if let Some(Object(psc)) = context.get("providerState") {
psc
.iter()
.map(|(k, v)| (k.as_str(), v.clone()))
.collect()
} else {
context.clone()
};
generate_value_from_context(exp, &provider_state_config, dt).map(|val| val.to_string())
}
Generator::MockServerURL(example, regex) => if let Some(mock_server_details) = context.get("mockServer") {
debug!("Generating URL from Mock Server details");
match mock_server_details.as_object() {
Expand Down Expand Up @@ -1140,11 +1161,21 @@ impl GenerateValue<Value> for Generator {
}
},
Generator::RandomBoolean => Ok(json!(rand::thread_rng().gen::<bool>())),
Generator::ProviderStateGenerator(ref exp, ref dt) =>
match generate_value_from_context(exp, context, dt) {
Generator::ProviderStateGenerator(ref exp, ref dt) => {
// Provider state values may come under a "providerState" key
let provider_state_config = if let Some(Object(psc)) = context.get("providerState") {
psc
.iter()
.map(|(k, v)| (k.as_str(), v.clone()))
.collect()
} else {
context.clone()
};
match generate_value_from_context(exp, &provider_state_config, dt) {
Ok(val) => val.as_json(),
Err(err) => Err(err)
},
}
}
Generator::MockServerURL(example, regex) => {
debug!("context = {:?}", context);
if let Some(mock_server_details) = context.get("mockServer") {
Expand Down

0 comments on commit 50fc022

Please sign in to comment.