From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id aHRuD8gKmmSXywAASxT56A (envelope-from ) for ; Tue, 27 Jun 2023 00:01:44 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id sPJXD8gKmmSDqgAA9RJhRA (envelope-from ) for ; Tue, 27 Jun 2023 00:01:44 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id CED4B398EB for ; Tue, 27 Jun 2023 00:01:43 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1687816903; a=rsa-sha256; cv=none; b=fGkgws3AQfVbevYQOP9QCozrOdKuC/Db5O6oEz2yrxGQSbikLhW+RJ26YTuhzKFtNif6i7 s/ggkDADmqTWpCjl4LOlQKcmUVcMUSNB3Eg0R5iwzWZZumGW9T8hn6F8CcAcvT3I5rI4kq hL2c7rlC1uRuvwvUoKh+QAp+mFflkXU//Da8a7N9rLPTP0+jAC1C8pnCe/02XsuByPGDfv wwpBwxk1zD14SYHprkpd3sFNLtAoJUB2an2862Xm4Muw8iHCkSKoge+FTi3Y7cD5sHIm+S 9gtfSiX1safEWN7A0r/qSQtTIFgtnTTOC54x9KZT+VzYU6+/c8Em1fGPwtCXLw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1687816903; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=fMI64SBh9esN7k/PF+ibsduHpoUbInjHx2w9owfahg0=; b=IdTz5td1zlD2urSOZp2KLmDNQlByKA8zdyLMwy3O7PnsDTTnQzLtRmUHMrIsww/3r/9Jb8 GNmY/a3myrzFfU1rv4gahK3xfng4u9/zlD21LIheLIRjfvUMQlJxMWk3YRUtuElRMSxXZz moAT9xvir1XcFDkFeCIrXNhvhoCidcOtqCOngXV5vQp8VKCGVnWQTjzDdEWhXeYSfxo5wZ e0FgJt6IcFd3dU50UTIyK/dhk5pJmPWCMeAtEQRAgtnXNBdzNkSYVN4F72BplzSL66vj1C SxNycsD2+6hcjFkPmo9++mhJ3WifU64CLtenllP28BOW6V+cSUqw80cISo8oWg== Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDuGq-0005WG-9Y; Mon, 26 Jun 2023 18:01:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDuGi-00051u-Ru for guix-patches@gnu.org; Mon, 26 Jun 2023 18:01:10 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDuGd-0005XB-UD for guix-patches@gnu.org; Mon, 26 Jun 2023 18:01:06 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qDuGd-0008So-PT for guix-patches@gnu.org; Mon, 26 Jun 2023 18:01:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63985] [PATCH v3 05/11] services: configuration: Add serializer-options field. Resent-From: Bruno Victal Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 26 Jun 2023 22:01:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63985 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63985@debbugs.gnu.org Cc: Bruno Victal Received: via spool by 63985-submit@debbugs.gnu.org id=B63985.168781683632418 (code B ref 63985); Mon, 26 Jun 2023 22:01:03 +0000 Received: (at 63985) by debbugs.gnu.org; 26 Jun 2023 22:00:36 +0000 Received: from localhost ([127.0.0.1]:47181 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qDuGB-0008Ql-9g for submit@debbugs.gnu.org; Mon, 26 Jun 2023 18:00:35 -0400 Received: from smtpmciv4.myservices.hosting ([185.26.107.240]:44064) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qDuG4-0008QG-Bz for 63985@debbugs.gnu.org; Mon, 26 Jun 2023 18:00:29 -0400 Received: from mail1.netim.hosting (unknown [185.26.106.173]) by smtpmciv4.myservices.hosting (Postfix) with ESMTP id 64250207A8 for <63985@debbugs.gnu.org>; Tue, 27 Jun 2023 00:00:11 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail1.netim.hosting (Postfix) with ESMTP id 451B2800A0; Tue, 27 Jun 2023 00:00:11 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail1.netim.hosting Received: from mail1.netim.hosting ([127.0.0.1]) by localhost (mail1-2.netim.hosting [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 1BqnHD56ZU7V; Tue, 27 Jun 2023 00:00:10 +0200 (CEST) Received: from guix-nuc.home.arpa (unknown [10.192.1.83]) (Authenticated sender: lumen@makinata.eu) by mail1.netim.hosting (Postfix) with ESMTPSA id 8A6698009A; Tue, 27 Jun 2023 00:00:10 +0200 (CEST) From: Bruno Victal Date: Mon, 26 Jun 2023 22:59:31 +0100 Message-Id: <3ff11bf98a7658400615305609654432a4f30bb0.1687816734.git.mirai@makinata.eu> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: scn1.migadu.com X-Migadu-Spam-Score: -1.75 X-Migadu-Queue-Id: CED4B398EB X-Spam-Score: -1.75 X-TUID: p/Mc8YJsuWQ4 Allow relaying additional arguments to a serialize- procedure. * gnu/services/configuration.scm (configuration-field) : New field. (base-transducer, define-maybe-helper): Adjust to relay additional arguments. (normalize-extra-args): Implement serializer-options literal. * tests/services/configuration.scm: Add tests for serializer-options. * doc/guix.texi (Complex Configurations): Document serializer-options. --- doc/guix.texi | 11 +++++ gnu/services/configuration.scm | 49 ++++++++++++------- tests/services/configuration.scm | 82 ++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 16 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 8355260378..14802e9366 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -42032,6 +42032,17 @@ Complex Configurations ((symbol? value) (symbol->string value)) (else (error "bad value")))) @end lisp + +@item @code{(serializer-options @var{arglst})} +@var{arglst} is a list of extra arguments that are relayed to the +serializing procedure. This allows for writing serialization +procedures that take more than two arguments. + +An example of a serializer procedure that requires additional data: +@lisp +(define* (serialize-port field value #:key context) + #~(format #f "section=~a,port=~d" #$context #$value)) +@end lisp @end table In some cases multiple different configuration records might be defined diff --git a/gnu/services/configuration.scm b/gnu/services/configuration.scm index cd2cb8318b..4eee5a26c2 100644 --- a/gnu/services/configuration.scm +++ b/gnu/services/configuration.scm @@ -50,6 +50,7 @@ (define-module (gnu services configuration) configuration-field-error configuration-field-sanitizer configuration-field-serializer + configuration-field-serializer-options configuration-field-getter configuration-field-default-value-thunk configuration-field-documentation @@ -127,6 +128,7 @@ (define-record-type* (predicate configuration-field-predicate) (sanitizer configuration-field-sanitizer) (serializer configuration-field-serializer) + (serializer-options configuration-field-serializer-options) (default-value-thunk configuration-field-default-value-thunk) (documentation configuration-field-documentation)) @@ -144,9 +146,13 @@ (define (base-transducer config) ;; Only serialize fields whose value isn't '%unset-marker%. (tfilter-maybe-value config) (tmap (lambda (field) - ((configuration-field-serializer field) - (configuration-field-name field) - ((configuration-field-getter field) config)))))) + (let ((serializer (configuration-field-serializer field)) + (field-name (configuration-field-name field)) + (value + ((configuration-field-getter field) config)) + (serializer-options + (configuration-field-serializer-options field))) + (apply serializer field-name value serializer-options)))))) (define (serialize-configuration config fields) #~(string-append @@ -173,10 +179,9 @@ (define (define-maybe-helper serialize? prefix syn) (or (not (maybe-value-set? val)) (stem? val))) #,@(if serialize? - (list #'(define (serialize-maybe-stem field-name val) - (if (stem? val) - (serialize-stem field-name val) - ""))) + (list #'(define (serialize-maybe-stem field-name val . rest) + (when (maybe-value-set? val) + (apply serialize-stem field-name val rest)))) '())))))) (define-syntax define-maybe @@ -210,38 +215,49 @@ (define (define-configuration-helper serialize? serializer-prefix syn) "Extract and normalize arguments following @var{doc}." (let loop ((s s) (sanitizer* #f) - (serializer* #f)) - (syntax-case s (sanitizer serializer empty-serializer) + (serializer* #f) + (serializer-options* #f)) + (syntax-case s (sanitizer serializer empty-serializer serializer-options) (((sanitizer proc) tail ...) (if sanitizer* (syntax-violation 'sanitizer "duplicate entry" #'proc) - (loop #'(tail ...) #'proc serializer*))) + (loop #'(tail ...) + #'proc serializer* serializer-options*))) (((serializer proc) tail ...) (if serializer* (syntax-violation 'serializer "duplicate or conflicting entry" #'proc) - (loop #'(tail ...) sanitizer* #'proc))) + (loop #'(tail ...) + sanitizer* #'proc serializer-options*))) ((empty-serializer tail ...) (if serializer* (syntax-violation 'empty-serializer "duplicate or conflicting entry" #f) - (loop #'(tail ...) sanitizer* #'empty-serializer))) + (loop #'(tail ...) + sanitizer* #'empty-serializer #f))) + (((serializer-options args) tail ...) + (if serializer-options* + (syntax-violation 'serializer-options + "duplicate or conflicting entry" #f) + (loop #'(tail ...) + sanitizer* serializer* #'args))) (() ; stop condition - (values (list sanitizer* serializer*))) + (values (list sanitizer* serializer* + (or serializer-options* #'(quote ()))))) ((proc) ; TODO: deprecated, to be removed. - (every not (list sanitizer* serializer*)) + (every not (list sanitizer* serializer* serializer-options*)) (begin (warning #f (G_ "specifying serializers after documentation is \ deprecated, use (serializer ~a) instead~%") (syntax->datum #'proc)) - (values (list #f #'proc))))))) + (values (list #f #'proc #'(quote ())))))))) (syntax-case syn () ((_ stem (field field-type+def doc extra-args ...) ...) (with-syntax ((((field-type def) ...) (map normalize-field-type+def #'(field-type+def ...))) - (((sanitizer* serializer*) ...) + (((sanitizer* serializer* serializer-options*) ...) (map normalize-extra-args #'((extra-args ...) ...)))) (with-syntax (((field-getter ...) @@ -327,6 +343,7 @@ (define (define-configuration-helper serialize? serializer-prefix syn) (or field-sanitizer (id #'stem #'validate- #'stem #'- #'field))) (serializer field-serializer) + (serializer-options serializer-options*) (default-value-thunk (lambda () (if (maybe-value-set? (syntax->datum field-default)) diff --git a/tests/services/configuration.scm b/tests/services/configuration.scm index 40a4e74b4d..8b1d1e4749 100644 --- a/tests/services/configuration.scm +++ b/tests/services/configuration.scm @@ -297,6 +297,88 @@ (define (sanitize-port value) (lambda _ "lorem") (sanitizer (lambda () #t))))))) +(test-group "Serializer options" + (test-group "Serialize keyword arguments" + (define* (serialize-port field value #:key host) + (format #f "host=~a,port=~d" host value)) + + (define-configuration kwarg-config + (port + (port 80) + "Lorem Ipsum." + (serializer-options '(#:host "[2001:db8::1]")))) + + (define-maybe port) + (define-configuration kwarg-maybe-config + (port + (maybe-port 80) + "Lorem Ipsum." + (serializer-options '(#:host "[2001:db8::1]")))) + + (test-equal "keyword argument provided" + "host=[2001:db8::1],port=80" + (eval-gexp + (serialize-configuration (kwarg-config) + kwarg-config-fields))) + + (test-equal "keyword argument provided, maybe type" + "host=[2001:db8::1],port=80" + (eval-gexp + (serialize-configuration (kwarg-maybe-config) + kwarg-maybe-config-fields)))) + + (test-group "Serialize optional arguments" + (define* (serialize-port field-name value #:optional override-name) + (format #f "~a=~d" (or override-name field-name) value)) + + (define-configuration with-optarg + (port + (port 80) + "Lorem Ipsum." + (serializer-options '(service-port)))) + + (define-configuration without-optarg + (port + (port 80) + "Lorem Ipsum.")) + + (test-equal "optional argument, provided" + "service-port=80" + (eval-gexp (serialize-configuration (with-optarg) + with-optarg-fields))) + + (test-equal "optional argument, absent" + "port=80" + (eval-gexp (serialize-configuration (without-optarg) + without-optarg-fields)))) + + (test-group "Serialize optional & keyword arguments" + (define* (serialize-port field-name value #:optional override-name + #:key host) + (format #f "host=~a,~a=~d" host (or override-name field-name) value)) + + (define-configuration mixed-args + (port + (port 80) + "Lorem Ipsum." + (serializer-options '(service-port #:host "example.com")))) + + (define-configuration mixed-no-optarg + (port + (port 80) + "Lorem Ipsum." + (serializer-options '(#:host "example.com")))) + + (test-equal "mixed arguments, optional provided" + "host=example.com,service-port=80" + (eval-gexp (serialize-configuration (mixed-args) + mixed-args-fields))) + + (test-equal "mixed arguments, optional absent" + "host=example.com,port=80" + (eval-gexp (serialize-configuration (mixed-no-optarg) + mixed-no-optarg-fields))))) + ;;; ;;; define-maybe macro. -- 2.39.2