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 update sha1.default() to work with any class #226

Merged
merged 13 commits into from
Jan 3, 2025
Merged
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2025-01-01 Bill Denney <[email protected]>

* R/sha1.R: Default sha1 method now works for all classes by default. If you
eddelbuettel marked this conversation as resolved.
Show resolved Hide resolved
were previously using one of the methods directly (like `sha1.call()`),
please switch to using `sha1()` directly.

2024-12-31 Bill Denney <[email protected]>

* DESCRiPTION (Description): Correct typos
Expand Down
6 changes: 0 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ S3method(print, AES)

S3method(sha1, anova)
S3method(sha1, array)
S3method(sha1, call)
S3method(sha1, character)
S3method(sha1, complex)
S3method(sha1, data.frame)
S3method(sha1, Date)
S3method(sha1, default)
S3method(sha1, integer)
S3method(sha1, factor)
S3method(sha1, formula)
S3method(sha1, "function")
S3method(sha1, logical)
S3method(sha1, list)
S3method(sha1, matrix)
S3method(sha1, name)
Expand All @@ -37,7 +32,6 @@ S3method(sha1, numeric)
S3method(sha1, pairlist)
S3method(sha1, POSIXct)
S3method(sha1, POSIXlt)
S3method(sha1, raw)
S3method(sha1, "(")

S3method(makeRaw, default)
Expand Down
14 changes: 1 addition & 13 deletions R/sha1.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ sha1.default <- function(x, digits = 14L, zapsmall = 7L, ..., algo = "sha1") {
sha1.list(x, digits = digits, zapsmall = zapsmall, ..., algo = algo)
)
}
stop( # #nocov start
"sha1() has no method for the '",
paste(class(x), collapse = "', '"),
"' class",
call. = FALSE
) # #nocov end
sha1_attr_digest(x = x, digits = digits, zapsmall = zapsmall, ..., algo = algo)
}

sha1.numeric <- function(x, digits = 14L, zapsmall = 7L, ..., algo = "sha1"){
Expand Down Expand Up @@ -306,13 +301,6 @@ sha1_attr_digest <- function(x, digits = 14L, zapsmall = 7L, ..., algo = "sha1")
digest(x, algo = algo)
}

sha1.call <- function(...) {sha1_attr_digest(...)}
sha1.character <- function(...) {sha1_attr_digest(...)}
sha1.factor <- function(...) {sha1_attr_digest(...)}
sha1.logical <- function(...) {sha1_attr_digest(...)}
sha1.integer <- function(...) {sha1_attr_digest(...)}
sha1.raw <- function(...) {sha1_attr_digest(...)}

eddelbuettel marked this conversation as resolved.
Show resolved Hide resolved
# sha1_digest variants ####

sha1_digest <- function(x, digits = 14L, zapsmall = 7L, ..., algo = "sha1") {
Expand Down
16 changes: 16 additions & 0 deletions inst/tinytest/test_sha1.R
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,19 @@ saveRDS(x, f)
y <- readRDS(f)
expect_identical(sha1(x), sha1(y))
expect_identical(sha1(x, environment = FALSE), sha1(y, environment = FALSE))

# Check that default sha1 method works for many types (#224)
# Class: <-
expect_true(is.character(sha1(str2lang("a <- b"))))
# Class: character
expect_true(is.character(sha1("a")))
# Class: call
expect_true(is.character(sha1(str2lang("a(b)"))))
# Class: environment
expect_true(is.character(sha1(new.env())))
# Class: factor
expect_true(is.character(sha1(factor())))
# Class: integer
expect_true(is.character(sha1(1L)))
# Class: raw
expect_true(is.character(sha1(as.raw(1))))
12 changes: 0 additions & 12 deletions man/sha1.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
\alias{sha1_attr_digest}
\alias{sha1_digest}
\alias{sha1.array}
\alias{sha1.integer}
\alias{sha1.numeric}
\alias{sha1.character}
\alias{sha1.complex}
\alias{sha1.Date}
\alias{sha1.factor}
\alias{sha1.NULL}
\alias{sha1.logical}
\alias{sha1.matrix}
\alias{sha1.data.frame}
\alias{sha1.list}
Expand All @@ -21,8 +17,6 @@
\alias{sha1.POSIXct}
\alias{sha1.anova}
\alias{sha1.function}
\alias{sha1.call}
\alias{sha1.raw}
\alias{sha1.formula}
\alias{sha1.(}
\title{Calculate a SHA1 hash of an object}
Expand Down Expand Up @@ -50,12 +44,6 @@ sha1_digest(x, digits = 14, zapsmall = 7, ..., algo = "sha1")
\method{sha1}{name}(...)

sha1_attr_digest(x, digits = 14, zapsmall = 7, ..., algo = "sha1")
\method{sha1}{call}(...)
\method{sha1}{character}(...)
\method{sha1}{factor}(...)
\method{sha1}{integer}(...)
\method{sha1}{logical}(...)
\method{sha1}{raw}(...)
}
\arguments{
\item{x}{the object to calculate the SHA1}
Expand Down
Loading