Hi, Racket Discourse.
I am wondering whether there is an "obvious" way to include or exclude certain pieces of code based on some form of "check". In particular, I want to exclude and include certain sections in a Scribble document based on the presence or absence of certain values in the results of an API query.
Basically, certain events reported on in the results necessitate the addition of optional sections, which are perhaps absent again for other results, although the report itself is the same general document; think integrations being present or not, that kind of thing.
In essence, this isn't particularly difficult. Using something like a JSON object (stored in a file) to check the inclusion/exclusion cases can be done quite easily, a stupid example would be:
(require (for-syntax racket/match
syntax/parse
json))
(begin-for-syntax
(define (check-json key val filepath)
(match (with-input-from-file filepath
(lambda () (read-json)))
[(hash-table ((== key) (== val))) #t]
[_ #f])))
(define-syntax (with-inclusion-condition stx)
(syntax-parse stx
[(with-inclusion-condition #t
BODY ...)
#'(begin
BODY ...)]
[(with-inclusion-condition #f
BODY ...)
#'(void)]
[(with-inclusion-condition (KEY VAL FILEPATH)
BODY ...)
(with-syntax ([result (check-json (syntax->datum #'KEY) (syntax->datum #'VAL)
(syntax->datum #'FILEPATH))])
#'(with-inclusion-condition result BODY ...))]))
Of course, this becomes more complicated if one were to allow arbitrary code to be executed as the check. I can't just use eval-syntax
willy-nilly, because the necessary external pieces of code might not be present at the time or place where the check is performed.
Would this be considered "un-Rackety", so to speak, because of a better/more general solution?