add toString method to oneOf/anyOf objects (#12727)

This commit is contained in:
William Cheng 2022-06-29 11:01:56 +08:00 committed by GitHub
parent 3feb11acbb
commit 0232d1cd00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 15 deletions

View File

@ -69,11 +69,11 @@
#' @description
#' Serialize {{{classname}}} to JSON string.
#'
#' @return JSON string reprenation of the {{{classname}}}.
#' @return JSON string representation of the {{{classname}}}.
#' @export
toJSONString = function() {
if (!is.null(self$actual_instance)) {
self$actual_instance$toJSONString()
as.character(jsonlite::minify((self$actual_instance$toJSONString())))
} else {
NULL
}
@ -83,7 +83,7 @@
#' @description
#' Serialize {{{classname}}} to JSON.
#'
#' @return JSON reprenation of the {{{classname}}}.
#' @return JSON representation of the {{{classname}}}.
#' @export
toJSON = function() {
if (!is.null(self$actual_instance)) {
@ -111,6 +111,22 @@
# no error thrown, restore old values
self$actual_instance <- actual_instance_bak
self$actual_type <- actual_type_bak
},
#' Returns the string representation of the instance.
#'
#' @description
#' Returns the string representation of the instance.
#'
#' @return The string representation of the instance.
#' @export
toString = function() {
jsoncontent <- c(
sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) {NULL} else {self$actual_instance$toJSONString()}),
sprintf('"actual_type": "%s"', self$actual_type),
sprintf('"any_of": "%s"', paste( unlist(self$any_of), collapse=', '))
)
jsoncontent <- paste(jsoncontent, collapse = ",")
as.character(jsonlite::prettify(paste('{', jsoncontent, '}', sep = "")))
}
)
)

View File

@ -84,11 +84,11 @@
#' @description
#' Serialize {{{classname}}} to JSON string.
#'
#' @return JSON string reprenation of the {{{classname}}}.
#' @return JSON string representation of the {{{classname}}}.
#' @export
toJSONString = function() {
if (!is.null(self$actual_instance)) {
self$actual_instance$toJSONString()
as.character(jsonlite::minify(self$actual_instance$toJSONString()))
} else {
NULL
}
@ -98,7 +98,7 @@
#' @description
#' Serialize {{{classname}}} to JSON.
#'
#' @return JSON reprenation of the {{{classname}}}.
#' @return JSON representation of the {{{classname}}}.
#' @export
toJSON = function() {
if (!is.null(self$actual_instance)) {
@ -126,6 +126,22 @@
# no error thrown, restore old values
self$actual_instance <- actual_instance_bak
self$actual_type <- actual_type_bak
},
#' Returns the string representation of the instance.
#'
#' @description
#' Returns the string representation of the instance.
#'
#' @return The string representation of the instance.
#' @export
toString = function() {
jsoncontent <- c(
sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) {NULL} else {self$actual_instance$toJSONString()}),
sprintf('"actual_type": "%s"', self$actual_type),
sprintf('"one_of": "%s"', paste( unlist(self$one_of), collapse=', '))
)
jsoncontent <- paste(jsoncontent, collapse = ",")
as.character(jsonlite::prettify(paste('{', jsoncontent, '}', sep = "")))
}
)
)

View File

@ -92,11 +92,11 @@ AnyOfPig <- R6::R6Class(
#' @description
#' Serialize AnyOfPig to JSON string.
#'
#' @return JSON string reprenation of the AnyOfPig.
#' @return JSON string representation of the AnyOfPig.
#' @export
toJSONString = function() {
if (!is.null(self$actual_instance)) {
self$actual_instance$toJSONString()
as.character(jsonlite::minify((self$actual_instance$toJSONString())))
} else {
NULL
}
@ -106,7 +106,7 @@ AnyOfPig <- R6::R6Class(
#' @description
#' Serialize AnyOfPig to JSON.
#'
#' @return JSON reprenation of the AnyOfPig.
#' @return JSON representation of the AnyOfPig.
#' @export
toJSON = function() {
if (!is.null(self$actual_instance)) {
@ -134,6 +134,22 @@ AnyOfPig <- R6::R6Class(
# no error thrown, restore old values
self$actual_instance <- actual_instance_bak
self$actual_type <- actual_type_bak
},
#' Returns the string representation of the instance.
#'
#' @description
#' Returns the string representation of the instance.
#'
#' @return The string representation of the instance.
#' @export
toString = function() {
jsoncontent <- c(
sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) {NULL} else {self$actual_instance$toJSONString()}),
sprintf('"actual_type": "%s"', self$actual_type),
sprintf('"any_of": "%s"', paste( unlist(self$any_of), collapse=', '))
)
jsoncontent <- paste(jsoncontent, collapse = ",")
as.character(jsonlite::prettify(paste('{', jsoncontent, '}', sep = "")))
}
)
)

View File

@ -108,11 +108,11 @@ Pig <- R6::R6Class(
#' @description
#' Serialize Pig to JSON string.
#'
#' @return JSON string reprenation of the Pig.
#' @return JSON string representation of the Pig.
#' @export
toJSONString = function() {
if (!is.null(self$actual_instance)) {
self$actual_instance$toJSONString()
as.character(jsonlite::minify(self$actual_instance$toJSONString()))
} else {
NULL
}
@ -122,7 +122,7 @@ Pig <- R6::R6Class(
#' @description
#' Serialize Pig to JSON.
#'
#' @return JSON reprenation of the Pig.
#' @return JSON representation of the Pig.
#' @export
toJSON = function() {
if (!is.null(self$actual_instance)) {
@ -150,6 +150,22 @@ Pig <- R6::R6Class(
# no error thrown, restore old values
self$actual_instance <- actual_instance_bak
self$actual_type <- actual_type_bak
},
#' Returns the string representation of the instance.
#'
#' @description
#' Returns the string representation of the instance.
#'
#' @return The string representation of the instance.
#' @export
toString = function() {
jsoncontent <- c(
sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) {NULL} else {self$actual_instance$toJSONString()}),
sprintf('"actual_type": "%s"', self$actual_type),
sprintf('"one_of": "%s"', paste( unlist(self$one_of), collapse=', '))
)
jsoncontent <- paste(jsoncontent, collapse = ",")
as.character(jsonlite::prettify(paste('{', jsoncontent, '}', sep = "")))
}
)
)

View File

@ -237,6 +237,9 @@ test_that("Tests oneOf", {
nested_oneof$size <- 15
expect_equal(nested_oneof$toJSONString(), '{"size":15,"nested_pig":{"className":"BasquePig","color":"red"}}')
# test toString
expect_equal(as.character(jsonlite::minify(pig$toString())), "{\"actual_instance\":{\"className\":\"BasquePig\",\"color\":\"red\"},\"actual_type\":\"BasquePig\",\"one_of\":\"BasquePig, DanishPig\"}")
expect_equal(as.character(jsonlite::minify(Pig$new()$toString())), "{\"one_of\":\"BasquePig, DanishPig\"}")
})
test_that("Tests anyOf", {