From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 6Cj2EY//TmL1NAEAgWs5BA (envelope-from ) for ; Thu, 07 Apr 2022 17:13:19 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id yMKODo//TmLCawAAauVa8A (envelope-from ) for ; Thu, 07 Apr 2022 17:13:19 +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 B12882C707 for ; Thu, 7 Apr 2022 17:13:18 +0200 (CEST) Received: from localhost ([::1]:53694 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ncToy-0001rx-LD for larch@yhetil.org; Thu, 07 Apr 2022 11:13:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47566) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ncTol-0001rc-2P for guix-patches@gnu.org; Thu, 07 Apr 2022 11:13:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:39240) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ncTok-00014N-Pd for guix-patches@gnu.org; Thu, 07 Apr 2022 11:13:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ncTok-0007xx-IF for guix-patches@gnu.org; Thu, 07 Apr 2022 11:13:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54674] [PATCH v3 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: Thu, 07 Apr 2022 15:13: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.164934432730537 (code B ref 54674); Thu, 07 Apr 2022 15:13:02 +0000 Received: (at 54674) by debbugs.gnu.org; 7 Apr 2022 15:12:07 +0000 Received: from localhost ([127.0.0.1]:33134 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ncTnn-0007wO-IH for submit@debbugs.gnu.org; Thu, 07 Apr 2022 11:12:07 -0400 Received: from mail-ej1-f44.google.com ([209.85.218.44]:38726) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ncTnj-0007vp-3T for 54674@debbugs.gnu.org; Thu, 07 Apr 2022 11:12:03 -0400 Received: by mail-ej1-f44.google.com with SMTP id r13so11528902ejd.5 for <54674@debbugs.gnu.org>; Thu, 07 Apr 2022 08:11:59 -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=YKMe6l2LFyryuc59w6VA3CAXuv0hy3RynD8elKpJdIs=; b=UsPhagrvZ1uhEzXkY3DNAZ3SfcGpZTeQmMXXoJzO2lNnjNGgT1vnyjouu8X2R2qdX7 a0hUA6DtvmpDDh9VeNO+ikgMu9yR4UO/5YXf8v9bkv9cjfO1F3k1O+PlfoSQ67P3LsWS LfVK/jVjOeavzMC/ILGWuB3PQ/sj4e2qYteorkuqpOa7XiEaWzu3GxUgFIA0qwmIFmVY gJHos8vgQqcr/f5BmjQ+NLtqAOo9gyM3kxR1Kd16dczC10FFtYubOhjYUsx9MAnT7YEA vbc7fjx9UlWZdHKLkB+ehgkm2IcdoGIGzzGLX1eclEY9U+dH3jBkwj4v2CmKDedaNKSw xPjA== 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=YKMe6l2LFyryuc59w6VA3CAXuv0hy3RynD8elKpJdIs=; b=8Ogar/dRuckVcKgHemsZaMhGfoYar6Dz6DyCx6UmfdboHt0h487peIte+LTibhFVeT +GsAcjBpd/aE9iZkwIuUhl7FciV7nms2jkyc5xDxBxcgjlUAVP08hlYRMd+9S286/ahD 8TRwMuPOP2CdCB2JsFwIYriji6pnyMD51z2AaDC+16+L5NcpaxJMPgh/ecx0I17nFdMs 67MFB6Jw22fdeqzr1Rf9XR0trgobxeSfmtuGbU9KwIWHe5jyCmCQaGUNMdPQzIIN8E4/ ShryYCwmLdEbYy5zgSINHHbYF1pAoPFHQUeWArioUS5TrqehUFC5IwAYJ6aeLP0+yTsC YXgg== X-Gm-Message-State: AOAM530Ba82YrJNcgUnt5LGTHbMrbjGa22GYqyFRv9zuXZlV2+YsvYTs WSsPO9dEi4bSI6VomW/9Rho96OOXTiE= X-Google-Smtp-Source: ABdhPJwioe6x0k9ZA4ehXNDxIX3jVHEoO/4NgevL/XXEUjAYBBxs45XmX8mhacuVGBKhGVVMm/nnPg== X-Received: by 2002:a17:907:2d90:b0:6db:729e:7f25 with SMTP id gt16-20020a1709072d9000b006db729e7f25mr14080528ejc.203.1649344312854; Thu, 07 Apr 2022 08:11:52 -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 h24-20020a170906719800b006e82f0e4060sm956632ejk.205.2022.04.07.08.11.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 08:11:52 -0700 (PDT) From: Attila Lendvai Date: Thu, 7 Apr 2022 17:01:40 +0200 Message-Id: <20220407150140.32738-1-attila@lendvai.name> X-Mailer: git-send-email 2.34.0 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=1649344399; 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=KnNwe/Sa1Vkllb53/p6A+n18GyrOeN7sv9KHngXABrw=; b=DWOnTr0Bx8zgYKARmpcQD4F6E0ULcsSYFTRLFv3oxY8pUjlhyCu7pG18TAbbK/Azwh0R/l ROydWO6CQGW7JS1lNCr5ZyeFlpZclfVwjzJIT/P1xLOqzXn884L0F6d696ub2EunXsCJ+E DYRzPdz0H5YEbFiW9rXMmSpCE0ZXygBgS3B/GA3mfQqYyKR1heRmyrF9VYTkJnOdrKwRrK t6bbKGoqOP6Cb0wUJ0JwzjZQXLmqq+t0Satx+gcOu4TR57VaYdyF0TAYVp7oHt9PiG/ivE jXujMSoLsfzTBcjThlqAZgLXt9luC1piQvclQr0XNtWaZPlZE3GWLRfrVCuxsw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1649344399; a=rsa-sha256; cv=none; b=ZkAjEistYBLYOhU6qZ4ssxZhY8IJrHkVBkF6T3wIBuoFr4lPIKXOdPMpW/+if/1DI8MVRP X23X7fJFFNBkdftOb3YBp1QYLnVCRXHh+u72b6+wwC9HUz/C8GWIFesVbPRKtZMzOrTGL/ HOXmFOwzNCfWrhytYWAdCdW9cftBEXCrQWeBgLOStA0hZ0PRhN7vh+joQxE70m0obmnsFT aTV+rxyZDzGXUbduM+N+gWxMcc4bJQ1iEFDX0pkTKIG1XJ7hvcA0TEtCpiEtwfLx3iUhyd gc2Ja3oIc2VcQ6EvNmTH/3MqtPfr/fPZzM7TLYf2DyV/AzfAbhcNrFkqse3ffQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20210112 header.b=UsPhagrv; 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.14 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20210112 header.b=UsPhagrv; 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: B12882C707 X-Spam-Score: 1.14 X-Migadu-Scanner: scn0.migadu.com X-TUID: OpTT3lfarF2G 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. --- v3: clean up the commit message. the code was not changed. note that a large part of the diff is whitespace only. 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.34.0