-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathstring_enum_ref.R
More file actions
95 lines (85 loc) · 2.54 KB
/
string_enum_ref.R
File metadata and controls
95 lines (85 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#' @docType class
#' @title StringEnumRef
#' @description StringEnumRef Class
#' @format An \code{R6Class} generator object
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
StringEnumRef <- R6::R6Class(
"StringEnumRef",
public = list(
#' @description
#' Initialize a new StringEnumRef class.
#'
#' @param ... Optional arguments.
initialize = function(...) {
local.optional.var <- list(...)
val <- unlist(local.optional.var)
enumvec <- .parse_StringEnumRef()
if (length(val) == 0L) {
val = "DUMMY_ENUM"
} else {
stopifnot(length(val) == 1L)
}
if (!val %in% enumvec) {
if (!(val=="DUMMY_ENUM")) {
stop("Use one of the valid values: ",
paste0(enumvec, collapse = ", "))
}
}
private$value <- val
},
#' @description
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
toJSON = function() {
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
return(self$toSimpleType())
},
#' @description
#' Convert StringEnumRef to a base R type
#'
#' @return A base R type, e.g. a list or numeric/character array.
toSimpleType = function() {
return(private$value)
},
#' @description
#' Deserialize JSON string into an instance of StringEnumRef
#'
#' @param input_json the JSON input
#'
#' @return the instance of StringEnumRef
fromJSON = function(input_json) {
private$value <- jsonlite::fromJSON(input_json,
simplifyVector = FALSE)
self
},
#' @description
#' To JSON String
#'
#' @param ... Parameters passed to `jsonlite::toJSON`
#' @return StringEnumRef in JSON format
toJSONString = function(...) {
json <- jsonlite::toJSON(self$toSimpleType(), auto_unbox = TRUE, ...)
return(as.character(jsonlite::minify(json)))
},
#' @description
#' Deserialize JSON string into an instance of StringEnumRef
#'
#' @param input_json the JSON input
#'
#' @return the instance of StringEnumRef
fromJSONString = function(input_json) {
private$value <- jsonlite::fromJSON(input_json,
simplifyVector = FALSE)
self
}
),
private = list(
value = NULL
)
)
# add to utils.R
.parse_StringEnumRef <- function(vals) {
res <- gsub("^\\[|\\]$", "", "[success, failure, unclassified]")
unlist(strsplit(res, ", "))
}