Skip to content

Commit

Permalink
feat: Added 'repeats' and 'extends' properties for Parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
beneboy committed Sep 2, 2019
1 parent 7c062d1 commit 398e658
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
8 changes: 8 additions & 0 deletions py/stencila/schema/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,14 +1504,18 @@ class Parameter(Variable):
"""A parameter that can be set and used in evaluated code."""

default: Optional["Node"] = None
extends: Optional[bool] = None
repeats: Optional[bool] = None
required: Optional[bool] = None

def __init__(
self,
name: str,
default: Optional["Node"] = None,
extends: Optional[bool] = None,
id: Optional[str] = None,
meta: Optional[Dict[str, Any]] = None,
repeats: Optional[bool] = None,
required: Optional[bool] = None,
schema: Optional["SchemaTypes"] = None
) -> None:
Expand All @@ -1523,6 +1527,10 @@ def __init__(
)
if default is not None:
self.default = default
if extends is not None:
self.extends = extends
if repeats is not None:
self.repeats = repeats
if required is not None:
self.required = required

Expand Down
6 changes: 6 additions & 0 deletions r/R/types.R
Original file line number Diff line number Diff line change
Expand Up @@ -1778,17 +1778,21 @@ Variable <- function(
#' @name Parameter
#' @param name The name the parameter is referred to in code. \bold{Required}.
#' @param default The default value of the parameter.
#' @param extends Indicates that this parameter is variadic and can accept multiple named arguments.
#' @param id The identifier for this item.
#' @param meta Metadata associated with this item.
#' @param repeats Indicates that this parameter is variadic and can accept multiple arguments.
#' @param required Is this parameter required, if not it should have a default or default is assumed to be null.
#' @param schema The schema that the value of the parameter will be validated against.
#' @seealso \code{\link{Variable}}
#' @export
Parameter <- function(
name,
default,
extends,
id,
meta,
repeats,
required,
schema
){
Expand All @@ -1800,6 +1804,8 @@ Parameter <- function(
)
self$type <- as_scalar("Parameter")
self[["default"]] <- check_property("Parameter", "default", FALSE, missing(default), "Node", default)
self[["extends"]] <- check_property("Parameter", "extends", FALSE, missing(extends), "logical", extends)
self[["repeats"]] <- check_property("Parameter", "repeats", FALSE, missing(repeats), "logical", repeats)
self[["required"]] <- check_property("Parameter", "required", FALSE, missing(required), "logical", required)
class(self) <- c("list", "Entity")
self
Expand Down
8 changes: 8 additions & 0 deletions schema/Parameter.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ properties:
'@id': stencila:required
description: Is this parameter required, if not it should have a default or default is assumed to be null.
type: boolean
repeats:
'@id': stencila:repeats
description: Indicates that this parameter is variadic and can accept multiple arguments.
type: boolean
extends:
'@id': stencila:extends
description: Indicates that this parameter is variadic and can accept multiple named arguments.
type: boolean
17 changes: 13 additions & 4 deletions ts/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ function parseItem(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
item: any,
parameters: Parameter[],
code: (CodeChunkExecution | CodeExpression)[]
code: (CodeChunkExecution | CodeExpression)[],
functionDepth: number = 0
): void {
if (isA('Entity', item) || item instanceof Object) {
if (isA('Parameter', item)) {
if (isA('Function', item)) {
++functionDepth
} else if (isA('Parameter', item) && functionDepth === 0) {
parameters.push(item)
} else if (
(isA('CodeChunk', item) || isA('CodeExpression', item)) &&
Expand All @@ -139,10 +142,14 @@ function parseItem(
}

Object.entries(item).forEach(([, i]) => {
parseItem(i, parameters, code)
parseItem(i, parameters, code, functionDepth)
})

if (isA('Function', item)) {
--functionDepth
}
} else if (Array.isArray(item)) {
item.forEach(i => parseItem(i, parameters, code))
item.forEach(i => parseItem(i, parameters, code, functionDepth))
}
}

Expand Down Expand Up @@ -234,6 +241,8 @@ function parseFunctionDeclaration(
if (fn.params !== undefined) {
fn.params.forEach(p => {
if (p.type === 'Identifier') parameters.push(parameter(p.name))
else if (p.type === 'RestElement' && p.argument.type === 'Identifier')
parameters.push(parameter(p.argument.name, { repeats: true }))
})
}

Expand Down

0 comments on commit 398e658

Please sign in to comment.