Skip to content

Commit

Permalink
Improve input validation
Browse files Browse the repository at this point in the history
Check if the 'bins' parameter of the 'split.data.by.bins' actually
contains 'bins' component. Use 'get.date.from.string' instead of
accessing 'lubridate::ymd_hms' directly, to encapsulate date conversion.
Allow 'vector' component of 'bins' to be of any subclass of
'numeric' instead of explicitly 'numeric'.

Disallow lists that contain elements that are not representing a date
in 'split.data.time.based' as they do not comply with the expected
format of bins for 'split.data.by.time.or.bins'.

Signed-off-by: Maximilian Löffler <[email protected]>
  • Loading branch information
maxloeffler committed Nov 28, 2023
1 parent cdc00f0 commit ba2c8a1
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions util-split.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ split.data.time.based = function(project.data, time.period = "3 months", bins =
number.windows = NULL, split.basis = c("commits", "mails", "issues"),
sliding.window = FALSE, project.conf.new = NULL) {

# validate type of the 'bins' parameter
# validate existence and type of the 'bins' parameter
if (!is.null(bins) && !lubridate::is.POSIXct(bins)) {
dates = parallel::mclapply(bins, function(bin) lubridate::ymd_hms(bin, truncated = 3))
dates = parallel::mclapply(unlist(bins), get.date.from.string)
if (any(is.na(dates))) {
logging::logerror("The bins parameter, if present, needs to be a character representing a date")
logging::logerror(paste("The bins parameter, if present, needs to be vector",
"whose elements represent dates"))
stop("Stopped due to incorrect parameter types")
}
}
Expand Down Expand Up @@ -104,21 +105,27 @@ split.data.by.bins = function(project.data, activity.amount, bins, split.basis =
stop("Stopped due to incorrect parameter types")
}

# validate type of the 'bins' component of the 'bins' parameter
dates = parallel::mclapply(bins[["bins"]], function(bin) lubridate::ymd_hms(bin, truncated = 3))
# validate existence and type of the 'bins' component of the 'bins' parameter
if (!("bins" %in% names(bins))) {
logging::logerror("The 'bins' parameter needs to include a component 'bins'")
stop("Stopped due to incorrect parameter types")
}

dates = parallel::mclapply(bins[["bins"]], get.date.from.string)
if (any(is.na(dates))) {
logging::logerror("The 'bins' component of the bins parameter needs to be a character representing a date")
logging::logerror(paste("The 'bins' component of the 'bins' parameter, needs to be vector",
"whose elements represent dates"))
stop("Stopped due to incorrect parameter types")
}

# validate type of the 'vector' component of the 'bins' parameter
if (class(bins[["vector"]]) != "numeric") {
# validate existence and type of the 'vector' component of the 'bins' parameter
if (!inherits(bins[["vector"]], "numeric")) {
logging::logerror("The 'vector' component of the bins parameter needs to be a numeric vector")
stop("Stopped due to incorrect parameter types")
}

split = split.data.by.time.or.bins(project.data, activity.amount, bins, split.by.time = FALSE,
sliding.window = sliding.window, split.basis = split.basis)
sliding.window = sliding.window, split.basis = split.basis)
return(split)
}

Expand Down

0 comments on commit ba2c8a1

Please sign in to comment.