Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove constraints from default_main where possible. #96

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rust-connector-sdk/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ pub enum MutationError {
#[async_trait]
pub trait Connector {
/// The type of unvalidated, raw configuration, as provided by the user.
type RawConfiguration;
type RawConfiguration: Sync + Send;
/// The type of validated configuration
type Configuration;
type Configuration: Sync + Send;
/// The type of unserializable state
type State;
type State: Sync + Send;

fn make_empty_configuration() -> Self::RawConfiguration;

Expand Down
82 changes: 38 additions & 44 deletions rust-connector-sdk/src/default_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,27 @@ struct CheckHealthCommand {

type Port = u16;

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct ServerState<C: Connector> {
configuration: C::Configuration,
state: C::State,
metrics: Registry,
}

impl<C: Connector> Clone for ServerState<C>
where
C::Configuration: Clone,
C::State: Clone,
{
fn clone(&self) -> Self {
Self {
configuration: self.configuration.clone(),
state: self.state.clone(),
metrics: self.metrics.clone(),
}
}
}

/// A default main function for a connector.
///
/// The intent is that this function can replace your `main` function
Expand All @@ -158,12 +172,11 @@ pub struct ServerState<C: Connector> {
/// - It reads configuration as JSON from a file specified on the command line,
/// - It reports traces to an OTLP collector specified on the command line,
/// - Logs are written to stdout
pub async fn default_main<C: Connector + Clone + Default + 'static>(
) -> Result<(), Box<dyn Error + Send + Sync>>
pub async fn default_main<C: Connector + 'static>() -> Result<(), Box<dyn Error + Send + Sync>>
where
C::RawConfiguration: Serialize + DeserializeOwned + JsonSchema + Sync + Send,
C::Configuration: Serialize + DeserializeOwned + Sync + Send + Clone,
C::State: Sync + Send + Clone,
C::RawConfiguration: Serialize + DeserializeOwned + JsonSchema,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking the argument of this PR a bit further, is there any sense to any impl Connector for C that doesn't satisfy the constraints of default_main?

A connector that doesn't might as well not use ndc-sdk at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you can make your own main, in theory. I was wondering this but didn't want to push it too far.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe. I'm just wondering what if anything anyone who makes their own main function actually gets from using the sdk.

At any rate this is still a fine improvement.

C::Configuration: Clone + Serialize,
C::State: Clone,
{
let CliArgs { command } = CliArgs::parse();

Expand All @@ -176,13 +189,13 @@ where
}
}

async fn serve<C: Connector + Clone + Default + 'static>(
async fn serve<C: Connector + 'static>(
serve_command: ServeCommand,
) -> Result<(), Box<dyn Error + Send + Sync>>
where
C::RawConfiguration: DeserializeOwned + Sync + Send,
C::Configuration: Serialize + DeserializeOwned + Sync + Send + Clone,
C::State: Sync + Send + Clone,
C::RawConfiguration: DeserializeOwned,
C::Configuration: Clone + Serialize,
C::State: Clone,
{
init_tracing(&serve_command.service_name, &serve_command.otlp_endpoint)
.expect("Unable to initialize tracing");
Expand Down Expand Up @@ -242,13 +255,11 @@ where
}

/// Initialize the server state from the configuration file.
pub async fn init_server_state<C: Connector + Clone + Default + 'static>(
pub async fn init_server_state<C: Connector + 'static>(
config_file: impl AsRef<Path>,
) -> ServerState<C>
where
C::RawConfiguration: DeserializeOwned + Sync + Send,
C::Configuration: Serialize + DeserializeOwned + Sync + Send + Clone,
C::State: Sync + Send + Clone,
C::RawConfiguration: DeserializeOwned,
{
let configuration_json = std::fs::read_to_string(config_file).unwrap();
let raw_configuration =
Expand All @@ -269,14 +280,13 @@ where
}
}

pub fn create_router<C: Connector + Clone + 'static>(
pub fn create_router<C: Connector + 'static>(
state: ServerState<C>,
service_token_secret: Option<String>,
) -> Router
where
C::RawConfiguration: DeserializeOwned + Sync + Send,
C::Configuration: Serialize + Clone + Sync + Send,
C::State: Sync + Send + Clone,
C::Configuration: Clone,
C::State: Clone,
{
let router = Router::new()
.route("/capabilities", get(get_capabilities::<C>))
Expand Down Expand Up @@ -350,14 +360,13 @@ where
))
}

