Skip to content

Commit

Permalink
rjson::fromJSON can read URL, too. So implement reading URL schema in…
Browse files Browse the repository at this point in the history
… the same function as reading a file.
  • Loading branch information
Anthony Williams authored and Anthony Williams committed Feb 22, 2022
1 parent 0c87fe3 commit c4423cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
15 changes: 4 additions & 11 deletions R/get-template.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ get_template_keys_synID <- function(syn, synID, ...) {
#' @export
#' @inheritParams get_template
#' @param id Registered synapse ID of schema.
#' @param file Filepath to json schema.
#' @param url URL to json schema.
get_template_keys_schema <- function(syn, id = NA, file = NA, url = NA) {
#' @param file Filepath or URL to json schema.
get_template_keys_schema <- function(syn, id = NA, file = NA) {

if (sum(!is.na(id), !is.na(file), !is.na(url)) != 1){
stop("Specify only one of id, file, or url")
if (sum(!is.na(id), !is.na(file)) != 1){
stop("Specify only one of id, file")
}

if (!is.na(id)){
Expand All @@ -107,10 +106,6 @@ get_template_keys_schema <- function(syn, id = NA, file = NA, url = NA) {
if (!is.na(file)) {
schema <- get_file_schema(file = file)
}
if (!is.na(url)) {
schema <- get_url_schema(url = url)
}

return(names(schema$properties))
}

Expand Down Expand Up @@ -204,8 +199,6 @@ rest_post <- function(syn, uri, body = NULL) {
#' @param file Filepath of json schema.
get_file_schema <- function(file) {

if (!file.exists(file)) stop(glue::glue("{file} does not exist."))
if (!tolower(tools::file_ext(file)) == "json") stop("File must be json.")
schema <- fromJSON(file = file)
return(schema)

Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-get-template.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ test_that("get_template can get a schema file", {
"specimenID", "platform", "assay", "readLength", "dnaBatch", "Component",
"assayTarget", "libraryBatch"))

schema_url <- get_file_schema(file="https://raw.githubusercontent.com/afwillia/sysbioDCCjsonschemas/schematic_workflow/schema_metadata_templates/schematic_test/amp.ad.data.Assay16SrRNAseqMetadataTemplate.schema.json")
expect_equal(names(schema_url$properties),
c("dnaExtractionMethod", "runType", "libraryPrep", "sequencingBatch",
"specimenID", "platform", "assay", "readLength", "dnaBatch", "Component",
"assayTarget", "libraryBatch"))
})

test_that("get_synapse_schema returns error if schema not returned", {
Expand Down

0 comments on commit c4423cd

Please sign in to comment.