From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.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 CJuKIpHahGQqXQAASxT56A (envelope-from ) for ; Sat, 10 Jun 2023 22:18:25 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id YKV4IpHahGTyPAAAauVa8A (envelope-from ) for ; Sat, 10 Jun 2023 22:18:25 +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 4B9A925099 for ; Sat, 10 Jun 2023 22:18:25 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q852C-0000ds-Og; Sat, 10 Jun 2023 16:18:04 -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 1q852A-0000d9-Um for guix-patches@gnu.org; Sat, 10 Jun 2023 16:18:02 -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 1q852A-00062B-Lc for guix-patches@gnu.org; Sat, 10 Jun 2023 16:18:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1q852A-0006zZ-FH for guix-patches@gnu.org; Sat, 10 Jun 2023 16:18:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63985] [PATCH RFC v2 4/5] services: configuration: Add serializer-options field. Resent-From: Bruno Victal Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 10 Jun 2023 20:18:02 +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.168642822326775 (code B ref 63985); Sat, 10 Jun 2023 20:18:02 +0000 Received: (at 63985) by debbugs.gnu.org; 10 Jun 2023 20:17:03 +0000 Received: from localhost ([127.0.0.1]:36380 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q851B-0006xT-Ro for submit@debbugs.gnu.org; Sat, 10 Jun 2023 16:17:02 -0400 Received: from smtpmciv5.myservices.hosting ([185.26.107.241]:50460) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q8519-0006xB-Ar for 63985@debbugs.gnu.org; Sat, 10 Jun 2023 16:17:00 -0400 Received: from mail1.netim.hosting (unknown [185.26.106.173]) by smtpmciv5.myservices.hosting (Postfix) with ESMTP id B499F20DD3 for <63985@debbugs.gnu.org>; Sat, 10 Jun 2023 22:16:58 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail1.netim.hosting (Postfix) with ESMTP id 4BAF58009A; Sat, 10 Jun 2023 22:11:33 +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 E1PIExsNQxch; Sat, 10 Jun 2023 22:11:32 +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 B881380098; Sat, 10 Jun 2023 22:11:32 +0200 (CEST) From: Bruno Victal Date: Sat, 10 Jun 2023 21:10:55 +0100 Message-Id: <38055b82cfb09cbadfb6eb06f45953fb4303846b.1686427611.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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1686428305; 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=H6uQV8SB7Dd04waa4pZGK8bfe7OXWwJLOYkHEMPWqHY=; b=ZL4zmlYhkYduQimUTzM5u+RcE7cn9oO2raQH/NphoqoyqrJnIuKp+Tv0zehe/5/pBiu5/k U7prEwNhmcv6F9n5lCSe9le+TPqE8VHN0x1oj/9jNARPs05XiGnoNXL85VgM7YZE3y+x2L ZFg7U9Ofu7X3N5G7rtVLGmOLtTIy27yV3tVK6MgO8kKYmqpQwjHLkLnOZORtn3X/zcMW+i Hp+POEznJefS+YCPgKgwjn2MZC4vNVG4xaC823b+bfNPKIQ8NDCt665Allohlbphv9fEXM /4LFpxcRJKSVf1L1EzIna0CRT3Z3FSTsLarA7umydEa/sI4qTeKVO3n+kDiZJg== 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-Seal: i=1; s=key1; d=yhetil.org; t=1686428305; a=rsa-sha256; cv=none; b=L/D4kmtOabXwjUfg18P1R370vsUSRzr9Skw0cNwqVJmf87i5eBji68Muc/wkzzSSLs0lD0 ijihEVGbTVYvcb0p2xIAB4SC93Apot+u4lg3V9jIcBbr1WGpWaIKjfatmSZ/vo2AUSb4UL +UwsDQNEbDSNhzuyyzL4JmYjcab6Pa96v2xJQ5u5BLREFP2o2onD7Eh2go2Ptp0/2MlXtL mulPp+hEA6UWMVLSg6OWxyBcHCussJxKlyqkLVRLEeQ44IiU5bhGZ+hxyUy14i7g6IxOYW qzXPIDBADAMQ+gpcwxUKLfLN6NxZYKSkAIZ3Pjdl5mBuD7eF6/LIeznciFq7EQ== X-Migadu-Scanner: scn1.migadu.com X-Migadu-Spam-Score: -1.71 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" X-Migadu-Queue-Id: 4B9A925099 X-Spam-Score: -1.71 X-TUID: zPd2aKkP+Udk 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. --- gnu/services/configuration.scm | 49 +++++++++++++++++++++----------- tests/services/configuration.scm | 34 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 16 deletions(-) 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 1db83bb273..d23a0cde89 100644 --- a/tests/services/configuration.scm +++ b/tests/services/configuration.scm @@ -297,6 +297,40 @@ (define (sanitize-port value) (lambda _ "lorem") (sanitizer (lambda () #t))))))) +(test-group "Serializer 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]")))) + + (test-equal "keyword argument provided" + "host=[2001:db8::1],port=80" + (eval-gexp + (serialize-configuration (kwarg-config) + kwarg-config-fields)))) + +(test-group "Serializer keyword arguments, define-maybe" + (define* (serialize-port field value #:key host) + (format #f "host=~a,port=~d" host value)) + + (define-maybe port) + + (define-configuration kwarg-config + (port + (maybe-port 80) + "Lorem Ipsum." + (serializer-options '(#:host "[2001:db8::1]")))) + + (test-equal "keyword argument provided, maybe type" + "host=[2001:db8::1],port=80" + (eval-gexp + (serialize-configuration (kwarg-config) + kwarg-config-fields)))) + ;;; ;;; define-maybe macro. -- 2.39.2