From patchwork Tue May 17 11:39:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Attila Lendvai X-Patchwork-Id: 39450 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 8196327BBEA; Tue, 17 May 2022 13:59:18 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id B1CB127BBE9 for ; Tue, 17 May 2022 13:59:17 +0100 (BST) Received: from localhost ([::1]:43130 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nqwnE-0001ua-Ri for patchwork@mira.cbaines.net; Tue, 17 May 2022 08:59:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43538) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqvZW-0002Re-KH for guix-patches@gnu.org; Tue, 17 May 2022 07:41:08 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:33837) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nqvZW-0003bW-2W for guix-patches@gnu.org; Tue, 17 May 2022 07:41:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nqvZW-0005ek-0r for guix-patches@gnu.org; Tue, 17 May 2022 07:41:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54674] [PATCH v5 1/3] services: configuration: Support (field1 maybe-number "") format. References: <20220401191957.16624-1-attila@lendvai.name> In-Reply-To: <20220401191957.16624-1-attila@lendvai.name> Resent-From: Attila Lendvai Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 17 May 2022 11:41:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54674 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54674@debbugs.gnu.org Cc: Attila Lendvai Received: via spool by 54674-submit@debbugs.gnu.org id=B54674.165278761021639 (code B ref 54674); Tue, 17 May 2022 11:41:01 +0000 Received: (at 54674) by debbugs.gnu.org; 17 May 2022 11:40:10 +0000 Received: from localhost ([127.0.0.1]:55961 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nqvYe-0005cu-SH for submit@debbugs.gnu.org; Tue, 17 May 2022 07:40:09 -0400 Received: from mail-ej1-f47.google.com ([209.85.218.47]:39528) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nqvYc-0005cS-SD for 54674@debbugs.gnu.org; Tue, 17 May 2022 07:40:07 -0400 Received: by mail-ej1-f47.google.com with SMTP id tk15so10609824ejc.6 for <54674@debbugs.gnu.org>; Tue, 17 May 2022 04:40:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6uUZF8XXQGeV4rmDblZZ7y/STfpyc3AamXzBcSZcbQ4=; b=mqWs1SKPYQnzFYvkg3eREGSBUoZmAEUNR4WDhFuj/uLpweSpQuIEpjJ77XPDXS2U4v 5JTX1rdhm3zFG3bgYITKU35xUjzulybXwrQmsz6Jzl6j6O2GNREYl4fX2gK1Eg44fWpl cNlT/HeR33/rQDclkF54O8s7r3GReOljt/7xDI8yi1vLJ8KefwiRrtCgsVCHL7wX3Hta gX1nCnR0Hn68BwqCxxqHP6RZ1BU602fAvCxTYsPFdVdZO2BYWci5eIEYU+Kh7CIITfVT zFuEpUw9acuQH9vSjA/jH0hQEBLajpnlu1mDZao6ebou+Rtf+Lr/Au2pr4S3jhBABpFV 6YVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=6uUZF8XXQGeV4rmDblZZ7y/STfpyc3AamXzBcSZcbQ4=; b=AlTQeVc+6+ARltmo13jAMrg+ofWgUDYCgVWrXfT12BMQIK6MK2DkrLy9NteBnUcBTQ GtUI5DGcMXfYaOFiIMwoV8fCgNjPIrT7kUQUvsJZKTTW2fMCkxZJofcIdaEJIc5AOAXJ EbubGUoT/LHKeGo4/MDgjoxVMhqtkVGjVlNeUQoVCf2TyOASvw4AX1pHwKF6LAR36jRE LgRrqnTYHdAEJkWnoMr3PBJiZFAX7kAGJ23Dz+5GmVMFqIvH/uuxF43Vfnmm392HvCyb iPyPjitCpouhGrEsqe443QeH3IxgdtajTLBggZSLnrACyNZ3qZoJLx4xr/AbspcRVwmH cndw== X-Gm-Message-State: AOAM532lRpt5+bElHahhYxYDqyC8+TCQhEgpUb1n+ZNcKuVKHWjTtZ6D YNpQc9kIivEWJqnLAqFMf1xgYXAGsPE= X-Google-Smtp-Source: ABdhPJxvhf7kwMXL4pnKj0Q1SY7ylBmlBrSrDZQgXx/ddbfjGS3Ap/+NT8UsS2UFoWmGeYdhgFJrqA== X-Received: by 2002:a17:906:d554:b0:6f5:2242:a499 with SMTP id cr20-20020a170906d55400b006f52242a499mr19140339ejc.488.1652787600765; Tue, 17 May 2022 04:40:00 -0700 (PDT) Received: from lelap.local (catv-89-132-245-188.catv.fixed.vodafone.hu. [89.132.245.188]) by smtp.gmail.com with ESMTPSA id 12-20020a17090602cc00b006fe7725ed7dsm9154ejk.34.2022.05.17.04.39.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 04:40:00 -0700 (PDT) From: Attila Lendvai Date: Tue, 17 May 2022 13:39:26 +0200 Message-Id: <20220517113926.3931-1-attila@lendvai.name> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 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+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches As opposed to explicitly using 'disabled as value, or using the (field1 (maybe-number) "") format. It's mostly the work of Maxime Devos shared under #54674, with some modifications by Attila Lendvai. * gnu/services/configuration.scm (normalize-field-type+def): New function. (define-configuration-helper) (define-configuration): Support new field format. * tests/services/configuration.scm (config-with-maybe-number->string): New function. ("maybe value serialization of the instance"): New test. ("maybe value serialization of the instance, unspecified"): New test. --- v5 fixes some jami issues gnu/services/configuration.scm | 169 +++++++++++++++++-------------- tests/services/configuration.scm | 28 ++++- 2 files changed, 114 insertions(+), 83 deletions(-) diff --git a/gnu/services/configuration.scm b/gnu/services/configuration.scm index 0de350a4df..bdca33ed68 100644 --- a/gnu/services/configuration.scm +++ b/gnu/services/configuration.scm @@ -5,6 +5,7 @@ ;;; Copyright © 2021 Xinglu Chen ;;; Copyright © 2021 Maxim Cournoyer ;;; Copyright © 2021 Andrew Tropin +;;; Copyright © 2022 Maxime Devos ;;; ;;; This file is part of GNU Guix. ;;; @@ -162,78 +163,90 @@ (define-maybe-helper #t #f #'(_ stem)))))) (define-syntax-rule (define-maybe/no-serialization stem) (define-maybe stem (no-serialization))) +(define (normalize-field-type+def s) + (syntax-case s () + ((field-type def) + (identifier? #'field-type) + (values #'(field-type def))) + ((field-type) + (identifier? #'field-type) + (values #'(field-type 'disabled))) + (field-type + (identifier? #'field-type) + (values #'(field-type 'disabled))))) + (define (define-configuration-helper serialize? serializer-prefix syn) (syntax-case syn () - ((_ stem (field (field-type def ...) doc custom-serializer ...) ...) - (with-syntax (((field-getter ...) - (map (lambda (field) - (id #'stem #'stem #'- field)) - #'(field ...))) - ((field-predicate ...) - (map (lambda (type) - (id #'stem type #'?)) - #'(field-type ...))) - ((field-default ...) - (map (match-lambda - ((field-type default-value) - default-value) - ((field-type) - ;; Quote `undefined' to prevent a possibly - ;; unbound warning. - (syntax 'undefined))) - #'((field-type def ...) ...))) - ((field-serializer ...) - (map (lambda (type custom-serializer) - (and serialize? - (match custom-serializer - ((serializer) - serializer) - (() - (if serializer-prefix - (id #'stem - serializer-prefix - #'serialize- type) - (id #'stem #'serialize- type)))))) - #'(field-type ...) - #'((custom-serializer ...) ...)))) - #`(begin - (define-record-type* #,(id #'stem #'< #'stem #'>) - #,(id #'stem #'% #'stem) - #,(id #'stem #'make- #'stem) - #,(id #'stem #'stem #'?) - (%location #,(id #'stem #'stem #'-location) - (default (and=> (current-source-location) - source-properties->location)) - (innate)) - #,@(map (lambda (name getter def) - (if (eq? (syntax->datum def) (quote 'undefined)) - #`(#,name #,getter) - #`(#,name #,getter (default #,def)))) - #'(field ...) - #'(field-getter ...) - #'(field-default ...))) - (define #,(id #'stem #'stem #'-fields) - (list (configuration-field - (name 'field) - (type 'field-type) - (getter field-getter) - (predicate field-predicate) - (serializer field-serializer) - (default-value-thunk - (lambda () - (display '#,(id #'stem #'% #'stem)) - (if (eq? (syntax->datum field-default) - 'undefined) - (configuration-no-default-value - '#,(id #'stem #'% #'stem) 'field) - field-default))) - (documentation doc)) - ...)) - (define-syntax-rule (stem arg (... ...)) - (let ((conf (#,(id #'stem #'% #'stem) arg (... ...)))) - (validate-configuration conf - #,(id #'stem #'stem #'-fields)) - conf))))))) + ((_ stem (field field-type+def doc custom-serializer ...) ...) + (with-syntax + ((((field-type def) ...) + (map normalize-field-type+def #'(field-type+def ...)))) + (with-syntax + (((field-getter ...) + (map (lambda (field) + (id #'stem #'stem #'- field)) + #'(field ...))) + ((field-predicate ...) + (map (lambda (type) + (id #'stem type #'?)) + #'(field-type ...))) + ((field-default ...) + (map (match-lambda + ((field-type default-value) + default-value)) + #'((field-type def) ...))) + ((field-serializer ...) + (map (lambda (type custom-serializer) + (and serialize? + (match custom-serializer + ((serializer) + serializer) + (() + (if serializer-prefix + (id #'stem + serializer-prefix + #'serialize- type) + (id #'stem #'serialize- type)))))) + #'(field-type ...) + #'((custom-serializer ...) ...)))) + #`(begin + (define-record-type* #,(id #'stem #'< #'stem #'>) + #,(id #'stem #'% #'stem) + #,(id #'stem #'make- #'stem) + #,(id #'stem #'stem #'?) + (%location #,(id #'stem #'stem #'-location) + (default (and=> (current-source-location) + source-properties->location)) + (innate)) + #,@(map (lambda (name getter def) + (if (eq? (syntax->datum def) (quote 'undefined)) + #`(#,name #,getter) + #`(#,name #,getter (default #,def)))) + #'(field ...) + #'(field-getter ...) + #'(field-default ...))) + (define #,(id #'stem #'stem #'-fields) + (list (configuration-field + (name 'field) + (type 'field-type) + (getter field-getter) + (predicate field-predicate) + (serializer field-serializer) + (default-value-thunk + (lambda () + (display '#,(id #'stem #'% #'stem)) + (if (eq? (syntax->datum field-default) + 'undefined) + (configuration-no-default-value + '#,(id #'stem #'% #'stem) 'field) + field-default))) + (documentation doc)) + ...)) + (define-syntax-rule (stem arg (... ...)) + (let ((conf (#,(id #'stem #'% #'stem) arg (... ...)))) + (validate-configuration conf + #,(id #'stem #'stem #'-fields)) + conf)))))))) (define no-serialization ;syntactic keyword for 'define-configuration' '(no serialization)) @@ -241,26 +254,26 @@ (define no-serialization ;syntactic keyword for 'define-configuration' (define-syntax define-configuration (lambda (s) (syntax-case s (no-serialization prefix) - ((_ stem (field (field-type def ...) doc custom-serializer ...) ... + ((_ stem (field field-type+def doc custom-serializer ...) ... (no-serialization)) (define-configuration-helper - #f #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 ...) ... + ((_ stem (field field-type+def doc custom-serializer ...) ... (prefix serializer-prefix)) (define-configuration-helper - #t #'serializer-prefix #'(_ stem (field (field-type def ...) + #t #'serializer-prefix #'(_ stem (field field-type+def doc custom-serializer ...) ...))) - ((_ stem (field (field-type def ...) doc custom-serializer ...) ...) + ((_ stem (field field-type+def doc custom-serializer ...) ...) (define-configuration-helper - #t #f #'(_ 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 - stem (field (field-type def ...) + stem (field field-type+def doc custom-serializer ...) ...) - (define-configuration stem (field (field-type def ...) + (define-configuration stem (field field-type+def doc custom-serializer ...) ... (no-serialization))) diff --git a/tests/services/configuration.scm b/tests/services/configuration.scm index 86a36a388d..0debf8095b 100644 --- a/tests/services/configuration.scm +++ b/tests/services/configuration.scm @@ -27,6 +27,9 @@ (define-module (tests services configuration) (test-begin "services-configuration") +(define (serialize-number field value) + (format #f "~a=~a" field value)) + ;;; ;;; define-configuration macro. @@ -47,7 +50,6 @@ (define-configuration port-configuration-cs 80 (port-configuration-cs-port (port-configuration-cs))) -(define serialize-number "") (define-configuration port-configuration-ndv (port (number) "The port number.")) @@ -101,15 +103,31 @@ (define-configuration configuration-with-prefix (define-maybe number) (define-configuration config-with-maybe-number - (port (maybe-number 80) "The port number.")) - -(define (serialize-number field value) - (format #f "~a=~a" field value)) + (port (maybe-number 80) "") + (count maybe-number "")) (test-equal "maybe value serialization" "port=80" (serialize-maybe-number "port" 80)) +(define (config-with-maybe-number->string x) + (eval (gexp->approximate-sexp + (serialize-configuration x config-with-maybe-number-fields)) + (current-module))) + +(test-equal "maybe value serialization of the instance" + "port=42count=43" + (config-with-maybe-number->string + (config-with-maybe-number + (port 42) + (count 43)))) + +(test-equal "maybe value serialization of the instance, unspecified" + "port=42" + (config-with-maybe-number->string + (config-with-maybe-number + (port 42)))) + (define-maybe/no-serialization string) (define-configuration config-with-maybe-string/no-serialization