pub fn create_v2_router<C: Connector + Clone + 'static>(
pub fn create_v2_router<C: Connector + 'static>(
state: ServerState<C>,
service_token_secret: Option<String>,
) -> Router
where
C::RawConfiguration: DeserializeOwned + Sync + Send,
C::Configuration: Serialize + Clone + Sync + Send,
C::State: Sync + Send + Clone,
C::Configuration: Clone + Serialize,
C::State: Clone,
{
Router::new()
.route("/schema", post(v2_compat::post_schema::<C>))
Expand Down Expand Up @@ -480,8 +489,8 @@ async fn configuration<C: Connector + 'static>(
command: ConfigurationCommand,
) -> Result<(), Box<dyn Error + Send + Sync>>
where
C::RawConfiguration: Serialize + DeserializeOwned + JsonSchema + Sync + Send,
C::Configuration: Sync + Send + Serialize,
C::RawConfiguration: Serialize + DeserializeOwned + JsonSchema,
C::Configuration: Serialize,
{
match command.command {
ConfigurationSubcommand::Serve(serve_command) => {
Expand All @@ -494,8 +503,8 @@ async fn serve_configuration<C: Connector + 'static>(
serve_command: ServeConfigurationCommand,
) -> Result<(), Box<dyn Error + Send + Sync>>
where
C::RawConfiguration: Serialize + DeserializeOwned + JsonSchema + Sync + Send,
C::Configuration: Sync + Send + Serialize,
C::RawConfiguration: Serialize + DeserializeOwned + JsonSchema,
C::Configuration: Serialize,
{
let port = serve_command.port;
let address = net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::UNSPECIFIED), port);
Expand Down Expand Up @@ -543,19 +552,13 @@ where
Ok(())
}

async fn get_empty<C: Connector>() -> Json<C::RawConfiguration>
where
C::RawConfiguration: Serialize,
{
async fn get_empty<C: Connector>() -> Json<C::RawConfiguration> {
Json(C::make_empty_configuration())
}

async fn post_update<C: Connector>(
WithRejection(Json(configuration), _): WithRejection<Json<C::RawConfiguration>, JsonRejection>,
) -> Result<Json<C::RawConfiguration>, (StatusCode, String)>
where
C::RawConfiguration: Serialize + DeserializeOwned,
{
) -> Result<Json<C::RawConfiguration>, (StatusCode, String)> {
let updated = C::update_configuration(configuration)
.await
.map_err(|err| match err {
Expand Down Expand Up @@ -594,7 +597,6 @@ async fn post_validate<C: Connector>(
WithRejection(Json(configuration), _): WithRejection<Json<C::RawConfiguration>, JsonRejection>,
) -> Result<Json<ValidateResponse>, (StatusCode, Json<ValidateErrors>)>
where
C::RawConfiguration: DeserializeOwned,
C::Configuration: Serialize,
{
let configuration =
Expand Down Expand Up @@ -671,11 +673,7 @@ struct ConnectorAdapter<C: Connector> {
}

#[async_trait]
impl<C: Connector> ndc_test::Connector for ConnectorAdapter<C>
where
C::Configuration: Send + Sync + 'static,
C::State: Send + Sync + 'static,
{
impl<C: Connector> ndc_test::Connector for ConnectorAdapter<C> {
async fn get_capabilities(
&self,
) -> Result<ndc_client::models::CapabilitiesResponse, ndc_test::Error> {
Expand Down Expand Up @@ -726,8 +724,6 @@ async fn test<C: Connector + 'static>(
) -> Result<(), Box<dyn Error + Send + Sync>>
where
C::RawConfiguration: DeserializeOwned,
C::Configuration: Sync + Send + 'static,
C::State: Send + Sync + 'static,
{
let test_configuration = ndc_test::TestConfiguration {
seed: command.seed,
Expand All @@ -752,8 +748,6 @@ async fn replay<C: Connector + 'static>(
) -> Result<(), Box<dyn Error + Send + Sync>>
where
C::RawConfiguration: DeserializeOwned,
C::Configuration: Sync + Send + 'static,
C::State: Send + Sync + 'static,
{
let connector = make_connector_adapter::<C>(command.configuration).await;
let results = ndc_test::test_snapshots_in_directory(&connector, command.snapshots_dir).await;
Expand Down
Loading