-#TODO: Some methods do the same thing like collecion.index and collection.list.
-# Make one implementation of the method and make other reference to it.
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
getAPIDocument <- function(){
url <- "https://4xphq.arvadosapi.com/discovery/v1/apis/arvados/v1/rest"
#' @export
generateAPI <- function()
{
- JSONDocument <- getAPIDocument()
-
- generateArvadosClasses(JSONDocument)
- generateArvadosAPIClass(JSONDocument)
+ #TODO: Consider passing discovery document URL as parameter.
+ #TODO: Consider passing location where to create new files.
+ discoveryDocument <- getAPIDocument()
+
+ methodResources <- discoveryDocument$resources
+ resourceNames <- names(methodResources)
+
+ methodDoc <- genMethodsDoc(methodResources, resourceNames)
+ classDoc <- genAPIClassDoc(methodResources, resourceNames)
+ arvadosAPIHeader <- genAPIClassHeader()
+ arvadosProjectMethods <- genProjectMethods()
+ arvadosClassMethods <- genClassContent(methodResources, resourceNames)
+ arvadosAPIFooter <- genAPIClassFooter()
+
+ arvadosClass <- c(methodDoc,
+ classDoc,
+ arvadosAPIHeader,
+ arvadosProjectMethods,
+ arvadosClassMethods,
+ arvadosAPIFooter)
+
+ fileConn <- file("./R/Arvados.R", "w")
+ writeLines(unlist(arvadosClass), fileConn)
+ close(fileConn)
+ NULL
}
-generateArvadosAPIClass <- function(discoveryDocument)
+genAPIClassHeader <- function()
{
- classMetaData <- discoveryDocument$schemas
- functionResources <- discoveryDocument$resources
- resourceNames <- names(functionResources)
+ c("Arvados <- R6::R6Class(",
+ "",
+ "\t\"Arvados\",",
+ "",
+ "\tpublic = list(",
+ "",
+ "\t\tinitialize = function(authToken = NULL, hostName = NULL, numRetries = 0)",
+ "\t\t{",
+ "\t\t\tif(!is.null(hostName))",
+ "\t\t\t\tSys.setenv(ARVADOS_API_HOST = hostName)",
+ "",
+ "\t\t\tif(!is.null(authToken))",
+ "\t\t\t\tSys.setenv(ARVADOS_API_TOKEN = authToken)",
+ "",
+ "\t\t\thostName <- Sys.getenv(\"ARVADOS_API_HOST\")",
+ "\t\t\ttoken <- Sys.getenv(\"ARVADOS_API_TOKEN\")",
+ "",
+ "\t\t\tif(hostName == \"\" | token == \"\")",
+ "\t\t\t\tstop(paste(\"Please provide host name and authentification token\",",
+ "\t\t\t\t\t\t \"or set ARVADOS_API_HOST and ARVADOS_API_TOKEN\",",
+ "\t\t\t\t\t\t \"environment variables.\"))",
+ "",
+ "\t\t\tprivate$token <- token",
+ "\t\t\tprivate$host <- paste0(\"https://\", hostName, \"/arvados/v1/\")",
+ "\t\t\tprivate$numRetries <- numRetries",
+ "\t\t\tprivate$REST <- RESTService$new(token, hostName,",
+ "\t\t\t HttpRequest$new(), HttpParser$new(),",
+ "\t\t\t numRetries)",
+ "",
+ "\t\t},\n")
+}
- arvadosAPIHeader <- generateAPIClassHeader()
- arvadosAPIFooter <- generateAPIClassFooter()
+genProjectMethods <- function()
+{
+ c("\t\tprojects.get = function(uuid)",
+ "\t\t{",
+ "\t\t\tself$groups.get(uuid)",
+ "\t\t},",
+ "",
+ "\t\tprojects.create = function(group, ensure_unique_name = \"false\")",
+ "\t\t{",
+ "\t\t\tgroup <- c(\"group_class\" = \"project\", group)",
+ "\t\t\tself$groups.create(group, ensure_unique_name)",
+ "\t\t},",
+ "",
+ "\t\tprojects.update = function(group, uuid)",
+ "\t\t{",
+ "\t\t\tgroup <- c(\"group_class\" = \"project\", group)",
+ "\t\t\tself$groups.update(group, uuid)",
+ "\t\t},",
+ "",
+ "\t\tprojects.list = function(filters = NULL, where = NULL,",
+ "\t\t\torder = NULL, select = NULL, distinct = NULL,",
+ "\t\t\tlimit = \"100\", offset = \"0\", count = \"exact\",",
+ "\t\t\tinclude_trash = NULL)",
+ "\t\t{",
+ "\t\t\tfilters[[length(filters) + 1]] <- list(\"group_class\", \"=\", \"project\")",
+ "\t\t\tself$groups.list(filters, where, order, select, distinct,",
+ "\t\t\t limit, offset, count, include_trash)",
+ "\t\t},",
+ "",
+ "\t\tprojects.delete = function(uuid)",
+ "\t\t{",
+ "\t\t\tself$groups.delete(uuid)",
+ "\t\t},",
+ "")
+}
+genClassContent <- function(methodResources, resourceNames)
+{
arvadosMethods <- Map(function(resource, resourceName)
{
methodNames <- names(resource$methods)
functions <- Map(function(methodMetaData, methodName)
{
+ #NOTE: Index, show and destroy are aliases for the preferred names
+ # "list", "get" and "delete". Until they are removed from discovery
+ # document we will filter them here.
+ if(methodName %in% c("index", "show", "destroy"))
+ return(NULL)
+
methodName <- paste0(resourceName, ".", methodName)
- createFunction(methodName, methodMetaData, classMetaData)
+ createMethod(methodName, methodMetaData)
}, resource$methods, methodNames)
unlist(unname(functions))
- }, functionResources, resourceNames)
+ }, methodResources, resourceNames)
- arvadosClass <- c(arvadosAPIHeader, arvadosMethods, arvadosAPIFooter)
+ arvadosMethods
+}
- #TODO: Save to a file or load in memory?
- fileConn <- file("ArvadosAPI.R", "w")
- writeLines(unlist(arvadosClass), fileConn)
- close(fileConn)
- NULL
+genAPIClassFooter <- function()
+{
+ c("\t\tgetHostName = function() private$host,",
+ "\t\tgetToken = function() private$token,",
+ "\t\tsetRESTService = function(newREST) private$REST <- newREST,",
+ "\t\tgetRESTService = function() private$REST",
+ "\t),",
+ "",
+ "\tprivate = list(",
+ "",
+ "\t\ttoken = NULL,",
+ "\t\thost = NULL,",
+ "\t\tREST = NULL,",
+ "\t\tnumRetries = NULL",
+ "\t),",
+ "",
+ "\tcloneable = FALSE",
+ ")")
}
-getFunctionName <- function(functionMetaData)
+createMethod <- function(name, methodMetaData)
{
- stringr::str_replace(functionMetaData$id, "arvados.", "")
+ args <- getMethodArguments(methodMetaData)
+ signature <- getMethodSignature(name, args)
+ body <- getMethodBody(methodMetaData)
+
+ c(signature,
+ "\t\t{",
+ body,
+ "\t\t},\n")
}
-#TODO: Make sure that arguments that are required always go first.
-# This is not the case if request$required is false.
-getFunctionArguments <- function(functionMetaData)
+getMethodArguments <- function(methodMetaData)
{
- request <- functionMetaData$request
- requestArgument <- NULL
+ request <- methodMetaData$request
+ requestArgs <- NULL
if(!is.null(request))
+ {
+ resourceName <- tolower(request$properties[[1]][[1]])
+
if(request$required)
- requestArgument <- names(request$properties)[1]
+ requestArgs <- resourceName
else
- requestArgument <- paste(names(request$properties)[1], "=", "NULL")
+ requestArgs <- paste(resourceName, "=", "NULL")
+ }
- argNames <- names(functionMetaData$parameters)
+ argNames <- names(methodMetaData$parameters)
args <- sapply(argNames, function(argName)
{
- arg <- functionMetaData$parameters[[argName]]
+ arg <- methodMetaData$parameters[[argName]]
if(!arg$required)
{
argName
})
- paste0(c(requestArgument, args), collapse = ", ")
+ c(requestArgs, args)
}
-getFunctionBody <- function(functionMetaData, classMetaData)
+getMethodSignature <- function(methodName, args)
{
- url <- getRequestURL(functionMetaData)
- headers <- getRequestHeaders()
- requestQueryList <- getRequestQueryList(functionMetaData)
- requestQueryList <- getRequestQueryList(functionMetaData)
- requestBody <- getRequestBody(functionMetaData)
- request <- getRequest(functionMetaData)
- response <- getResponse(functionMetaData)
- returnObject <- getReturnObject(functionMetaData, classMetaData)
-
- body <- c(url, headers, requestQueryList, requestBody, request, response, returnObject)
- paste0("\t\t\t", body)
+ collapsedArgs <- paste0(args, collapse = ", ")
+ lineLengthLimit <- 40
+
+ if(nchar(collapsedArgs) > lineLengthLimit)
+ {
+ return(paste0("\t\t",
+ formatArgs(paste(methodName, "= function("),
+ "\t", args, ")", lineLengthLimit)))
+ }
+ else
+ {
+ return(paste0("\t\t", methodName, " = function(", collapsedArgs, ")"))
+ }
}
-getRequestBody <- function(functionMetaData)
+getMethodBody <- function(methodMetaData)
{
- request <- functionMetaData$request
+ url <- getRequestURL(methodMetaData)
+ headers <- getRequestHeaders()
+ requestQueryList <- getRequestQueryList(methodMetaData)
+ requestBody <- getRequestBody(methodMetaData)
+ request <- getRequest(methodMetaData)
+ response <- getResponse(methodMetaData)
+ errorCheck <- getErrorCheckingCode()
+ returnStatement <- getReturnObject()
+
+ body <- c(url,
+ headers,
+ requestQueryList, "",
+ requestBody, "",
+ request, response, "",
+ errorCheck, "",
+ returnStatement)
- if(is.null(request) || !request$required)
- return("body <- NULL")
+ paste0("\t\t\t", body)
+}
- requestParameterName <- names(request$properties)[1]
- paste0("body <- ", requestParameterName, "$toJSON()")
+getRequestURL <- function(methodMetaData)
+{
+ endPoint <- methodMetaData$path
+ endPoint <- stringr::str_replace_all(endPoint, "\\{", "${")
+ url <- c(paste0("endPoint <- stringr::str_interp(\"", endPoint, "\")"),
+ paste0("url <- paste0(private$host, endPoint)"))
+ url
}
getRequestHeaders <- function()
{
- paste0("headers <- list(Authorization = paste(\"OAuth2\", private$token),",
- "\"Content-Type\" = \"application/json\")")
+ c("headers <- list(Authorization = paste(\"OAuth2\", private$token), ",
+ " \"Content-Type\" = \"application/json\")")
}
-getReturnObject <- function(functionMetaData, classMetaData)
+getRequestQueryList <- function(methodMetaData)
{
- returnClass <- functionMetaData$response[["$ref"]]
- classArguments <- getReturnClassArguments(returnClass, classMetaData)
+ queryArgs <- names(Filter(function(arg) arg$location == "query",
+ methodMetaData$parameters))
- c(paste0(returnClass, "$new(", classArguments, ")"))
+ if(length(queryArgs) == 0)
+ return("queryArgs <- NULL")
+
+ queryArgs <- sapply(queryArgs, function(arg) paste0(arg, " = ", arg))
+ collapsedArgs <- paste0(queryArgs, collapse = ", ")
+
+ lineLengthLimit <- 40
+
+ if(nchar(collapsedArgs) > lineLengthLimit)
+ return(formatArgs("queryArgs <- list(", "\t\t\t\t ", queryArgs, ")",
+ lineLengthLimit))
+ else
+ return(paste0("queryArgs <- list(", collapsedArgs, ")"))
}
-getReturnClassArguments <- function(className, classMetaData)
+getRequestBody <- function(methodMetaData)
{
- classArguments <- unique(names(classMetaData[[className]]$properties))
+ request <- methodMetaData$request
- arguments <- sapply(classArguments, function(arg)
- {
- paste0(arg, " = resource$", arg)
- })
+ if(is.null(request) || !request$required)
+ return("body <- NULL")
+
+ resourceName <- tolower(request$properties[[1]][[1]])
+
+ requestParameterName <- names(request$properties)[1]
- paste0(arguments, collapse = ", ")
+ c(paste0("if(length(", resourceName, ") > 0)"),
+ paste0("\tbody <- jsonlite::toJSON(list(", resourceName, " = ", resourceName, "), "),
+ "\t auto_unbox = TRUE)",
+ "else",
+ "\tbody <- NULL")
}
-getRequest <- function(functionMetaData)
+getRequest <- function(methodMetaData)
{
- method <- functionMetaData$httpMethod
- paste0("response <- private$http$exec(\"", method, "\", url, headers, body, queryArgs)")
+ method <- methodMetaData$httpMethod
+ c(paste0("response <- private$REST$http$exec(\"", method, "\", url, headers, body,"),
+ " queryArgs, private$numRetries)")
}
-getResponse <- function(functionMetaData)
+getResponse <- function(methodMetaData)
{
- "resource <- private$httpParser$parseJSONResponse(response)"
+ "resource <- private$REST$httpParser$parseJSONResponse(response)"
}
-getRequestURL <- function(functionMetaData)
+getErrorCheckingCode <- function()
{
- endPoint <- functionMetaData$path
- endPoint <- stringr::str_replace_all(endPoint, "\\{", "${")
- url <- c(paste0("endPoint <- stringr::str_interp(\"", endPoint, "\")"),
- paste0("url <- paste0(private$host, endPoint)"))
- url
+ c("if(!is.null(resource$errors))",
+ "\tstop(resource$errors)")
}
-getRequestQueryList <- function(functionMetaData)
+getReturnObject <- function()
{
- argNames <- names(functionMetaData$parameters)
+ "resource"
+}
- if(length(argNames) == 0)
- return("queryArgs <- NULL")
+#NOTE: Arvados class documentation:
- queryListContent <- sapply(argNames, function(arg) paste0(arg, " = ", arg))
+genMethodsDoc <- function(methodResources, resourceNames)
+{
+ methodsDoc <- unlist(unname(Map(function(resource, resourceName)
+ {
+ methodNames <- names(resource$methods)
- paste0("queryArgs <- list(", paste0(queryListContent, collapse = ', ') , ")")
-}
+ methodDoc <- Map(function(methodMetaData, methodName)
+ {
+ #NOTE: Index, show and destroy are aliases for the preferred names
+ # "list", "get" and "delete". Until they are removed from discovery
+ # document we will filter them here.
+ if(methodName %in% c("index", "show", "destroy"))
+ return(NULL)
-createFunction <- function(functionName, functionMetaData, classMetaData)
-{
- args <- getFunctionArguments(functionMetaData)
- aditionalArgs <-
- body <- getFunctionBody(functionMetaData, classMetaData)
+ methodName <- paste0(resourceName, ".", methodName)
+ getMethodDoc(methodName, methodMetaData)
- functionString <- c(paste0("\t\t", functionName, " = function(", args, ")"),
- "\t\t{",
- body,
- "\t\t},\n")
+ }, resource$methods, methodNames)
- functionString
+ unlist(unname(methodDoc))
+
+ }, methodResources, resourceNames)))
+
+ projectDoc <- genProjectMethodsDoc()
+
+ c(methodsDoc, projectDoc)
}
-generateAPIClassHeader <- function()
+genAPIClassDoc <- function(methodResources, resourceNames)
{
- c("#' @export",
- "ArvadosAPI <- R6::R6Class(",
- "",
- "\t\"ArvadosAPI\",",
- "",
- "\tpublic = list(",
- "",
- "\t\tinitialize = function(authToken = NULL, hostName = NULL, numRetries = 0)",
- "\t\t{",
- "\t\t\tif(!is.null(hostName))",
- "\t\t\t\tSys.setenv(ARVADOS_API_HOST = hostName)",
+ c("#' Arvados",
+ "#'",
+ "#' Arvados class gives users ability to access Arvados REST API.",
+ "#'" ,
+ "#' @section Usage:",
+ "#' \\preformatted{arv = Arvados$new(authToken = NULL, hostName = NULL, numRetries = 0)}",
+ "#'",
+ "#' @section Arguments:",
+ "#' \\describe{",
+ "#' \t\\item{authToken}{Authentification token. If not specified ARVADOS_API_TOKEN environment variable will be used.}",
+ "#' \t\\item{hostName}{Host name. If not specified ARVADOS_API_HOST environment variable will be used.}",
+ "#' \t\\item{numRetries}{Number which specifies how many times to retry failed service requests.}",
+ "#' }",
+ "#'",
+ "#' @section Methods:",
+ "#' \\describe{",
+ getAPIClassMethodList(methodResources, resourceNames),
+ "#' }",
+ "#'",
+ "#' @name Arvados",
+ "#' @examples",
+ "#' \\dontrun{",
+ "#' arv <- Arvados$new(\"your Arvados token\", \"example.arvadosapi.com\")",
+ "#'",
+ "#' collection <- arv$collections.get(\"uuid\")",
+ "#'",
+ "#' collectionList <- arv$collections.list(list(list(\"name\", \"like\", \"Test%\")))",
+ "#' collectionList <- listAll(arv$collections.list, list(list(\"name\", \"like\", \"Test%\")))",
+ "#'",
+ "#' deletedCollection <- arv$collections.delete(\"uuid\")",
+ "#'",
+ "#' updatedCollection <- arv$collections.update(list(name = \"New name\", description = \"New description\"),",
+ "#' \"uuid\")",
+ "#'",
+ "#' createdCollection <- arv$collections.create(list(name = \"Example\",",
+ "#' description = \"This is a test collection\"))",
+ "#' }",
+ "NULL",
"",
- "\t\t\tif(!is.null(authToken))",
- "\t\t\t\tSys.setenv(ARVADOS_API_TOKEN = authToken)",
- "",
- "\t\t\tprivate$rawHost <- Sys.getenv(\"ARVADOS_API_HOST\")",
- "\t\t\tprivate$host <- paste0(\"https://\", private$rawHost, \"/arvados/v1/\")",
- "\t\t\tprivate$token <- Sys.getenv(\"ARVADOS_API_TOKEN\")",
- "\t\t\tprivate$numRetries <- numRetries",
- "\t\t\tprivate$http <- ArvadosR:::HttpRequest$new()",
- "\t\t\tprivate$httpParser <- ArvadosR:::HttpParser$new()",
- "",
- "\t\t\tif(private$rawHost == \"\" | private$token == \"\")",
- "\t\t\t\tstop(paste(\"Please provide host name and authentification token\",",
- "\t\t\t\t\t\t \"or set ARVADOS_API_HOST and ARVADOS_API_TOKEN\",",
- "\t\t\t\t\t\t \"environment variables.\"))",
- "\t\t},\n")
+ "#' @export")
}
-generateAPIClassFooter <- function()
+getAPIClassMethodList <- function(methodResources, resourceNames)
{
- c("\t\tgetHostName = function() private$host,",
- "\t\tgetToken = function() private$token",
- "\t),",
- "",
- "\tprivate = list(",
- "",
- "\t\ttoken = NULL,",
- "\t\trawHost = NULL,",
- "\t\thost = NULL,",
- "\t\thttp = NULL,",
- "\t\thttpParser = NULL,",
- "\t\tnumRetries = NULL",
- "\t),",
- "",
- "\tcloneable = FALSE",
- ")")
+ methodList <- unlist(unname(Map(function(resource, resourceName)
+ {
+ methodNames <- names(resource$methods)
+ paste0(resourceName,
+ ".",
+ methodNames[!(methodNames %in% c("index", "show", "destroy"))])
+
+ }, methodResources, resourceNames)))
+
+ hardcodedMethods <- c("projects.create", "projects.get",
+ "projects.list", "projects.update", "projects.delete")
+ paste0("#' \t\\item{}{\\code{\\link{", sort(c(methodList, hardcodedMethods)), "}}}")
}
-generateArvadosClasses <- function(resources)
+getMethodDoc <- function(methodName, methodMetaData)
{
- classes <- sapply(resources$schemas, function(classSchema)
- {
- getArvadosClass(classSchema)
+ name <- paste("#' @name", methodName)
+ usage <- getMethodUsage(methodName, methodMetaData)
+ description <- paste("#'", methodName, "is a method defined in Arvados class.")
+ params <- getMethodDescription(methodMetaData)
+ returnValue <- paste("#' @return", methodMetaData$response[["$ref"]], "object.")
+
+ c(paste("#'", methodName),
+ "#' ",
+ description,
+ "#' ",
+ usage,
+ params,
+ returnValue,
+ name,
+ "NULL",
+ "")
+}
- }, USE.NAMES = TRUE)
+getMethodUsage <- function(methodName, methodMetaData)
+{
+ lineLengthLimit <- 40
+ args <- getMethodArguments(methodMetaData)
+ c(formatArgs(paste0("#' @usage arv$", methodName,
+ "("), "#' \t", args, ")", lineLengthLimit))
+}
- unlist(unname(classes))
+getMethodDescription <- function(methodMetaData)
+{
+ request <- methodMetaData$request
+ requestDoc <- NULL
- fileConn <- file("ArvadosClasses.R", "w")
- writeLines(unlist(classes), fileConn)
- close(fileConn)
- NULL
+ if(!is.null(request))
+ {
+ requestDoc <- unname(unlist(sapply(request$properties, function(prop)
+ {
+ className <- sapply(prop, function(ref) ref)
+ objectName <- paste0(tolower(substr(className, 1, 1)),
+ substr(className, 2, nchar(className)))
+ paste("#' @param", objectName, className, "object.")
+ })))
+ }
+
+ argNames <- names(methodMetaData$parameters)
+
+ argsDoc <- unname(unlist(sapply(argNames, function(argName)
+ {
+ arg <- methodMetaData$parameters[[argName]]
+ argDescription <- arg$description
+ paste("#' @param", argName, argDescription)
+ })))
+
+ c(requestDoc, argsDoc)
+}
+
+genProjectMethodsDoc <- function()
+{
+ #TODO: Manually update this documentation to reflect changes in discovery document.
+ c("#' project.get",
+ "#' ",
+ "#' projects.get is equivalent to groups.get method.",
+ "#' ",
+ "#' @usage arv$projects.get(uuid)",
+ "#' @param uuid The UUID of the Group in question.",
+ "#' @return Group object.",
+ "#' @name projects.get",
+ "NULL",
+ "",
+ "#' project.create",
+ "#' ",
+ "#' projects.create wrapps groups.create method by setting group_class attribute to \"project\".",
+ "#' ",
+ "#' @usage arv$projects.create(group, ensure_unique_name = \"false\")",
+ "#' @param group Group object.",
+ "#' @param ensure_unique_name Adjust name to ensure uniqueness instead of returning an error on (owner_uuid, name) collision.",
+ "#' @return Group object.",
+ "#' @name projects.create",
+ "NULL",
+ "",
+ "#' project.update",
+ "#' ",
+ "#' projects.update wrapps groups.update method by setting group_class attribute to \"project\".",
+ "#' ",
+ "#' @usage arv$projects.update(group, uuid)",
+ "#' @param group Group object.",
+ "#' @param uuid The UUID of the Group in question.",
+ "#' @return Group object.",
+ "#' @name projects.update",
+ "NULL",
+ "",
+ "#' project.delete",
+ "#' ",
+ "#' projects.delete is equivalent to groups.delete method.",
+ "#' ",
+ "#' @usage arv$project.delete(uuid)",
+ "#' @param uuid The UUID of the Group in question.",
+ "#' @return Group object.",
+ "#' @name projects.delete",
+ "NULL",
+ "",
+ "#' project.list",
+ "#' ",
+ "#' projects.list wrapps groups.list method by setting group_class attribute to \"project\".",
+ "#' ",
+ "#' @usage arv$projects.list(filters = NULL,",
+ "#' where = NULL, order = NULL, distinct = NULL,",
+ "#' limit = \"100\", offset = \"0\", count = \"exact\",",
+ "#' include_trash = NULL, uuid = NULL, recursive = NULL)",
+ "#' @param filters ",
+ "#' @param where ",
+ "#' @param order ",
+ "#' @param distinct ",
+ "#' @param limit ",
+ "#' @param offset ",
+ "#' @param count ",
+ "#' @param include_trash Include items whose is_trashed attribute is true.",
+ "#' @param uuid ",
+ "#' @param recursive Include contents from child groups recursively.",
+ "#' @return Group object.",
+ "#' @name projects.list",
+ "NULL",
+ "")
}
-getArvadosClass <- function(classSchema)
+#NOTE: Utility functions:
+
+# This function is used to split very long lines of code into smaller chunks.
+# This is usually the case when we pass a lot of named argumets to a function.
+formatArgs <- function(prependAtStart, prependToEachSplit,
+ args, appendAtEnd, lineLength)
{
- name <- classSchema$id
- fields <- unique(names(classSchema$properties))
- fieldsList <- paste0("c(", paste0("\"", fields, "\"", collapse = ", "), ")")
- constructorArgs <- paste0(fields, " = NULL", collapse = ", ")
-
- classString <- c(paste0(name, " <- R6::R6Class("),
- "",
- paste0("\t\"", name, "\","),
- "",
- "\tpublic = list(",
- paste0("\t\t", fields, " = NULL,"),
- "",
- paste0("\t\tinitialize = function(", constructorArgs, ") {"),
- paste0("\t\t\tself$", fields, " <- ", fields),
- "\t\t\t",
- paste0("\t\t\tprivate$classFields <- ", fieldsList),
- "\t\t},",
- "",
- "\t\ttoJSON = function() {",
- "\t\t\tfields <- sapply(private$classFields, function(field)",
- "\t\t\t{",
- "\t\t\t\tself[[field]]",
- "\t\t\t}, USE.NAMES = TRUE)",
- "\t\t\t",
- paste0("\t\t\tjsonlite::toJSON(list(\"", tolower(name), "\" = Filter(Negate(is.null), fields)), auto_unbox = TRUE)"),
- "\t\t}",
- "\t),",
- "",
- "\tprivate = list(",
- "\t\tclassFields = NULL",
- "\t),",
- "",
- "\tcloneable = FALSE",
- ")",
- "")
+ if(length(args) > 1)
+ {
+ args[1:(length(args) - 1)] <- paste0(args[1:(length(args) - 1)], ",")
+ }
+
+ args[1] <- paste0(prependAtStart, args[1])
+ args[length(args)] <- paste0(args[length(args)], appendAtEnd)
+
+ argsLength <- length(args)
+ argLines <- list()
+ index <- 1
+
+ while(index <= argsLength)
+ {
+ line <- args[index]
+ index <- index + 1
+
+ while(nchar(line) < lineLength && index <= argsLength)
+ {
+ line <- paste(line, args[index])
+ index <- index + 1
+ }
+
+ argLines <- c(argLines, line)
+ }
+
+ argLines <- unlist(argLines)
+ argLinesLen <- length(argLines)
+
+ if(argLinesLen > 1)
+ argLines[2:argLinesLen] <- paste0(prependToEachSplit, argLines[2:argLinesLen])
+
+ argLines
}