aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Isaac2024-10-08 01:41:44 +0100
committerArun Isaac2024-10-08 01:47:22 +0100
commit45c7dda6154ef64d73c70656e85e8d6b314ddb68 (patch)
tree954cb28797f052a85f77acb8eace2ce5fd260be0
parent037d37c68d16952ea769ac1e65f727a9df742dd3 (diff)
downloadravanan-45c7dda6154ef64d73c70656e85e8d6b314ddb68.tar.gz
ravanan-45c7dda6154ef64d73c70656e85e8d6b314ddb68.tar.lz
ravanan-45c7dda6154ef64d73c70656e85e8d6b314ddb68.zip
javascript: Do not serialize JSON if not interpolating string.
* ravanan/javascript.scm (evaluate-parameter-reference): Do not serialize JSON tree if not interpolating parameter reference. * tests/javascript.scm ("evaluate parameter reference to JSON object", "evaluate parameter reference to JSON object using node"): New tests. ("evaluate parameter reference with node", "evaluate parameter reference (without context)", "evaluate parameter reference with node (without context)"): Update tests.
-rw-r--r--ravanan/javascript.scm42
-rw-r--r--tests/javascript.scm27
2 files changed, 40 insertions, 29 deletions
diff --git a/ravanan/javascript.scm b/ravanan/javascript.scm
index a416d27..d57a02d 100644
--- a/ravanan/javascript.scm
+++ b/ravanan/javascript.scm
@@ -175,21 +175,27 @@ keys @code{\"inputs\"}, @code{\"self\"} and @code{\"runtime\"}.
;; token is a string literal.
token))
- (let ((evaluated-tokens (map evaluate-token
- (tokenize-parameter-references str))))
- (if context
- ;; Evaluate immediately.
- (string-join (map (lambda (token)
- (if (string? token)
- token
- (scm->json-string (canonicalize-json token))))
- evaluated-tokens)
- "")
- ;; Compile to a G-expression that interpolates parameter reference
- ;; string.
- #~(string-join (map (lambda (token)
- (if (string? token)
- token
- (scm->json-string (canonicalize-json token))))
- (list #$@evaluated-tokens))
- ""))))
+ (match (tokenize-parameter-references str)
+ ;; There is only one token. This is not a string interpolation. Do not
+ ;; serialize JSON.
+ ((only-token)
+ (evaluate-token only-token))
+ ;; This is a string interpolation. Evaluate tokens and serialize JSON.
+ (tokens
+ (let ((evaluated-tokens (map evaluate-token tokens)))
+ (if context
+ ;; Evaluate immediately.
+ (string-join (map (lambda (token)
+ (if (string? token)
+ token
+ (scm->json-string (canonicalize-json token))))
+ evaluated-tokens)
+ "")
+ ;; Compile to a G-expression that interpolates parameter reference
+ ;; string.
+ #~(string-join (map (lambda (token)
+ (if (string? token)
+ token
+ (scm->json-string (canonicalize-json token))))
+ (list #$@evaluated-tokens))
+ ""))))))
diff --git a/tests/javascript.scm b/tests/javascript.scm
index c766ac3..75936c0 100644
--- a/tests/javascript.scm
+++ b/tests/javascript.scm
@@ -40,6 +40,14 @@
(evaluate-parameter-reference "$(inputs.message['bar'][\"foo\"][2])"
'(("inputs" ("message" ("bar" ("foo" . #("a" "b" "c" "d"))))))))
+(test-equal "evaluate parameter reference to JSON object"
+ '(("class" . "File")
+ ("path" . "/foo/bar"))
+ (evaluate-parameter-reference "$(inputs.fasta)"
+ '(("inputs" ("fasta"
+ ("class" . "File")
+ ("path" . "/foo/bar"))))))
+
(test-equal "evaluate parameter reference with string interpolation"
"24foo12foobar"
(evaluate-parameter-reference "$(runtime.cores)foo$(inputs.threads)$(inputs.output_filename)"
@@ -58,10 +66,15 @@
("vector" . #(0 1 2 3))))))
(test-equal "evaluate parameter reference with node"
- "3"
+ 3
(evaluate-parameter-reference "$(inputs.n + 1)"
'(("inputs" ("n" . 2)))))
+(test-equal "evaluate parameter reference to JSON object using node"
+ '(json-ref inputs "fasta")
+ (gexp->sexp-rec
+ (evaluate-parameter-reference "$(inputs.fasta)")))
+
(test-equal "evaluate parameter reference with string interpolation using node"
"24foo24foobar"
(evaluate-parameter-reference "$(runtime.cores)foo$(inputs.threads*2)$(inputs.output_filename)"
@@ -80,11 +93,7 @@
("vector" . #(0 1 2 3))))))
(test-equal "evaluate parameter reference (without context)"
- '(string-join
- (map (lambda (token)
- (if (string? token) token (scm->json-string (canonicalize-json token))))
- (list (json-ref inputs "message" "bar" "foo" 2)))
- "")
+ '(json-ref inputs "message" "bar" "foo" 2)
(gexp->sexp-rec
(evaluate-parameter-reference "$(inputs.message['bar'][\"foo\"][2])")))
@@ -110,11 +119,7 @@
(evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)")))
(test-equal "evaluate parameter reference with node (without context)"
- '(string-join
- (map (lambda (token)
- (if (string? token) token (scm->json-string (canonicalize-json token))))
- (list (evaluate-javascript (*approximate*) "(inputs.n + 1)" "")))
- "")
+ '(evaluate-javascript (*approximate*) "(inputs.n + 1)" "")
(gexp->sexp-rec
(evaluate-parameter-reference "$(inputs.n + 1)")))