All Downloads are FREE. Search and download functionalities are using the official Maven repository.

r.model.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{#models}}
{{#model}}
{{>partial_header}}
#' @docType class
#' @title {{classname}}
#'
#' @description {{classname}} Class
#'
#' @format An \code{R6Class} generator object
#'
{{#vars}}
#' @field {{baseName}} {{title}} {{#isContainer}}{{#isArray}}list( {{/isArray}}{{#isMap}}named list( {{/isMap}}{{/isContainer}}{{^isPrimitiveType}}\link{{=<% %>=}}{<%/isPrimitiveType%><%={{ }}=%>{{#isContainer}}{{#items}}{{dataType}}{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}{{=<% %>=}}<%^isPrimitiveType%>}<%={{ }}=%>{{/isPrimitiveType}}{{#isContainer}}{{#isArray}} ){{/isArray}}{{#isMap}} ){{/isMap}}{{/isContainer}} {{^required}}[optional]{{/required}}
#'
{{/vars}}
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
{{#isEnum}}
{{>modelEnum}}
{{/isEnum}}
{{^isEnum}}
#' @export
{{classname}} <- R6::R6Class(
  '{{classname}}',
  public = list(
    {{#vars}}
    `{{{baseName}}}` = NULL,
    {{/vars}}
    initialize = function(
        {{#requiredVars}}`{{baseName}}`, {{/requiredVars}}{{#optionalVars}}`{{baseName}}`={{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}NULL{{/defaultValue}}, {{/optionalVars}}...
    ) {
      local.optional.var <- list(...)
      {{#requiredVars}}
      if (!missing(`{{baseName}}`)) {
        {{^isContainer}}
        {{#isInteger}}
        stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isInteger}}
        {{#isLong}}
        stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isLong}}
        {{#isFloat}}
        stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isFloat}}
        {{#isDouble}}
        stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isDouble}}
        {{#isString}}
        stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isString}}
        {{#isDate}}
        stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isDate}}
        {{#isDateTime}}
        stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isDateTime}}
        {{^isPrimitiveType}}
        stopifnot(R6::is.R6(`{{baseName}}`))
        {{/isPrimitiveType}}
        {{/isContainer}}
        {{#isContainer}}
        {{#isPrimitiveType}}
        stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
        sapply(`{{baseName}}`, function(x) stopifnot(is.character(x)))
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
        sapply(`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
        {{/isPrimitiveType}}
        {{/isContainer}}
        self$`{{baseName}}` <- `{{baseName}}`
      }
      {{/requiredVars}}
      {{#optionalVars}}
      if (!is.null(`{{baseName}}`)) {
        {{^isContainer}}
        {{#isInteger}}
        stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isInteger}}
        {{#isLong}}
        stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isLong}}
        {{#isFloat}}
        stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isFloat}}
        {{#isDouble}}
        stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isDouble}}
        {{#isString}}
        stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isString}}
        {{#isDate}}
        stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isDate}}
        {{#isDateTime}}
        stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
        {{/isDateTime}}
        {{^isPrimitiveType}}
        stopifnot(R6::is.R6(`{{baseName}}`))
        {{/isPrimitiveType}}
        {{/isContainer}}
        {{#isContainer}}
        {{#isPrimitiveType}}
        stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
        sapply(`{{baseName}}`, function(x) stopifnot(is.character(x)))
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
        sapply(`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
        {{/isPrimitiveType}}
        {{/isContainer}}
        self$`{{baseName}}` <- `{{baseName}}`
      }
      {{/optionalVars}}
    },
    toJSON = function() {
      {{classname}}Object <- list()
      {{#vars}}
      if (!is.null(self$`{{baseName}}`)) {
        {{classname}}Object[['{{baseName}}']] <-
       {{#isContainer}}
       {{#isArray}}
       {{#isPrimitiveType}}
          self$`{{baseName}}`
       {{/isPrimitiveType}}
       {{^isPrimitiveType}}
          lapply(self$`{{baseName}}`, function(x) x$toJSON())
        {{/isPrimitiveType}}
       {{/isArray}}
       {{#isMap}}
       {{#isPrimitiveType}}
          self$`{{baseName}}`
       {{/isPrimitiveType}}
       {{^isPrimitiveType}}
          lapply(self$`{{baseName}}`, function(x) x$toJSON())
       {{/isPrimitiveType}}
       {{/isMap}}
       {{/isContainer}}
       {{^isContainer}}
       {{#isPrimitiveType}}
          self$`{{baseName}}`
       {{/isPrimitiveType}}
       {{^isPrimitiveType}}
          self$`{{baseName}}`$toJSON()
       {{/isPrimitiveType}}
       {{/isContainer}}
      }
      {{/vars}}

      {{classname}}Object
    },
    fromJSON = function({{classname}}Json) {
      {{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
      {{#vars}}
      if (!is.null({{classname}}Object$`{{baseName}}`)) {
        {{#isContainer}}
        self$`{{baseName}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
        {{/isContainer}}
        {{^isContainer}}
        {{#isPrimitiveType}}
        self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        {{baseName}}Object <- {{dataType}}$new()
        {{baseName}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE, digits = NA))
        self$`{{baseName}}` <- {{baseName}}Object
        {{/isPrimitiveType}}
        {{/isContainer}}
      }
      {{/vars}}
      self
    },
    toJSONString = function() {
      jsoncontent <- c(
      {{#vars}}
        if (!is.null(self$`{{baseName}}`)) {
        sprintf(
        '"{{baseName}}":
        {{#isContainer}}
        {{#isArray}}
        {{#isPrimitiveType}}
           {{#isNumeric}}[%d]{{/isNumeric}}{{^isNumeric}}[%s]{{/isNumeric}}
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}[%s]
        {{/isPrimitiveType}}
        {{/isArray}}
        {{#isMap}}
        {{#isPrimitiveType}}
          {{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}"%s"{{/isNumeric}}
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}%s
        {{/isPrimitiveType}}
        {{/isMap}}
        {{/isContainer}}
        {{^isContainer}}
        {{#isPrimitiveType}}
          {{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}"%s"{{/isNumeric}}
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}%s
        {{/isPrimitiveType}}
        {{/isContainer}}',
        {{#isContainer}}
        {{#isArray}}
        {{#isPrimitiveType}}
        paste(unlist(lapply(self$`{{{baseName}}}`, function(x) paste0('"', x, '"'))), collapse=",")
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        paste(sapply(self$`{{{baseName}}}`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=TRUE, digits = NA)), collapse=",")
        {{/isPrimitiveType}}
        {{/isArray}}
        {{#isMap}}
        {{#isPrimitiveType}}
        jsonlite::toJSON(lapply(self$`{{{baseName}}}`, function(x){ x }), auto_unbox = TRUE, digits=NA)
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        jsonlite::toJSON(lapply(self$`{{{baseName}}}`, function(x){ x$toJSON() }), auto_unbox = TRUE, digits=NA)
        {{/isPrimitiveType}}
        {{/isMap}}
        {{/isContainer}}
        {{^isContainer}}
        {{#isPrimitiveType}}
        self$`{{baseName}}`
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        jsonlite::toJSON(self$`{{baseName}}`$toJSON(), auto_unbox=TRUE, digits = NA)
        {{/isPrimitiveType}}
        {{/isContainer}}
        )}{{^-last}},{{/-last}}
      {{/vars}}
      )
      jsoncontent <- paste(jsoncontent, collapse = ",")
      paste('{', jsoncontent, '}', sep = "")
    },
    fromJSONString = function({{classname}}Json) {
      {{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
      {{#vars}}
      {{! AAPI - added condition for handling container type of parameters, map and array}}
      {{#isContainer}}
      self$`{{baseName}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
      {{/isContainer}}
      {{^isContainer}}
      {{#isPrimitiveType}}
      self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
      {{/isPrimitiveType}}
      {{^isPrimitiveType}}
      self$`{{baseName}}` <- {{dataType}}$new()$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE, digits = NA))
      {{/isPrimitiveType}}
      {{/isContainer}}
      {{/vars}}
      self
    }
  )
)
{{/isEnum}}
{{/model}}
{{/models}}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy