From 4eab3ba1a7386d7ee02f8634c96c1ad18d298870 Mon Sep 17 00:00:00 2001 From: Lann Martin Date: Mon, 26 Aug 2024 16:48:55 -0400 Subject: [PATCH] factors: Fix toml key tracking Signed-off-by: Lann Martin --- .../src/runtime_config/spin.rs | 3 ++- crates/factor-llm/src/spin.rs | 3 ++- .../src/runtime_config/spin.rs | 4 ++-- .../factor-sqlite/src/runtime_config/spin.rs | 4 ++-- crates/factor-variables/src/spin_cli/mod.rs | 4 ++-- crates/runtime-config/src/lib.rs | 18 +++++------------- 6 files changed, 15 insertions(+), 21 deletions(-) diff --git a/crates/factor-key-value/src/runtime_config/spin.rs b/crates/factor-key-value/src/runtime_config/spin.rs index e604d3a5fc..64c4e1d57f 100644 --- a/crates/factor-key-value/src/runtime_config/spin.rs +++ b/crates/factor-key-value/src/runtime_config/spin.rs @@ -4,6 +4,7 @@ use crate::{DefaultLabelResolver, RuntimeConfig}; use anyhow::Context as _; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; +use spin_factors::runtime_config::toml::GetTomlValue; use spin_key_value::StoreManager; use std::{collections::HashMap, sync::Arc}; @@ -100,7 +101,7 @@ impl RuntimeConfigResolver { /// Resolves a toml table into a runtime config. pub fn resolve_from_toml( &self, - table: Option<&toml::Table>, + table: Option<&impl GetTomlValue>, ) -> anyhow::Result> { let Some(table) = table.and_then(|t| t.get("key_value_store")) else { return Ok(None); diff --git a/crates/factor-llm/src/spin.rs b/crates/factor-llm/src/spin.rs index b9b91c6991..5f04478aa3 100644 --- a/crates/factor-llm/src/spin.rs +++ b/crates/factor-llm/src/spin.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use std::sync::Arc; +use spin_factors::runtime_config::toml::GetTomlValue; use spin_llm_remote_http::RemoteHttpLlmEngine; use spin_world::async_trait; use spin_world::v1::llm::{self as v1}; @@ -80,7 +81,7 @@ impl LlmEngine for RemoteHttpLlmEngine { } pub fn runtime_config_from_toml( - table: &toml::Table, + table: &impl GetTomlValue, state_dir: Option, use_gpu: bool, ) -> anyhow::Result> { diff --git a/crates/factor-outbound-networking/src/runtime_config/spin.rs b/crates/factor-outbound-networking/src/runtime_config/spin.rs index c8e2a3fc57..863ebed7fe 100644 --- a/crates/factor-outbound-networking/src/runtime_config/spin.rs +++ b/crates/factor-outbound-networking/src/runtime_config/spin.rs @@ -37,9 +37,9 @@ impl SpinTlsRuntimeConfig { /// client_cert_file = "path/to/client.crt" /// client_private_key_file = "path/to/client.key" /// ``` - pub fn config_from_table( + pub fn config_from_table( &self, - table: &T, + table: &impl GetTomlValue, ) -> anyhow::Result> { let Some(tls_configs) = self.tls_configs_from_table(table)? else { return Ok(None); diff --git a/crates/factor-sqlite/src/runtime_config/spin.rs b/crates/factor-sqlite/src/runtime_config/spin.rs index b12a876166..b51fd4d50c 100644 --- a/crates/factor-sqlite/src/runtime_config/spin.rs +++ b/crates/factor-sqlite/src/runtime_config/spin.rs @@ -49,9 +49,9 @@ impl RuntimeConfigResolver { /// type = "$database-type" /// ... extra type specific configuration ... /// ``` - pub fn resolve_from_toml( + pub fn resolve_from_toml( &self, - table: &T, + table: &impl GetTomlValue, ) -> anyhow::Result> { let Some(table) = table.get("sqlite_database") else { return Ok(None); diff --git a/crates/factor-variables/src/spin_cli/mod.rs b/crates/factor-variables/src/spin_cli/mod.rs index f86c52d4a2..f5d8097781 100644 --- a/crates/factor-variables/src/spin_cli/mod.rs +++ b/crates/factor-variables/src/spin_cli/mod.rs @@ -12,12 +12,12 @@ pub use vault::*; use serde::Deserialize; use spin_expressions::Provider; -use spin_factors::anyhow; +use spin_factors::{anyhow, runtime_config::toml::GetTomlValue}; use crate::runtime_config::RuntimeConfig; /// Resolves a runtime configuration for the variables factor from a TOML table. -pub fn runtime_config_from_toml(table: &toml::Table) -> anyhow::Result { +pub fn runtime_config_from_toml(table: &impl GetTomlValue) -> anyhow::Result { // Always include the environment variable provider. let mut providers = vec![Box::::default() as _]; let Some(array) = table.get("variable_provider") else { diff --git a/crates/runtime-config/src/lib.rs b/crates/runtime-config/src/lib.rs index bd8d7745fd..85351b9cc4 100644 --- a/crates/runtime-config/src/lib.rs +++ b/crates/runtime-config/src/lib.rs @@ -189,12 +189,6 @@ impl<'a> TomlResolver<'a> { } } -impl AsRef for TomlResolver<'_> { - fn as_ref(&self) -> &toml::Table { - self.table.as_ref() - } -} - /// The TOML based runtime configuration source Spin CLI. pub struct TomlRuntimeConfigSource<'a, 'b> { toml: TomlResolver<'b>, @@ -226,7 +220,7 @@ impl FactorRuntimeConfigSource for TomlRuntimeConfigSource<'_, ' fn get_runtime_config( &mut self, ) -> anyhow::Result> { - self.key_value.resolve_from_toml(Some(self.toml.as_ref())) + self.key_value.resolve_from_toml(Some(&self.toml.table)) } } @@ -238,7 +232,7 @@ impl FactorRuntimeConfigSource for TomlRuntimeConfigSo let Some(tls) = self.tls else { return Ok(None); }; - tls.config_from_table(self.toml.as_ref()) + tls.config_from_table(&self.toml.table) } } @@ -246,9 +240,7 @@ impl FactorRuntimeConfigSource for TomlRuntimeConfigSource<'_, fn get_runtime_config( &mut self, ) -> anyhow::Result::RuntimeConfig>> { - Ok(Some(variables::runtime_config_from_toml( - self.toml.as_ref(), - )?)) + Ok(Some(variables::runtime_config_from_toml(&self.toml.table)?)) } } @@ -266,7 +258,7 @@ impl FactorRuntimeConfigSource for TomlRuntimeConfigSource< impl FactorRuntimeConfigSource for TomlRuntimeConfigSource<'_, '_> { fn get_runtime_config(&mut self) -> anyhow::Result> { - llm::runtime_config_from_toml(self.toml.as_ref(), self.toml.state_dir()?, self.use_gpu) + llm::runtime_config_from_toml(&self.toml.table, self.toml.state_dir()?, self.use_gpu) } } @@ -296,7 +288,7 @@ impl FactorRuntimeConfigSource for TomlRuntimeConfigSource<' impl FactorRuntimeConfigSource for TomlRuntimeConfigSource<'_, '_> { fn get_runtime_config(&mut self) -> anyhow::Result> { - self.sqlite.resolve_from_toml(self.toml.as_ref()) + self.sqlite.resolve_from_toml(&self.toml.table) } }