Skip to content

Commit

Permalink
Be generic over config resolver
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Jul 22, 2024
1 parent b48c047 commit 10f7b42
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
17 changes: 7 additions & 10 deletions crates/factor-key-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use std::{
};

use anyhow::ensure;
use runtime_config::RuntimeConfig;
use serde::de::DeserializeOwned;
use runtime_config::{RuntimeConfig, RuntimeConfigResolver};
use spin_factors::{
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, InstanceBuilders,
PrepareContext, RuntimeFactors,
Expand All @@ -20,22 +19,20 @@ use spin_key_value::{
};
pub use store::MakeKeyValueStore;

pub struct KeyValueFactor<C> {
runtime_config_resolver: Arc<dyn runtime_config::RuntimeConfigResolver<Config = C>>,
pub struct KeyValueFactor<R> {
runtime_config_resolver: Arc<R>,
}

impl<C> KeyValueFactor<C> {
pub fn new(
runtime_config_resolver: impl runtime_config::RuntimeConfigResolver<Config = C> + 'static,
) -> Self {
impl<R> KeyValueFactor<R> {
pub fn new(runtime_config_resolver: R) -> Self {
Self {
runtime_config_resolver: Arc::new(runtime_config_resolver),
}
}
}

impl<C: DeserializeOwned + 'static> Factor for KeyValueFactor<C> {
type RuntimeConfig = RuntimeConfig<C>;
impl<R: RuntimeConfigResolver + 'static> Factor for KeyValueFactor<R> {
type RuntimeConfig = RuntimeConfig<R::Config>;
type AppState = AppState;
type InstanceBuilder = InstanceBuilder;

Expand Down
18 changes: 9 additions & 9 deletions crates/factor-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ use std::sync::Arc;
use host::InstanceState;

use async_trait::async_trait;
use serde::de::DeserializeOwned;
use runtime_config::RuntimeConfigResolver;
use spin_factors::{anyhow, Factor};
use spin_locked_app::MetadataKey;
use spin_world::v1::sqlite as v1;
use spin_world::v2::sqlite as v2;

pub struct SqliteFactor<C> {
runtime_config_resolver: Arc<dyn runtime_config::RuntimeConfigResolver<Config = C>>,
pub struct SqliteFactor<R> {
runtime_config_resolver: Arc<R>,
}

impl<C> SqliteFactor<C> {
impl<R> SqliteFactor<R> {
/// Create a new `SqliteFactor`
pub fn new(
runtime_config_resolver: impl runtime_config::RuntimeConfigResolver<Config = C> + 'static,
) -> Self {
///
/// Takes a `runtime_config_resolver` that can resolve a runtime configuration into a connection pool.
pub fn new(runtime_config_resolver: R) -> Self {
Self {
runtime_config_resolver: Arc::new(runtime_config_resolver),
}
}
}

impl<C: DeserializeOwned + 'static> Factor for SqliteFactor<C> {
type RuntimeConfig = runtime_config::RuntimeConfig<C>;
impl<R: RuntimeConfigResolver + 'static> Factor for SqliteFactor<R> {
type RuntimeConfig = runtime_config::RuntimeConfig<R::Config>;
type AppState = AppState;
type InstanceBuilder = InstanceState;

Expand Down
2 changes: 1 addition & 1 deletion crates/factor-sqlite/src/runtime_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<C: DeserializeOwned> FactorRuntimeConfig for RuntimeConfig<C> {

/// Resolves some piece of runtime configuration to a connection pool
pub trait RuntimeConfigResolver: Send + Sync {
type Config;
type Config: DeserializeOwned;

/// Get a connection pool for a given config.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/factor-sqlite/tests/factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use spin_factors_test::{toml, TestEnvironment};

#[derive(RuntimeFactors)]
struct TestFactors {
sqlite: SqliteFactor<RuntimeConfig>,
sqlite: SqliteFactor<RuntimeConfigResolver>,
}

#[tokio::test]
Expand Down
2 changes: 1 addition & 1 deletion crates/factors/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Factors {
variables: VariablesFactor,
outbound_networking: OutboundNetworkingFactor,
outbound_http: OutboundHttpFactor,
key_value: KeyValueFactor<StoreConfig>,
key_value: KeyValueFactor<DelegatingRuntimeConfigResolver>,
}

struct Data {
Expand Down

0 comments on commit 10f7b42

Please sign in to comment.