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

Add input validation for setter and read functions #231

Merged
merged 10 commits into from
Jan 18, 2023
3 changes: 2 additions & 1 deletion util-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ verify.data.frame.columns = function(data, columns, data.types = NULL) {
## therefore if there aren't as many datatypes provided in \code{data.types} as column names have
## been provided in \code{columns} we can stop here already
if (!is.null(data.types) && length(columns) != length(data.types)) {
error.message = sprintf("If specified, the length of the two given vectors 'columns' and 'data.types' must match.")
error.message = sprintf(paste("If specified, the length of the two given vectors",
"'columns' and 'data.types' must match."))
bockthom marked this conversation as resolved.
Show resolved Hide resolved
logging::logerror(error.message)
stop(error.message)
}
Expand Down
23 changes: 7 additions & 16 deletions util-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -918,28 +918,19 @@ read.custom.event.timestamps = function(data.path, file.name) {
timestamps = as.list(custom.event.timestamps.table[[2]])
names(timestamps) = custom.event.timestamps.table[[1]]

## check if first column of input has type character
if (class(names(timestamps)) != "character") {
error.message = sprintf(
"The first column of custom event timestamps should be of type character but is '%s'",
class(names(timestamps)))
## convert all timestamps to POSIXct format
posix.timestamps = get.date.from.string(timestamps)

## if a timestamp is malformatted get.date.from.string returns a NA
if (any(is.na(posix.timestamps))) {
error.message = sprintf("Input timestamps are not in POSIXct format (YYYY-mm-DD HH:MM:SS).")
logging::logerror(error.message)
stop(error.message)
}

## check if second column of input is in POSIXct format by converting
## all elements to POSIXct and catch possible errors
tryCatch(sapply(timestamps, function(time) as.POSIXct(time)),
error = function(e) {
error.message = sprintf("The second column of custom event timestamps should be in POSIXct format")
logging::logerror(error.message)
stop(error.message)
})


## Sort the timestamps
if (length(timestamps) != 0) {
timestamps = timestamps[order(unlist(get.date.from.string(timestamps)))]
timestamps = timestamps[order(unlist(posix.timestamps))]
}

logging::logdebug("read.custom.event.timestamps: finished.")
Expand Down