summary refs log tree commit diff
path: root/tests/services/configuration.scm
diff options
context:
space:
mode:
authorXinglu Chen <public@yoctocell.xyz>2021-06-12 21:17:08 +0200
committerLudovic Courtès <ludo@gnu.org>2021-06-29 12:37:33 +0200
commit2ad896751c8f7104f139c3fd43cd632fd428ccd5 (patch)
treebe51b6c07f4a5ad62ee9f1b3ba8e7b393332f21c /tests/services/configuration.scm
parent5d47f30e93268438609acae31b11d31de66bcd57 (diff)
downloadguix-2ad896751c8f7104f139c3fd43cd632fd428ccd5.tar.gz
services: configuration: Allow specifying prefix for serializer names.
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.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
Diffstat (limited to 'tests/services/configuration.scm')
-rw-r--r--tests/services/configuration.scm12
1 files changed, 12 insertions, 0 deletions
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@gmail.com>
+;;; Copyright © 2021 Xinglu Chen <public@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.