GNU bug report logs -
#48976
[PATCH] services: configuration: Allow specifying prefix for serializer names.
Previous Next
Reported by: Xinglu Chen <public <at> yoctocell.xyz>
Date: Sat, 12 Jun 2021 19:18:02 UTC
Severity: normal
Tags: patch
Done: Ludovic Courtès <ludo <at> gnu.org>
Bug is archived. No further changes may be made.
To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 48976 in the body.
You can then email your comments to 48976 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
guix-patches <at> gnu.org
:
bug#48976
; Package
guix-patches
.
(Sat, 12 Jun 2021 19:18:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Xinglu Chen <public <at> yoctocell.xyz>
:
New bug report received and forwarded. Copy sent to
guix-patches <at> gnu.org
.
(Sat, 12 Jun 2021 19:18:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
Sometimes two configurations might have the same types for their field values,
but the values might be serialized in two completely different
ways (e.g. because the two programs have different configuration languages).
An example of this would be the ‘serialize-boolean’ procedure in (gnu services
mail) and (gnu services getmail). They both serialize a boolean value, but
because the Dovecot’s configuration language has a different syntax to the
configuration language for Getmail, two different procedures have to be
defined.
One way to workaround this would be to specify custom serializers for many
fields in order to separate the serialization of the values that have the same
type but serialize in different ways. This could get very tedious, especially
if there are many configurations in the same module.
Another way would be to move one of the configurations to its own module, like
what was done with (gnu services getmail). However, this would mean that
there would be multiple modules containing configurations for related
programs, e.g. we have (gnu services mail) and (gnu services getmail), it
doesn’t make much sense to keep the Getmail configuration in its own module.
This patch will allow one to write something like this:
(define-configuration foo-configuration
(bar
(string "bob")
"Option bar.")
(prefix bar-))
and the value of the ‘bar’ field would be serialized using a procedure named
‘bar-serialize-string’ instead of just ‘serialize-string’.
* gnu/services/configuration.scm (define-maybe-helper): Accept ‘prefix’
argument for using serializer with custom prefix.
(define-maybe): Pattern match on ‘prefix’ literal.
(define-configuration-helper): Accept ‘prefix’ argument for using serializer
with custom prefix.
(define-configuration): Pattern match on ‘prefix’ literal.
* tests/services/configuration.scm ("serialize-configuration with prefix"):
New test.
---
On top of that, I also have few services (at least 3 of them are
mail-related, Procmail, Notmuch, L2md) I want to upstream once Guix Home
has been merged. With this patch it would be possible to sort of put
each configuration in a namespace without having to create new modules
that just contain one configuration.
gnu/services/configuration.scm | 38 +++++++++++++++++++++++---------
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 f23840ee6d..fd07b6fa49 100644
--- a/gnu/services/configuration.scm
+++ b/gnu/services/configuration.scm
@@ -109,14 +109,18 @@ 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)
+(define (define-maybe-helper serialize? prefix 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)))
+ (serialize-stem (if prefix
+ (id #'stem prefix #'serialize- #'stem)
+ (id #'stem #'serialize- #'stem)))
+ (serialize-maybe-stem (if prefix
+ (id #'stem prefix #'serialize-maybe- #'stem)
+ (id #'stem #'serialize-maybe- #'stem))))
#`(begin
(define (maybe-stem? val)
(or (eq? val 'disabled) (stem? val)))
@@ -129,16 +133,18 @@ does not have a default value" field kind)))
(define-syntax define-maybe
(lambda (x)
- (syntax-case x (no-serialization)
+ (syntax-case x (no-serialization prefix)
((_ stem (no-serialization))
- (define-maybe-helper #f #'(_ stem)))
+ (define-maybe-helper #f #f #'(_ stem)))
+ ((_ stem (prefix serializer-prefix))
+ (define-maybe-helper #t #'serializer-prefix #'(_ stem)))
((_ stem)
- (define-maybe-helper #t #'(_ stem))))))
+ (define-maybe-helper #t #f #'(_ stem))))))
(define-syntax-rule (define-maybe/no-serialization stem)
(define-maybe stem (no-serialization)))
-(define (define-configuration-helper serialize? syn)
+(define (define-configuration-helper serialize? serializer-prefix syn)
(syntax-case syn ()
((_ stem (field (field-type def ...) doc custom-serializer ...) ...)
(with-syntax (((field-getter ...)
@@ -165,7 +171,11 @@ does not have a default value" field kind)))
((serializer)
serializer)
(()
- (id #'stem #'serialize- type)))))
+ (if serializer-prefix
+ (id #'stem
+ serializer-prefix
+ #'serialize- type)
+ (id #'stem #'serialize- type))))))
#'(field-type ...)
#'((custom-serializer ...) ...))))
#`(begin
@@ -212,15 +222,21 @@ does not have a default value" field kind)))
(define-syntax define-configuration
(lambda (s)
- (syntax-case s (no-serialization)
+ (syntax-case s (no-serialization prefix)
((_ stem (field (field-type def ...) doc custom-serializer ...) ...
(no-serialization))
(define-configuration-helper
- #f #'(_ stem (field (field-type def ...) doc custom-serializer ...)
+ #f #f #'(_ stem (field (field-type def ...) doc custom-serializer ...)
+ ...)))
+ ((_ stem (field (field-type def ...) doc custom-serializer ...) ...
+ (prefix serializer-prefix))
+ (define-configuration-helper
+ #t #'serializer-prefix #'(_ stem (field (field-type def ...)
+ doc custom-serializer ...)
...)))
((_ stem (field (field-type def ...) doc custom-serializer ...) ...)
(define-configuration-helper
- #t #'(_ stem (field (field-type def ...) doc custom-serializer ...)
+ #t #f #'(_ stem (field (field-type def ...) doc custom-serializer ...)
...))))))
(define-syntax-rule (define-configuration/no-serialization
diff --git a/tests/services/configuration.scm b/tests/services/configuration.scm
index 85badd2da6..86a36a388d 100644
--- a/tests/services/configuration.scm
+++ b/tests/services/configuration.scm
@@ -1,5 +1,6 @@
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
+;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -82,6 +83,17 @@
(let ((config (serializable-configuration)))
(serialize-configuration config serializable-configuration-fields)))))
+(define (custom-prefix-serialize-integer field-name name) name)
+
+(define-configuration configuration-with-prefix
+ (port (integer 10) "The port number.")
+ (prefix custom-prefix-))
+
+(test-assert "serialize-configuration with prefix"
+ (gexp?
+ (let ((config (configuration-with-prefix)))
+ (serialize-configuration config configuration-with-prefix-fields))))
+
;;;
;;; define-maybe macro.
base-commit: 00727470b92e0b8ab0e00dd2cd87495c0c136341
--
2.32.0
Reply sent
to
Ludovic Courtès <ludo <at> gnu.org>
:
You have taken responsibility.
(Tue, 29 Jun 2021 10:04:02 GMT)
Full text and
rfc822 format available.
Notification sent
to
Xinglu Chen <public <at> yoctocell.xyz>
:
bug acknowledged by developer.
(Tue, 29 Jun 2021 10:04:02 GMT)
Full text and
rfc822 format available.
Message #10 received at 48976-done <at> debbugs.gnu.org (full text, mbox):
Hi,
Xinglu Chen <public <at> yoctocell.xyz> skribis:
> Sometimes two configurations might have the same types for their field values,
> but the values might be serialized in two completely different
> ways (e.g. because the two programs have different configuration languages).
>
> An example of this would be the ‘serialize-boolean’ procedure in (gnu services
> mail) and (gnu services getmail). They both serialize a boolean value, but
> because the Dovecot’s configuration language has a different syntax to the
> configuration language for Getmail, two different procedures have to be
> defined.
>
> One way to workaround this would be to specify custom serializers for many
> fields in order to separate the serialization of the values that have the same
> type but serialize in different ways. This could get very tedious, especially
> if there are many configurations in the same module.
>
> Another way would be to move one of the configurations to its own module, like
> what was done with (gnu services getmail). However, this would mean that
> there would be multiple modules containing configurations for related
> programs, e.g. we have (gnu services mail) and (gnu services getmail), it
> doesn’t make much sense to keep the Getmail configuration in its own module.
>
> This patch will allow one to write something like this:
>
> (define-configuration foo-configuration
> (bar
> (string "bob")
> "Option bar.")
> (prefix bar-))
>
> and the value of the ‘bar’ field would be serialized using a procedure named
> ‘bar-serialize-string’ instead of just ‘serialize-string’.
>
> * gnu/services/configuration.scm (define-maybe-helper): Accept ‘prefix’
> argument for using serializer with custom prefix.
> (define-maybe): Pattern match on ‘prefix’ literal.
> (define-configuration-helper): Accept ‘prefix’ argument for using serializer
> with custom prefix.
> (define-configuration): Pattern match on ‘prefix’ literal.
> * tests/services/configuration.scm ("serialize-configuration with prefix"):
> New test.
Looks reasonable to me. Applied, thanks!
Ludo’.
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Tue, 27 Jul 2021 11:24:06 GMT)
Full text and
rfc822 format available.
This bug report was last modified 3 years and 323 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.