-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial implementation of R package with encoders
- Loading branch information
Showing
16 changed files
with
433 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
linters: with_defaults(object_name_linter(c("snake_case", "UpperCamelCase"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Package: stencilaschema | ||
Type: Package | ||
Title: Stencila schemas, protocols and other specs | ||
Description: Schemas, protocols and other specifications used in Stencila. | ||
Authors@R: c(person("Nokome", "Bentley", email = "nokome@stenci.la", role = c("aut", "cre"))) | ||
URL: https://github.com/stencila/schema#readme | ||
BugReports: https://github.com/stencila/schema/issues | ||
License: Apache-2.0 | ||
Version: 0.0.1 | ||
Imports: | ||
jsonlite, | ||
R6 | ||
Suggests: | ||
bench, | ||
covr, | ||
devtools, | ||
lintr, | ||
roxygen2, | ||
testthat | ||
RoxygenNote: 6.0.1.9000 | ||
Collate: | ||
'Encoder.R' | ||
'JsonEncoder.R' | ||
'JsonGzipEncoder.R' | ||
'jsonRpc.R' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(JsonEncoder) | ||
export(JsonGzipEncoder) | ||
export(Request) | ||
export(Response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Encoder <- R6::R6Class("Encoder", | ||
public = list( | ||
# nocov start | ||
name = function() { | ||
stop("Not implemented") | ||
}, | ||
|
||
decode = function() { | ||
stop("Not implemented") | ||
}, | ||
|
||
encode = function() { | ||
stop("Not implemented") | ||
} | ||
# nocov end | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' @include Encoder.R | ||
|
||
#' @export | ||
JsonEncoder <- R6::R6Class("JsonEncoder", | ||
inherit = Encoder, | ||
|
||
public = list( | ||
name = function() { | ||
"json" | ||
}, | ||
|
||
encode = function(instance) { | ||
members <- list() | ||
for (name in ls(instance, sorted = FALSE)) { | ||
if (!is.function(instance[[name]])) members[[name]] <- instance[[name]] | ||
} | ||
toString(jsonlite::toJSON( | ||
members, | ||
null = "null", | ||
na = "null", | ||
dataframe = "columns", | ||
digits = NA, | ||
auto_unbox = TRUE, | ||
force = TRUE | ||
)) | ||
}, | ||
|
||
decode = function(message, cls) { | ||
instance <- cls$new() | ||
members <- jsonlite::fromJSON(message, simplifyDataFrame = FALSE) | ||
for (name in ls(members)) { | ||
instance[[name]] <- members[[name]] | ||
} | ||
instance | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#' @include JsonEncoder.R | ||
|
||
#' @export | ||
JsonGzipEncoder <- R6::R6Class("JsonGzipEncoder", | ||
inherit = JsonEncoder, | ||
|
||
public = list( | ||
name = function() { | ||
"json+gzip" | ||
}, | ||
|
||
encode = function(instance) { | ||
memCompress(super$encode(instance)) | ||
}, | ||
|
||
decode = function(message, cls) { | ||
super$decode(memDecompress(message, "gzip", asChar = TRUE), cls) | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#' @export | ||
Request <- R6::R6Class("Request", | ||
public = list( | ||
jsonrpc = NULL, | ||
id = NULL, | ||
method = NULL, | ||
params = NULL, | ||
|
||
initialize = function(id=NULL, method=NULL, params=NULL) { | ||
self$jsonrpc <- "2.0" | ||
self$id <- id | ||
self$method <- method | ||
self$params <- params | ||
} | ||
) | ||
) | ||
|
||
#' @export | ||
Response <- R6::R6Class("Response", | ||
public = list( | ||
jsonrpc = NULL, | ||
id = NULL, | ||
result = NULL, | ||
error = NULL, | ||
|
||
initialize = function(id=NULL, result=NULL, error=NULL) { | ||
self$jsonrpc <- "2.0" | ||
self$id <- id | ||
self$result <- result | ||
self$error <- error | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
library(bench) | ||
library(stencilaschema) | ||
|
||
source(file.path("..", "fixtures", "requests.R")) | ||
|
||
roundtrip <- function(encoder, request) { | ||
message <- encoder$encode(request) | ||
encoder$decode(message, Request) | ||
} | ||
|
||
results <- bench::press( | ||
encoder = c("JsonEncoder", "JsonGzipEncoder"), | ||
request = names(requests), { | ||
encoder_instance <- get(encoder)$new() | ||
request_instance <- requests[[request]] | ||
bench::mark( | ||
roundtrip(encoder_instance, request_instance) | ||
) | ||
} | ||
) | ||
|
||
results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Example requests | ||
requests <- list( | ||
|
||
# Import a 100k Markdown document | ||
import_text = Request$new( | ||
id = 1, | ||
method = "import", | ||
params = list( | ||
thing = sample(LETTERS, 100000, TRUE), | ||
format = "text/markdown" | ||
) | ||
), | ||
|
||
# Execute a simple code cell | ||
execute_cell_simple = Request$new( | ||
id = 2, | ||
method = "execute", | ||
params = list( | ||
thing = list( | ||
type = "Cell", | ||
programmingLanguage = "R", | ||
text = "x + y", | ||
inputs = list( | ||
list( | ||
type = "Var", | ||
name = "x", | ||
value = 6 | ||
), | ||
list( | ||
type = "Var", | ||
name = "y", | ||
value = 7 | ||
) | ||
) | ||
) | ||
) | ||
), | ||
|
||
# Execute a call with a data table arg | ||
execute_call = Request$new( | ||
id = 3, | ||
method = "execute", | ||
params = list( | ||
thing = list( | ||
type = "Call", | ||
func = "http://example.org/functions/filter", | ||
args = list( | ||
list( | ||
type = "Datatable", | ||
columns = list( | ||
list( | ||
type = "Column", | ||
name = "col1", | ||
data = list( | ||
type = "Array", | ||
values = runif(10000) | ||
) | ||
), | ||
list( | ||
type = "Column", | ||
name = "col2", | ||
data = list( | ||
type = "Array", | ||
values = sample(LETTERS, 10000, TRUE) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
responses <- list( | ||
cell = Response$new( | ||
id = 1, | ||
result = list( | ||
type = "Cell" | ||
) | ||
), | ||
error = Response$new( | ||
id = 1, | ||
error = list( | ||
message = "An error happened" | ||
) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
library(testthat) | ||
library(stencilaschema) | ||
|
||
test_check("stencilaschema") |
Oops, something went wrong.