-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from natverse/feature/checkbodyids
Feature/checkbodyids
- Loading branch information
Showing
9 changed files
with
87 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,8 @@ Imports: | |
nat, | ||
readobj, | ||
memoise, | ||
dplyr | ||
dplyr, | ||
bit64 | ||
Remotes: | ||
natverse/drvid | ||
Encoding: UTF-8 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# private function to convert 1 or more 64 bit ids (e.g. body ids) to JSON | ||
id2json <- function(x, uniqueids=TRUE, ...) { | ||
bx=id2bit64(x) | ||
if(isTRUE(uniqueids)) bx=unique(bx) | ||
jsonlite::toJSON(bx, ...) | ||
} | ||
|
||
# ... this bit might also be useful | ||
id2bit64 <- function(x) { | ||
x=unlist(x, use.names = FALSE) | ||
if(is.factor(x)) { | ||
x=as.character(x) | ||
} | ||
if(isTRUE(is.character(x)) || bit64::is.integer64(x)) { | ||
# do nothing | ||
} else if(is.integer(x)){ | ||
BIGGESTINT=2147483647L | ||
if(any(x>BIGGESTINT)) | ||
stop("Some ids cannot be exactly represented! Please use character or bit64!") | ||
} else if(is.double(x)) { | ||
# biggest int that can be represented as double 2^(mantissa bits + 1) | ||
BIGGESTINT=2^53+1 | ||
if(any(x>BIGGESTINT)) | ||
stop("Some ids cannot be exactly represented! Please use character or bit64!") | ||
} else { | ||
stop("Unexpected data type for id. Use character, bit64, or numeric!") | ||
} | ||
bit64::as.integer64(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
test_that("id conversion works", { | ||
bigid="9223372036854775806" | ||
big.json="[9223372036854775806]" | ||
big.json2="[9223372036854775806,9223372036854775806]" | ||
|
||
medid="223372036854775806" | ||
med.json="[223372036854775806]" | ||
|
||
expect_equal(as.character(id2json(bigid)), | ||
big.json) | ||
expect_equal(as.character(id2json(bit64::as.integer64(bigid))), | ||
big.json) | ||
expect_error(id2json(as.numeric(bigid))) | ||
expect_error(id2json(NA)) | ||
expect_equal(as.character(id2json(medid)), | ||
med.json) | ||
expect_equal(as.character(id2json(bit64::as.integer64(medid))), | ||
med.json) | ||
expect_equal(as.character(id2json(as.factor(medid))), | ||
med.json) | ||
|
||
expect_equal(as.character(id2json(c(bigid, bigid))), | ||
big.json) | ||
expect_equal(as.character(id2json(c(bigid, bigid), uniqueids=FALSE)), | ||
big.json2) | ||
expect_equal(as.character(id2json(list(bigid, bigid))), | ||
big.json) | ||
}) |