diff options
| -rw-r--r-- | gnu/services/configuration.scm | 38 | ||||
| -rw-r--r-- | tests/services/configuration.scm | 12 |
2 files changed, 39 insertions, 11 deletions
diff --git a/gnu/services/configuration.scm b/gnu/services/configuration.scm index f23840ee6d1..fd07b6fa49e 100644 --- a/gnu/services/configuration.scm +++ b/gnu/services/configuration.scm | |||
| @@ -109,14 +109,18 @@ does not have a default value" field kind))) | |||
| 109 | "Assemble PARTS into a raw (unhygienic) identifier." | 109 | "Assemble PARTS into a raw (unhygienic) identifier." |
| 110 | (datum->syntax ctx (symbol-append (syntax->datum parts) ...))) | 110 | (datum->syntax ctx (symbol-append (syntax->datum parts) ...))) |
| 111 | 111 | ||
| 112 | (define (define-maybe-helper serialize? syn) | 112 | (define (define-maybe-helper serialize? prefix syn) |
| 113 | (syntax-case syn () | 113 | (syntax-case syn () |
| 114 | ((_ stem) | 114 | ((_ stem) |
| 115 | (with-syntax | 115 | (with-syntax |
| 116 | ((stem? (id #'stem #'stem #'?)) | 116 | ((stem? (id #'stem #'stem #'?)) |
| 117 | (maybe-stem? (id #'stem #'maybe- #'stem #'?)) | 117 | (maybe-stem? (id #'stem #'maybe- #'stem #'?)) |
| 118 | (serialize-stem (id #'stem #'serialize- #'stem)) | 118 | (serialize-stem (if prefix |
| 119 | (serialize-maybe-stem (id #'stem #'serialize-maybe- #'stem))) | 119 | (id #'stem prefix #'serialize- #'stem) |
| 120 | (id #'stem #'serialize- #'stem))) | ||
| 121 | (serialize-maybe-stem (if prefix | ||
| 122 | (id #'stem prefix #'serialize-maybe- #'stem) | ||
| 123 | (id #'stem #'serialize-maybe- #'stem)))) | ||
| 120 | #`(begin | 124 | #`(begin |
| 121 | (define (maybe-stem? val) | 125 | (define (maybe-stem? val) |
| 122 | (or (eq? val 'disabled) (stem? val))) | 126 | (or (eq? val 'disabled) (stem? val))) |
| @@ -129,16 +133,18 @@ does not have a default value" field kind))) | |||
| 129 | 133 | ||
| 130 | (define-syntax define-maybe | 134 | (define-syntax define-maybe |
| 131 | (lambda (x) | 135 | (lambda (x) |
| 132 | (syntax-case x (no-serialization) | 136 | (syntax-case x (no-serialization prefix) |
| 133 | ((_ stem (no-serialization)) | 137 | ((_ stem (no-serialization)) |
| 134 | (define-maybe-helper #f #'(_ stem))) | 138 | (define-maybe-helper #f #f #'(_ stem))) |
| 139 | ((_ stem (prefix serializer-prefix)) | ||
| 140 | (define-maybe-helper #t #'serializer-prefix #'(_ stem))) | ||
| 135 | ((_ stem) | 141 | ((_ stem) |
| 136 | (define-maybe-helper #t #'(_ stem)))))) | 142 | (define-maybe-helper #t #f #'(_ stem)))))) |
| 137 | 143 | ||
| 138 | (define-syntax-rule (define-maybe/no-serialization stem) | 144 | (define-syntax-rule (define-maybe/no-serialization stem) |
| 139 | (define-maybe stem (no-serialization))) | 145 | (define-maybe stem (no-serialization))) |
| 140 | 146 | ||
| 141 | (define (define-configuration-helper serialize? syn) | 147 | (define (define-configuration-helper serialize? serializer-prefix syn) |
| 142 | (syntax-case syn () | 148 | (syntax-case syn () |
| 143 | ((_ stem (field (field-type def ...) doc custom-serializer ...) ...) | 149 | ((_ stem (field (field-type def ...) doc custom-serializer ...) ...) |
| 144 | (with-syntax (((field-getter ...) | 150 | (with-syntax (((field-getter ...) |
| @@ -165,7 +171,11 @@ does not have a default value" field kind))) | |||
| 165 | ((serializer) | 171 | ((serializer) |
| 166 | serializer) | 172 | serializer) |
| 167 | (() | 173 | (() |
| 168 | (id #'stem #'serialize- type))))) | 174 | (if serializer-prefix |
| 175 | (id #'stem | ||
| 176 | serializer-prefix | ||
| 177 | #'serialize- type) | ||
| 178 | (id #'stem #'serialize- type)))))) | ||
| 169 | #'(field-type ...) | 179 | #'(field-type ...) |
| 170 | #'((custom-serializer ...) ...)))) | 180 | #'((custom-serializer ...) ...)))) |
| 171 | #`(begin | 181 | #`(begin |
| @@ -212,15 +222,21 @@ does not have a default value" field kind))) | |||
| 212 | 222 | ||
| 213 | (define-syntax define-configuration | 223 | (define-syntax define-configuration |
| 214 | (lambda (s) | 224 | (lambda (s) |
| 215 | (syntax-case s (no-serialization) | 225 | (syntax-case s (no-serialization prefix) |
| 216 | ((_ stem (field (field-type def ...) doc custom-serializer ...) ... | 226 | ((_ stem (field (field-type def ...) doc custom-serializer ...) ... |
| 217 | (no-serialization)) | 227 | (no-serialization)) |
| 218 | (define-configuration-helper | 228 | (define-configuration-helper |
| 219 | #f #'(_ stem (field (field-type def ...) doc custom-serializer ...) | 229 | #f #f #'(_ stem (field (field-type def ...) doc custom-serializer ...) |
| 230 | ...))) | ||
| 231 | ((_ stem (field (field-type def ...) doc custom-serializer ...) ... | ||
| 232 | (prefix serializer-prefix)) | ||
| 233 | (define-configuration-helper | ||
| 234 | #t #'serializer-prefix #'(_ stem (field (field-type def ...) | ||
| 235 | doc custom-serializer ...) | ||
| 220 | ...))) | 236 | ...))) |
| 221 | ((_ stem (field (field-type def ...) doc custom-serializer ...) ...) | 237 | ((_ stem (field (field-type def ...) doc custom-serializer ...) ...) |
| 222 | (define-configuration-helper | 238 | (define-configuration-helper |
| 223 | #t #'(_ stem (field (field-type def ...) doc custom-serializer ...) | 239 | #t #f #'(_ stem (field (field-type def ...) doc custom-serializer ...) |
| 224 | ...)))))) | 240 | ...)))))) |
| 225 | 241 | ||
| 226 | (define-syntax-rule (define-configuration/no-serialization | 242 | (define-syntax-rule (define-configuration/no-serialization |
diff --git a/tests/services/configuration.scm b/tests/services/configuration.scm index 85badd2da65..86a36a388d3 100644 --- a/tests/services/configuration.scm +++ b/tests/services/configuration.scm | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com> | 2 | ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com> |
| 3 | ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz> | ||
| 3 | ;;; | 4 | ;;; |
| 4 | ;;; This file is part of GNU Guix. | 5 | ;;; This file is part of GNU Guix. |
| 5 | ;;; | 6 | ;;; |
| @@ -82,6 +83,17 @@ | |||
| 82 | (let ((config (serializable-configuration))) | 83 | (let ((config (serializable-configuration))) |
| 83 | (serialize-configuration config serializable-configuration-fields))))) | 84 | (serialize-configuration config serializable-configuration-fields))))) |
| 84 | 85 | ||
| 86 | (define (custom-prefix-serialize-integer field-name name) name) | ||
| 87 | |||
| 88 | (define-configuration configuration-with-prefix | ||
| 89 | (port (integer 10) "The port number.") | ||
| 90 | (prefix custom-prefix-)) | ||
| 91 | |||
| 92 | (test-assert "serialize-configuration with prefix" | ||
| 93 | (gexp? | ||
| 94 | (let ((config (configuration-with-prefix))) | ||
| 95 | (serialize-configuration config configuration-with-prefix-fields)))) | ||
| 96 | |||
| 85 | 97 | ||
| 86 | ;;; | 98 | ;;; |
| 87 | ;;; define-maybe macro. | 99 | ;;; define-maybe macro. |
