fix: fill() with owned values again

This commit is contained in:
əlemi 2024-10-25 17:32:13 +02:00
parent 94fcb8efdc
commit 9c26b68806

View file

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
use base64::{prelude::BASE64_STANDARD, Engine}; use base64::{prelude::BASE64_STANDARD, Engine};
@ -8,7 +9,7 @@ use jaq_interpret::FilterT;
use crate::errors::InvalidHeaderError; use crate::errors::InvalidHeaderError;
use crate::{PostWomanError, APP_USER_AGENT}; use crate::{PostWomanError, APP_USER_AGENT};
use crate::ext::{stringify_json, var_matcher, FillError, FillableFromEnvironment, StringOr}; use crate::ext::{stringify_json, FillError, FillableFromEnvironment, StringOr};
use super::{ExtractorConfig, ClientConfig}; use super::{ExtractorConfig, ClientConfig};
@ -149,50 +150,52 @@ impl FillableFromEnvironment for EndpointConfig {
fn fill(mut self, env: &toml::Table) -> Result<Self, FillError> { fn fill(mut self, env: &toml::Table) -> Result<Self, FillError> {
let vars = Self::default_vars(env); let vars = Self::default_vars(env);
self.path = Self::replace(self.path, env)?; self.path = Self::replace(self.path, &vars)?;
if let Some(method) = self.method { if let Some(method) = self.method {
self.method = Some(Self::replace(method, env)?); self.method = Some(Self::replace(method, &vars)?);
} }
if let Some(b) = self.body { if let Some(b) = self.body {
match b { match b {
StringOr::Str(body) => { StringOr::Str(body) => {
self.body = Some(StringOr::Str(Self::replace(body, env)?)); self.body = Some(StringOr::Str(Self::replace(body, &vars)?));
}, },
StringOr::T(json) => { StringOr::T(json) => {
let wrap = toml::Value::Table(json.clone()); let wrap = toml::Value::Table(json);
let toml::Value::Table(out) = replace_recursive(wrap, env)? let toml::Value::Table(out) = replace_recursive(wrap, &vars)?
else { unreachable!("we put in a table, we get out a table") }; else { unreachable!("we put in a table, we get out a table") };
self.body = Some(StringOr::T(out)); self.body = Some(StringOr::T(out));
}, },
} }
} }
if let Some(query) = self.query { if let Some(query) = self.query {
let mut out = Vec::new();
for q in query { for q in query {
q = Self::replace(q, env)?; out.push(Self::replace(q, &vars)?);
} }
self.query = Some(query); self.query = Some(out);
} }
if let Some(headers) = self.headers { if let Some(headers) = self.headers {
let mut out = Vec::new();
for h in headers { for h in headers {
h = Self::replace(h, env)?; out.push(Self::replace(h.clone(), &vars)?);
} }
self.headers = Some(headers); self.headers = Some(out);
} }
Ok(self) Ok(self)
} }
} }
fn replace_recursive(element: toml::Value, env: &toml::Table) -> Result<toml::Value, FillError> { fn replace_recursive(element: toml::Value, env: &HashMap<String, String>) -> Result<toml::Value, FillError> {
Ok(match element { Ok(match element {
toml::Value::Float(x) => toml::Value::Float(x), toml::Value::Float(x) => toml::Value::Float(x),
toml::Value::Integer(x) => toml::Value::Integer(x), toml::Value::Integer(x) => toml::Value::Integer(x),
toml::Value::Boolean(x) => toml::Value::Boolean(x), toml::Value::Boolean(x) => toml::Value::Boolean(x),
toml::Value::Datetime(x) => toml::Value::Datetime(x), toml::Value::Datetime(x) => toml::Value::Datetime(x),
toml::Value::String(x) => toml::Value::String(EndpointConfig::replace(x, env)?), toml::Value::String(x) => toml::Value::String(EndpointConfig::replace(x, env)?),
toml::Value::Array(arr) => { toml::Value::Array(mut arr) => {
for v in arr.iter_mut() { for v in arr.iter_mut() {
*v = replace_recursive(v, env)?; *v = replace_recursive(v.clone(), env)?;
} }
toml::Value::Array(arr) toml::Value::Array(arr)
}, },