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

Feat/add md ds to connectors #55

Merged
merged 8 commits into from
Dec 11, 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
14 changes: 5 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ S3method(download_cnt,default)
S3method(list_content_cnt,connector_dbi)
S3method(list_content_cnt,connector_fs)
S3method(list_content_cnt,default)
S3method(print,cnts_datasources)
S3method(print,connectors)
S3method(print,nested_connectors)
S3method(read_cnt,connector_dbi)
S3method(read_cnt,connector_fs)
S3method(read_cnt,default)
Expand Down Expand Up @@ -48,9 +50,11 @@ export(connector_dbi)
export(connector_fs)
export(connectors)
export(create_directory_cnt)
export(datasources)
export(disconnect_cnt)
export(download_cnt)
export(list_content_cnt)
export(nested_connectors)
export(read_cnt)
export(read_ext)
export(read_file)
Expand All @@ -61,13 +65,5 @@ export(upload_cnt)
export(write_cnt)
export(write_ext)
export(write_file)
importFrom(R6,R6Class)
importFrom(checkmate,assert_list)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert)
importFrom(cli,cli_code)
importFrom(cli,cli_inform)
importFrom(cli,cli_text)
importFrom(jsonlite,read_json)
import(rlang)
importFrom(options,define_option)
importFrom(rlang,set_names)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

# connector dev

- Connectors constructor builds the datasources attribute
- Create a new class for nested connectors objects, "nested_connectors"
- Add README and vignette on how to extend connector


# connector 0.0.4 (2024-12-03)

### Migration:
Expand Down
71 changes: 41 additions & 30 deletions R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,19 @@ connect <- function(config = "_connector.yml", metadata = NULL, datasource = NUL
names(config) <- purrr::map(config, "name")
cnts <- config |>
purrr::map(\(x) connect(x, metadata, datasource, set_env))
return(do.call(connectors, cnts))

return(do.call(nested_connectors, cnts))
}

# Replace metadata if needed
if(!is.null(metadata)){
if (!is.null(metadata)) {
zephyr::msg(
c("Replace some metadata informations...")
)
config[["metadata"]] <- change_to_new_metadata(
old_metadata = config[["metadata"]],
new_metadata = metadata
)
old_metadata = config[["metadata"]],
new_metadata = metadata
)
}

