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

r.model.mustache Maven / Gradle / Ivy

{{#models}}
{{#model}}
{{>partial_header}}
#' @docType class
#' @title {{classname}}
#' @description {{classname}} Class
#' @format An \code{R6Class} generator object
{{#vars}}
#' @field {{baseName}} {{title}} {{#isContainer}}{{#isListContainer}}list( {{/isListContainer}}{{#isMapContainer}}named list( {{/isMapContainer}}{{/isContainer}}{{^isPrimitiveType}}\link{{=<% %>=}}{<%/isPrimitiveType%><%={{ }}=%>{{#isContainer}}{{#items}}{{dataType}}{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}{{=<% %>=}}<%^isPrimitiveType%>}<%={{ }}=%>{{/isPrimitiveType}}{{#isContainer}}{{#isListContainer}} ){{/isListContainer}}{{#isMapContainer}} ){{/isMapContainer}}{{/isContainer}} {{^required}}[optional]{{/required}}
#'
{{/vars}}
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
{{classname}} <- R6::R6Class(
  '{{classname}}',
  public = list(
    {{#vars}}
    `{{{baseName}}}` = NULL,
    {{/vars}}
    initialize = function({{#requiredVars}}`{{baseName}}`{{#hasMore}}, {{/hasMore}}{{/requiredVars}}{{#optionalVars}}{{#-first}}{{#requiredVars.0}}, {{/requiredVars.0}}{{/-first}}`{{baseName}}`={{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}NULL{{/defaultValue}}{{^-last}}, {{/-last}}{{/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}}
       {{#isListContainer}}
       {{#isPrimitiveType}}
          self$`{{baseName}}`
       {{/isPrimitiveType}}
       {{^isPrimitiveType}}
          lapply(self$`{{baseName}}`, function(x) x$toJSON())
        {{/isPrimitiveType}}
       {{/isListContainer}}
       {{#isMapContainer}}
       {{#isPrimitiveType}}
          self$`{{baseName}}`
       {{/isPrimitiveType}}
       {{^isPrimitiveType}}
          lapply(self$`{{baseName}}`, function(x) x$toJSON())
       {{/isPrimitiveType}}
       {{/isMapContainer}}
       {{/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}}
    },
    toJSONString = function() {
      jsoncontent <- c(
      {{#vars}}
        if (!is.null(self$`{{baseName}}`)) {
        sprintf(
        '"{{baseName}}":
        {{#isContainer}}
        {{#isListContainer}}
        {{#isPrimitiveType}}
           {{#isNumeric}}[%d]{{/isNumeric}}{{^isNumeric}}[%s]{{/isNumeric}}
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}[%s]
        {{/isPrimitiveType}}
        {{/isListContainer}}
        {{#isMapContainer}}
        {{#isPrimitiveType}}
          {{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}"%s"{{/isNumeric}}
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}%s
        {{/isPrimitiveType}}
        {{/isMapContainer}}
        {{/isContainer}}
        {{^isContainer}}
        {{#isPrimitiveType}}
          {{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}"%s"{{/isNumeric}}
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}%s
        {{/isPrimitiveType}}
        {{/isContainer}}',
        {{#isContainer}}
        {{#isListContainer}}
        {{#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}}
        {{/isListContainer}}
        {{#isMapContainer}}
        {{#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}}
        {{/isMapContainer}}
        {{/isContainer}}
        {{^isContainer}}
        {{#isPrimitiveType}}
        self$`{{baseName}}`
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        jsonlite::toJSON(self$`{{baseName}}`$toJSON(), auto_unbox=TRUE, digits = NA)
        {{/isPrimitiveType}}
        {{/isContainer}}
        )}{{#hasMore}},{{/hasMore}}
      {{/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
    }
  )
)
{{/model}}
{{/models}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy