summaryrefslogtreecommitdiff
path: root/gnu/services
diff options
context:
space:
mode:
authorMaxim Cournoyer <maxim.cournoyer@gmail.com>2021-05-16 01:09:39 -0400
committerMaxim Cournoyer <maxim.cournoyer@gmail.com>2021-05-17 23:27:39 -0400
commitb7297d66c58b4fe2c153dce4f1069235269cd005 (patch)
tree40f2e87c8f74db175aab3037f0b374e72974fea5 /gnu/services
parente7e2b1c10b9a779ac7938909dbfafbc739740363 (diff)
services: configuration: Add a define-maybe/no-serialization syntax.
Before this change, using define-maybe along define-configuration with the no-serialization syntactic keyword would result in the following warning: warning: possibly unbound variable `VARIABLE-NAME' This change introduces the define-maybe/no-serialization variant that does away with defining a serialization helper procedure, which makes it possible to avoid the above warning. * gnu/services/configuration.scm (define-maybe/no-serialization): New syntax. (define-maybe-helper): New procedure. (define-maybe): Define syntax using the above procedure. * tests/services/configuration.scm (tests): Fix module name. (custom-number-serializer): Do not print to standard output. (maybe-number?, serialize-maybe-number): New procedures defined via the define-maybe macro. (config-with-maybe-number): New configuration. (serialize-number): New procedure. ("maybe value serialization"): New test. (maybe-string?): New procedure defined via the define-maybe/no-serialization macro. (config-with-maybe-string/no-serialization): New configuration. ("maybe value without serialization no procedure bound"): New test.
Diffstat (limited to 'gnu/services')
-rw-r--r--gnu/services/configuration.scm37
1 files changed, 26 insertions, 11 deletions
diff --git a/gnu/services/configuration.scm b/gnu/services/configuration.scm
index 21cb829382..72b1d1cec6 100644
--- a/gnu/services/configuration.scm
+++ b/gnu/services/configuration.scm
@@ -48,6 +48,7 @@
serialize-configuration
define-maybe
+ define-maybe/no-serialization
validate-configuration
generate-documentation
configuration->documentation
@@ -107,20 +108,34 @@ does not have a default value" field kind)))
"Assemble PARTS into a raw (unhygienic) identifier."
(datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
+(define (define-maybe-helper serialize? syn)
+ (syntax-case syn ()
+ ((_ stem)
+ (with-syntax
+ ((stem? (id #'stem #'stem #'?))
+ (maybe-stem? (id #'stem #'maybe- #'stem #'?))
+ (serialize-stem (id #'stem #'serialize- #'stem))
+ (serialize-maybe-stem (id #'stem #'serialize-maybe- #'stem)))
+ #`(begin
+ (define (maybe-stem? val)
+ (or (eq? val 'disabled) (stem? val)))
+ #,@(if serialize?
+ (list #'(define (serialize-maybe-stem field-name val)
+ (if (stem? val)
+ (serialize-stem field-name val)
+ "")))
+ '()))))))
+
(define-syntax define-maybe
(lambda (x)
- (syntax-case x ()
+ (syntax-case x (no-serialization)
+ ((_ stem (no-serialization))
+ (define-maybe-helper #f #'(_ stem)))
((_ stem)
- (with-syntax
- ((stem? (id #'stem #'stem #'?))
- (maybe-stem? (id #'stem #'maybe- #'stem #'?))
- (serialize-stem (id #'stem #'serialize- #'stem))
- (serialize-maybe-stem (id #'stem #'serialize-maybe- #'stem)))
- #'(begin
- (define (maybe-stem? val)
- (or (eq? val 'disabled) (stem? val)))
- (define (serialize-maybe-stem field-name val)
- (if (stem? val) (serialize-stem field-name val) ""))))))))
+ (define-maybe-helper #t #'(_ stem))))))
+
+(define-syntax-rule (define-maybe/no-serialization stem)
+ (define-maybe stem (no-serialization)))
(define (define-configuration-helper serialize? syn)
(syntax-case syn ()