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.
Also allow 'vector' component of 'bins' to be of any subclass of
'numeric' instead of explicitly 'numeric'.

Signed-off-by: Maximilian Löffler <[email protected]>
  • Loading branch information
maxloeffler committed Nov 20, 2023
1 parent cdc00f0 commit bb5aeb2
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions util-split.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ 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(bins, function(bin) get.date.from.string(bin))
if (any(is.na(dates))) {
logging::logerror("The bins parameter, if present, needs to be a character representing a date")
stop("Stopped due to incorrect parameter types")
Expand Down Expand Up @@ -104,21 +104,26 @@ 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"]], function(bin) get.date.from.string(bin))
if (any(is.na(dates))) {
logging::logerror("The 'bins' component of the bins parameter needs to be a character representing a date")
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 bb5aeb2

Please sign in to comment.