Skip to content

Commit

Permalink
Merge pull request #2765 from lann/fix-toml-key-tracking
Browse files Browse the repository at this point in the history
factors: Fix toml key tracking
  • Loading branch information
lann authored Aug 26, 2024
2 parents c58a6b8 + 4eab3ba commit c132a03
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 21 deletions.
3 changes: 2 additions & 1 deletion crates/factor-key-value/src/runtime_config/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Option<RuntimeConfig>> {
let Some(table) = table.and_then(|t| t.get("key_value_store")) else {
return Ok(None);
Expand Down
3 changes: 2 additions & 1 deletion crates/factor-llm/src/spin.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -80,7 +81,7 @@ impl LlmEngine for RemoteHttpLlmEngine {
}

pub fn runtime_config_from_toml(
table: &toml::Table,
table: &impl GetTomlValue,
state_dir: Option<PathBuf>,
use_gpu: bool,
) -> anyhow::Result<Option<RuntimeConfig>> {
Expand Down
4 changes: 2 additions & 2 deletions crates/factor-outbound-networking/src/runtime_config/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: GetTomlValue>(
pub fn config_from_table(
&self,
table: &T,
table: &impl GetTomlValue,
) -> anyhow::Result<Option<super::RuntimeConfig>> {
let Some(tls_configs) = self.tls_configs_from_table(table)? else {
return Ok(None);
Expand Down
4 changes: 2 additions & 2 deletions crates/factor-sqlite/src/runtime_config/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ impl RuntimeConfigResolver {
/// type = "$database-type"
/// ... extra type specific configuration ...
/// ```
pub fn resolve_from_toml<T: GetTomlValue>(
pub fn resolve_from_toml(
&self,
table: &T,
table: &impl GetTomlValue,
) -> anyhow::Result<Option<super::RuntimeConfig>> {
let Some(table) = table.get("sqlite_database") else {
return Ok(None);
Expand Down
4 changes: 2 additions & 2 deletions crates/factor-variables/src/spin_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuntimeConfig> {
pub fn runtime_config_from_toml(table: &impl GetTomlValue) -> anyhow::Result<RuntimeConfig> {
// Always include the environment variable provider.
let mut providers = vec![Box::<EnvVariablesProvider>::default() as _];
let Some(array) = table.get("variable_provider") else {
Expand Down
18 changes: 5 additions & 13 deletions crates/runtime-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,6 @@ impl<'a> TomlResolver<'a> {
}
}

impl AsRef<toml::Table> 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>,
Expand Down Expand Up @@ -226,7 +220,7 @@ impl FactorRuntimeConfigSource<KeyValueFactor> for TomlRuntimeConfigSource<'_, '
fn get_runtime_config(
&mut self,
) -> anyhow::Result<Option<spin_factor_key_value::RuntimeConfig>> {
self.key_value.resolve_from_toml(Some(self.toml.as_ref()))
self.key_value.resolve_from_toml(Some(&self.toml.table))
}
}

Expand All @@ -238,17 +232,15 @@ impl FactorRuntimeConfigSource<OutboundNetworkingFactor> 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)
}
}

impl FactorRuntimeConfigSource<VariablesFactor> for TomlRuntimeConfigSource<'_, '_> {
fn get_runtime_config(
&mut self,
) -> anyhow::Result<Option<<VariablesFactor as spin_factors::Factor>::RuntimeConfig>> {
Ok(Some(variables::runtime_config_from_toml(
self.toml.as_ref(),
)?))
Ok(Some(variables::runtime_config_from_toml(&self.toml.table)?))
}
}

Expand All @@ -266,7 +258,7 @@ impl FactorRuntimeConfigSource<OutboundMysqlFactor> for TomlRuntimeConfigSource<

impl FactorRuntimeConfigSource<LlmFactor> for TomlRuntimeConfigSource<'_, '_> {
fn get_runtime_config(&mut self) -> anyhow::Result<Option<spin_factor_llm::RuntimeConfig>> {
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)
}
}

Expand Down Expand Up @@ -296,7 +288,7 @@ impl FactorRuntimeConfigSource<OutboundMqttFactor> for TomlRuntimeConfigSource<'

impl FactorRuntimeConfigSource<SqliteFactor> for TomlRuntimeConfigSource<'_, '_> {
fn get_runtime_config(&mut self) -> anyhow::Result<Option<spin_factor_sqlite::RuntimeConfig>> {
self.sqlite.resolve_from_toml(self.toml.as_ref())
self.sqlite.resolve_from_toml(&self.toml.table)
}
}

Expand Down

0 comments on commit c132a03

Please sign in to comment.