connections <- config |>
Expand All @@ -113,7 +114,7 @@ connect <- function(config = "_connector.yml", metadata = NULL, datasource = NUL
filter_config(datasource = datasource) |>
connect_from_config()

if(logging){
if (logging) {
rlang::check_installed("connector.logger")
connections <- connector.logger::add_logs(connections)
}
Expand All @@ -128,20 +129,31 @@ connect_from_config <- function(config) {
purrr::map(create_connection) |>
rlang::set_names(purrr::map_chr(config$datasources, list("name", 1)))

## clean datasources
# unlist name of datasource
for(i in seq_along(config$datasources)){
config$datasources[[i]]$name <- config$datasources[[i]]$name[[1]]
}

connections$datasources <- as_datasources(config["datasources"])

do.call(what = connectors, args = connections)
}

#' @noRd
info_config <- function(config){
msg_ <- c(">" = "{.strong {config$name}}",
"*" = "{config$backend$type}",
"*" = "{config$backend[!names(config$backend) %in% 'type']}"
info_config <- function(config) {
msg_ <- c(
">" = "{.strong {config$name}}",
"*" = "{config$backend$type}",
"*" = "{config$backend[!names(config$backend) %in% 'type']}"
)

cli::cat_rule()
zephyr::msg(
c("Connection to:",
msg_),
c(
"Connection to:",
msg_
),
msg_fun = cli::cli_bullets
)
}
Expand All @@ -150,19 +162,18 @@ info_config <- function(config){
#' @param config [list] The configuration of a single connection
#' @noRd
create_connection <- function(config) {

info_config(config)

switch(config$backend$type,
"connector_fs" = {
create_backend_fs(config$backend)
},
"connector_dbi" = {
create_backend_dbi(config$backend)
},
{
create_backend(config$backend)
}
"connector_fs" = {
create_backend_fs(config$backend)
},
"connector_dbi" = {
create_backend_dbi(config$backend)
},
{
create_backend(config$backend)
}
)
}

Expand Down Expand Up @@ -305,20 +316,20 @@ assert_config <- function(config, env = parent.frame()) {
var <- paste0("datasources", y)
checkmate::assert_list(x, .var.name = var, add = val)
checkmate::assert_names(names(x),
type = "unique", must.include = c("name", "backend"),
.var.name = var, add = val
type = "unique", must.include = c("name", "backend"),
.var.name = var, add = val
)
checkmate::assert_character(x[["name"]],
len = 1,
.var.name = paste0(var, ".name"), add = val
len = 1,
.var.name = paste0(var, ".name"), add = val
)
checkmate::assert_list(x[["backend"]],
names = "unique",
.var.name = paste0(var, ".backend"), add = val
names = "unique",
.var.name = paste0(var, ".backend"), add = val
)
checkmate::assert_character(x[["backend"]][["type"]],
len = 1,
.var.name = paste0(var, ".backend.type"), add = val
len = 1,
.var.name = paste0(var, ".backend.type"), add = val
)
}
)
Expand Down
2 changes: 0 additions & 2 deletions R/connect_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#' @param old_metadata [list] a list of element to be replace
#' @param new_metadata [list] a list of element to replace old's
#'
#' @importFrom checkmate assert_list
#'
#' @return [list] a updated list with new data
#' @noRd
change_to_new_metadata <- function(old_metadata, new_metadata) {
Expand Down
1 change: 1 addition & 0 deletions R/connector-package.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#' @keywords internal
#' @import rlang
"_PACKAGE"

## usethis namespace: start
Expand Down
8 changes: 6 additions & 2 deletions R/connector.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#'
#' read_cnt(cnt_my_class)
#'
#' @importFrom R6 R6Class
#' @export

connector <- R6::R6Class(
Expand Down Expand Up @@ -122,7 +121,12 @@ print_cnt <- function(connector_object) {
which() |>
utils::head(1)

specs <- if(R6::is.R6(connector_object)) {connector_object$.__enclos_env__$.__active__} else {NULL}
specs <- if (R6::is.R6(connector_object)) {
connector_object$.__enclos_env__$.__active__
} else {
NULL
}

if (length(specs) == 0) {
specs <- NULL
}
Expand Down
98 changes: 96 additions & 2 deletions R/connectors.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,34 @@
#' @export
connectors <- function(...) {
x <- rlang::list2(...)
ds_ <- x[["datasources"]]

if (!is.null(ds_) & !inherits(ds_, "cnts_datasources")) {
cli::cli_abort("'datasources' is a reserved name. It cannot be used as a name for a data source.")
}

if (is.null(ds_)) {
cnts <- substitute(rlang::list2(...))
datasources <- connectors_to_datasources(cnts)
} else {
datasources <- ds_
}

checkmate::assert_list(x = x, names = "named")
structure(
x,
class = c("connectors")
x[names(x) != "datasources"],
class = c("connectors"),
datasources = datasources
)
}

#' @export
print.connectors <- function(x, ...) {
print_connectors(x, ...)
}

#' @noRd
print_connectors <- function(x, ...) {
classes <- x |>
lapply(\(x) class(x)[[1]]) |>
unlist()
Expand All @@ -55,3 +74,78 @@ print.connectors <- function(x, ...) {
)
return(invisible(x))
}

#' @export
print.cnts_datasources <- function(x, ...) {
cli::cli_h1("Datasources")

for(ds in x[["datasources"]]) {
cli::cli_h2(ds$name)
cli::cli_ul()
cli::cli_li("Backend Type: {.val {ds$backend$type}}")
for (param_name in names(ds$backend)[names(ds$backend) != "type"]) {
cli::cli_li("{param_name}: {.val {ds$backend[[param_name]]}}")
}
cli::cli_end()
cli::cli_end()
}

return(x)
}

#' @noRd
as_datasources <- function(...) {
structure(
...,
class = "cnts_datasources"
)
}

#' Extract data sources from connectors
#'
#' This function extracts the "datasources" attribute from a connectors object.
#'
#' @param connectors An object containing connectors with a "datasources" attribute.
#'
#' @return An object containing the data sources extracted from the "datasources" attribute.
#'
#' @details
#' The function uses the `attr()` function to access the "datasources" attribute
#' of the `connectors` object. It directly returns this attribute without any
#' modification.
#'
#' @examples
#' # Assume we have a 'my_connectors' object with a 'datasources' attribute
#' my_connectors <- list()
#' attr(my_connectors, "datasources") <- list(source1 = "data1", source2 = "data2")
#'
#' # Using the function
#' result <- datasources(my_connectors)
#' print(result)
#'
#' @export
datasources <- function(connectors) {
vladimirobucina marked this conversation as resolved.
Show resolved Hide resolved
ds <- attr(connectors, "datasources")
ds
}

#' Create a nested connectors object
#'
#' This function creates a nested connectors object from the provided arguments.
#'
#' @param ... Any number of connectors object.
#'
#' @return A list with class "nested_connectors" containing the provided arguments.
#' @export
nested_connectors <- function(...) {
x <- rlang::list2(...)
structure(
x,
class = c("nested_connectors")
)
}

#' @export
print.nested_connectors <- function(x, ...) {
print_connectors(x, ...)
}
Loading
Loading