From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id 4GptAf3PX2JaRgEAbAwnHQ (envelope-from ) for ; Wed, 20 Apr 2022 11:18:53 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id CNqQAP3PX2KhMQAAG6o9tA (envelope-from ) for ; Wed, 20 Apr 2022 11:18:53 +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 814776F79 for ; Wed, 20 Apr 2022 11:18:52 +0200 (CEST) Received: from localhost ([::1]:38134 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nh6U6-0002GX-6q for larch@yhetil.org; Wed, 20 Apr 2022 05:18:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45680) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nh6TK-00023j-Q9 for guix-patches@gnu.org; Wed, 20 Apr 2022 05:18:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:51181) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nh6TK-0003pK-El for guix-patches@gnu.org; Wed, 20 Apr 2022 05:18:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nh6TK-0006ZK-93 for guix-patches@gnu.org; Wed, 20 Apr 2022 05:18:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54674] [PATCH v4 1/2] 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: Wed, 20 Apr 2022 09:18:02 +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.165044624625167 (code B ref 54674); Wed, 20 Apr 2022 09:18:02 +0000 Received: (at 54674) by debbugs.gnu.org; 20 Apr 2022 09:17:26 +0000 Received: from localhost ([127.0.0.1]:45075 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nh6Sj-0006Xq-Hc for submit@debbugs.gnu.org; Wed, 20 Apr 2022 05:17:26 -0400 Received: from mail-ej1-f53.google.com ([209.85.218.53]:38466) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nh6Sh-0006XZ-Ne for 54674@debbugs.gnu.org; Wed, 20 Apr 2022 05:17:24 -0400 Received: by mail-ej1-f53.google.com with SMTP id r13so2210245ejd.5 for <54674@debbugs.gnu.org>; Wed, 20 Apr 2022 02:17:23 -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=+iyFTRtqAtXkgYiGtD4uae6oIfGkmk+wBlwrs0+AIjM=; b=KMuRhwgYyduIoiobXGYuRX5dNaZxtSRVs3rYvJgldzS6nKzbi4fg03W6T95n9KHsXZ s3cOFkIVQBvFyagkzddoPDHrhdVhBRYklv1lsB59co8e9Ubqvp1U5JKTShvIyux19UEb g17JIXXV1FGxoRX9gjLRV7c3pkWFQ5MB6U17/F0lfkweQMBJmL6lejY/ak3b9hb1xS43 O05N0teCWEnWgm9G1I/BhVr1rOK3e+R2+ZOimtbhJpEYP7DnDiVTFExJpa7EL2J51Xv9 sBU9780oMKozxh2a1xh4AjYf4Ep9wcYYZu8UhkwcY2oWaYtsBYxBWyZUOub9cBuYIbPI AdZA== 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=+iyFTRtqAtXkgYiGtD4uae6oIfGkmk+wBlwrs0+AIjM=; b=4Vih30GhNSAU8NjIVoUguz0fElhCSqjsJ1ttibcevyqtmW+2tl53S+COwPi61d1yza u4un82e2D5i21NPJVXETwq7FESpCJmoPVUuP4HxofhBUbM9ocfnLsQcefisMugmT0vUR TQk4vthU+xgxeWZ+SB1DD4TpIeF5ykRrPsRO6YcovFCf9/YKw0K8UDRDPDH1/9Rplalx PgBQpDdMxVgn3FbOrHt41ShwinQ/frAbBPA5yuDTWHUII5RrIDUDUIAC3MT05Dt/BDRI ds0GdjMGNqNj4/CZS5Qbt6GQzsAH9K4P8fL/QAqSPeBviCqKHliNBJybPFR7IBLBQwwB 5zRw== X-Gm-Message-State: AOAM533KxeNUuPh0BsBHZZITzS2+1hMrpK0fSIehDzMEE7UHwQJQeHnj meENqQ/OYsNPG9+C8uLkmYhlU8Ne1a0= X-Google-Smtp-Source: ABdhPJydOQeH8KZol5cGh0hWRFIJ00z1vvyg3htviWKqcVe2ZWIZp1wvzpdoHywCQ5rhX79T/fbyug== X-Received: by 2002:a17:906:7948:b0:6da:64ed:178e with SMTP id l8-20020a170906794800b006da64ed178emr18012513ejo.523.1650446237677; Wed, 20 Apr 2022 02:17:17 -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 n25-20020aa7db59000000b00415965e9727sm9598101edt.18.2022.04.20.02.17.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 02:17:17 -0700 (PDT) From: Attila Lendvai Date: Wed, 20 Apr 2022 11:15:53 +0200 Message-Id: <20220420091553.26732-1-attila@lendvai.name> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1650446332; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: 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:dkim-signature; bh=41VV5R/MT4L7BT3fEIi0DfdZ8VbrZ+Fg46NMZE8lpG0=; b=qly4N323GBI6NzLJXH7FS5Go5EFNFajiICXul/CWUpKni0fycPWAOuK7uo/EkNSkOoLLj4 7xp9O17qVq8S8G4WYGtETYjy6lZvNOuqmYJkYglsmfu5JyuxqYnApfP0Y6rwXZ/Vd/71mq 0DMzpfq4mmsJC0UBTvSfpGn1e34la0AFsm4Gfp4YEphG+S/TQooZF7+KAtByoOBvzeMCRF StG7LBy24X9t9ud2G4J9dHei6CGcVFpjlLzS9+E+tLMwXHuTz82ohvkpNTFiUrju6SJeEh agtIuuDSdD/1tzv0hlyjMxh/aFakTrMQMOWl+eDs0tBalx0+4NyX+/L7tmnHMg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1650446332; a=rsa-sha256; cv=none; b=mJREmIP+fIhv34mIfddTGddcm/TfPYxLws2ad/WKAXIOhQSxSZKx52f7pqtu+cnWLjFk1k UmpbwSwq7du7lCX/aVAQL3tisEg3VKfYItiLjsRzJ+wi5lOsSKUHC7rHZJqQijb4gbqvfl 5IgTkBUb1Tngd/VIJenIGr4o8HHHqGDaamNrp7v/v/vFxw1DxKsFJ9rxTiTNK2DqduLgSg rdymmeOk3dXGtredmuRx5n62IjjUfeMRAj0AXk1Y/FbrtTZDYTnfIYGG83Pvtv4eV3cHoG AVUHTFAod0ZVwZB75EqVU3hMapb6U3qOzsepTWsTZgzMcggS1FfiY+HwQC43tg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20210112 header.b=KMuRhwgY; 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-Spam-Score: 1.96 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20210112 header.b=KMuRhwgY; 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: 814776F79 X-Spam-Score: 1.96 X-Migadu-Scanner: scn1.migadu.com X-TUID: yfNF6ov0vynh 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. --- v4: the only change is to drop the extra parens around the type in all the (field1 (maybe-foo) "") forms. 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 -- 2.35.1