Skip to content

Commit

Permalink
Finish implementing v2_schema_response_to_configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoverton committed Mar 13, 2024
1 parent 34a3f2d commit fb2adbe
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::PathBuf;
use clap::Subcommand;

use mongodb_agent_common::{interface_types::MongoConfig, schema::get_schema};
use configuration::Configuration;
use mongodb_connector::api_type_conversions::v2_schema_response_to_configuration;

/// The command invoked by the user.
#[derive(Debug, Clone, Subcommand)]
Expand Down Expand Up @@ -40,7 +40,6 @@ pub async fn run(command: Command, context: &Context) -> anyhow::Result<()> {
Ok(())
}


/// Initialize an empty directory with an empty connector configuration.
///
/// An empty configuration contains default settings and options, and is expected to be filled with
Expand All @@ -65,11 +64,11 @@ pub async fn run(command: Command, context: &Context) -> anyhow::Result<()> {
///
/// This expects a configuration with a valid connection URI.
async fn update(context: &Context) -> anyhow::Result<()> {
// let input_configuration: Configuration = configuration::read_directory(&context.path).await?;
let schema = get_schema(&context.mongo_config).await?;

// let output_configuration = input_configuration; // XXX TODO: update configuration
// configuration::write_directory(&context.path, &output_configuration).await?;
let configuration = v2_schema_response_to_configuration(schema);

configuration::write_directory(&context.path, &configuration).await?;

Ok(())
}
1 change: 1 addition & 0 deletions crates/mongodb-connector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ indexmap = { version = "2.1.0", features = ["serde"] }
itertools = "^0.10"
mongodb = "2.8"
mongodb-agent-common = { path = "../mongodb-agent-common" }
mongodb-support = { path = "../mongodb-support" }
ndc-sdk = { git = "https://github.com/hasura/ndc-hub.git" }
prometheus = "*" # share version from ndc-sdk
serde = { version = "1.0", features = ["derive"] }
Expand Down
85 changes: 54 additions & 31 deletions crates/mongodb-connector/src/api_type_conversions/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use configuration::{metadata::{Collection, ObjectField, ObjectType}, Configuration};
use configuration::{
metadata::{Collection, ObjectField, ObjectType},
Configuration,
};

pub fn v2_schema_response_to_configuration(
response: dc_api_types::SchemaResponse,
Expand All @@ -11,19 +14,15 @@ fn v2_schema_response_to_metadata(
response: dc_api_types::SchemaResponse,
) -> configuration::Metadata {
let table_object_types = response.tables.iter().map(table_to_object_type);
let nested_object_types =
response
.object_types
let nested_object_types = response.object_types.into_iter().map(|ot| ObjectType {
name: ot.name.to_string(),
description: ot.description,
fields: ot
.columns
.into_iter()
.map(|ot| ObjectType {
name: ot.name.to_string(),
description: ot.description,
fields: ot
.columns
.into_iter()
.map(column_info_to_object_field)
.collect(),
});
.map(column_info_to_object_field)
.collect(),
});
let object_types = table_object_types.chain(nested_object_types).collect();

let collections = response
Expand All @@ -41,19 +40,41 @@ fn v2_schema_response_to_metadata(
fn column_info_to_object_field(column_info: dc_api_types::ColumnInfo) -> ObjectField {
let t = v2_to_v3_column_type(column_info.r#type);
let is_nullable = column_info.nullable;
ObjectField {
name: column_info.name,
description: column_info.description.flatten(),
r#type: maybe_nullable(t, is_nullable),
}
ObjectField {
name: column_info.name,
description: column_info.description.flatten(),
r#type: maybe_nullable(t, is_nullable),
}
}

fn maybe_nullable(t: configuration::metadata::Type, is_nullable: bool) -> configuration::metadata::Type {
todo!()
fn maybe_nullable(
t: configuration::metadata::Type,
is_nullable: bool,
) -> configuration::metadata::Type {
if is_nullable {
configuration::metadata::Type::Nullable(Box::new(t))
} else {
t
}
}

fn v2_to_v3_column_type(r#type: dc_api_types::ColumnType) -> configuration::metadata::Type {
todo!()
fn v2_to_v3_column_type(t: dc_api_types::ColumnType) -> configuration::metadata::Type {
match t {
dc_api_types::ColumnType::Scalar(name) => {
let bson_scalar_type = mongodb_support::BsonScalarType::from_bson_name(&name).unwrap(); // XXX TODO: handle error
configuration::metadata::Type::Scalar(bson_scalar_type)
}
dc_api_types::ColumnType::Object(name) => {
configuration::metadata::Type::Object(name.to_string())
}
dc_api_types::ColumnType::Array {
element_type,
nullable,
} => configuration::metadata::Type::ArrayOf(Box::new(maybe_nullable(
v2_to_v3_column_type(*element_type),
nullable,
))),
}
}

fn table_to_object_type(table: &dc_api_types::TableInfo) -> ObjectType {
Expand All @@ -69,21 +90,23 @@ fn table_to_object_type(table: &dc_api_types::TableInfo) -> ObjectType {
}
}

fn collection_type_name_from_table_name(clone: Vec<String>) -> String {
todo!()
fn collection_type_name_from_table_name(table_name: Vec<String>) -> String {
name_from_qualified_name(table_name)
}

fn table_to_collection(
table: dc_api_types::TableInfo,
) -> Collection {
fn table_to_collection(table: dc_api_types::TableInfo) -> Collection {
let collection_type = collection_type_name_from_table_name(table.name.clone());
Collection {
name: name_from_qualified_name(table.name),
description: table.description.flatten(),
r#type: todo!(),
}
r#type: collection_type,
}
}

fn name_from_qualified_name(name: Vec<String>) -> String {
todo!()
// TODO: handle qualified names
pub fn name_from_qualified_name(qualified_name: Vec<String>) -> String {
qualified_name
.into_iter()
.last()
.expect("qualified name vec is not empty")
}

0 comments on commit fb2adbe

Please sign in to comment.