From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 0AXQDs3Y3GB/YgEAgWs5BA (envelope-from ) for ; Wed, 30 Jun 2021 22:49:17 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id SGNtCs3Y3GA3bwAAbx9fmQ (envelope-from ) for ; Wed, 30 Jun 2021 20:49:17 +0000 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 659891C462 for ; Wed, 30 Jun 2021 22:49:16 +0200 (CEST) Received: from localhost ([::1]:52736 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lyh91-0000TL-Aw for larch@yhetil.org; Wed, 30 Jun 2021 16:49:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45716) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lyh8p-0000Qo-NW for guix-patches@gnu.org; Wed, 30 Jun 2021 16:49:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:47373) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lyh8p-00014k-9e for guix-patches@gnu.org; Wed, 30 Jun 2021 16:49:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lyh8p-00057n-7Y for guix-patches@gnu.org; Wed, 30 Jun 2021 16:49:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49169] [PATCH v2 01/16] records: Support field sanitizers. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 30 Jun 2021 20:49:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49169 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 49169@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 49169-submit@debbugs.gnu.org id=B49169.162508614019652 (code B ref 49169); Wed, 30 Jun 2021 20:49:03 +0000 Received: (at 49169) by debbugs.gnu.org; 30 Jun 2021 20:49:00 +0000 Received: from localhost ([127.0.0.1]:58911 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lyh8l-00056f-EM for submit@debbugs.gnu.org; Wed, 30 Jun 2021 16:49:00 -0400 Received: from eggs.gnu.org ([209.51.188.92]:47856) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lyh8j-00055e-9S for 49169@debbugs.gnu.org; Wed, 30 Jun 2021 16:48:57 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:53120) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lyh8d-0000vF-NI; Wed, 30 Jun 2021 16:48:51 -0400 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=42586 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lyh8d-0001jQ-FW; Wed, 30 Jun 2021 16:48:51 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Wed, 30 Jun 2021 22:48:17 +0200 Message-Id: <20210630204832.25753-2-ludo@gnu.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210630204832.25753-1-ludo@gnu.org> References: <20210622090221.15182-1-ludo@gnu.org> <20210630204832.25753-1-ludo@gnu.org> 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1625086156; 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; bh=bXKFYM508wCg6pLdIKra0WbylriCBFVMwayb3wYUHm8=; b=Mow419c3J4hvhX3IkNT/J2oXascAtSVLwQy2ea+r1q/SUxlVCpdNkX4nFPh8LF67i++QRL h8Csbze20QTx65G0NCoH76VgFrWeiKJnGTqCwFHQsqrHndCDKEc8wQeSpqpKHoA0Q86n3m oc9bZf9Z/TV/+aj9ZiTfcD7WJXFFfWIjRi2I+lmfxglRV1uu8wcN7yYNCb0JoGza6i/Mfh UzNe0BkSE2lMqZcQh/JbQHpi8BB+mSRygMHtnC+JxUqlOTIyxa7CKh6YLqAcz6fwCydWXG Lz6XRy6ep0IuASPxY/bhrqnNn7iFoFklMhjXYGdRXeKzfGRyaMyUuBpLc5P/9Q== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1625086156; a=rsa-sha256; cv=none; b=bVviH76ItxI/au72SpQTwAL+HnoxlNyUC78YO+MNZ+LcYa6ImLI1wkYWzhmp63OnYlLV33 UGi53NJb0m7M+/x7n+Hsti8T5X4rRbV6fjXcCI/N00xkbKPAPgCp/8xnUCQvuccOHNw4Jc dvgdUrBCy4axauMZMODo28syd+kOcTS8DRZeFtOgL/fRhNQWzSJ4PufOFZeSgNCm6aCBUW OS/GIZ03jT5dq3vuf/PbCXuu9QQ9vH3730DU2o118jHhsUyKseIwZZxy6t+Bh/E570+hbA UDBCCTWYqDIqDszlTB+2lSQRWIbUA93AJdk0FWP+R3W9voNOzJaRLAyminPkww== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: -0.42 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 659891C462 X-Spam-Score: -0.42 X-Migadu-Scanner: scn0.migadu.com X-TUID: +QiEfuqzJObh * guix/records.scm (make-syntactic-constructor): Add #:sanitizers. [field-sanitizer]: New procedure. [wrap-field-value]: Honor F's sanitizer. (define-record-type*)[field-sanitizer]: New procedure. Pass #:sanitizer to 'make-syntactic-constructor'. * tests/records.scm ("define-record-type* & sanitize") ("define-record-type* & sanitize & thunked"): New tests. --- guix/records.scm | 65 +++++++++++++++++++++++++++++++++++++---------- tests/records.scm | 38 +++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 14 deletions(-) diff --git a/guix/records.scm b/guix/records.scm index 3d54a51956..ed94c83dac 100644 --- a/guix/records.scm +++ b/guix/records.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès ;;; Copyright © 2018 Mark H Weaver ;;; ;;; This file is part of GNU Guix. @@ -120,7 +120,8 @@ context of the definition of a thunked field." "Make the syntactic constructor NAME for TYPE, that calls CTOR, and expects all of EXPECTED fields to be initialized. DEFAULTS is the list of FIELD/DEFAULT-VALUE tuples, THUNKED is the list of identifiers of thunked -fields, and DELAYED is the list of identifiers of delayed fields. +fields, DELAYED is the list of identifiers of delayed fields, and SANITIZERS +is the list of FIELD/SANITIZER tuples. ABI-COOKIE is the cookie (an integer) against which to check the run-time ABI of TYPE matches the expansion-time ABI." @@ -130,6 +131,7 @@ of TYPE matches the expansion-time ABI." #:this-identifier this-identifier #:delayed delayed #:innate innate + #:sanitizers sanitizers #:defaults defaults) (define-syntax name (lambda (s) @@ -169,19 +171,30 @@ of TYPE matches the expansion-time ABI." (define (innate-field? f) (memq (syntax->datum f) 'innate)) + (define field-sanitizer + (let ((lst (map (match-lambda + ((f p) + (list (syntax->datum f) p))) + #'sanitizers))) + (lambda (f) + (or (and=> (assoc-ref lst (syntax->datum f)) car) + #'(lambda (x) x))))) + (define (wrap-field-value f value) - (cond ((thunked-field? f) - #`(lambda (x) - (syntax-parameterize ((#,this-identifier - (lambda (s) - (syntax-case s () - (id - (identifier? #'id) - #'x))))) - #,value))) - ((delayed-field? f) - #`(delay #,value)) - (else value))) + (let* ((sanitizer (field-sanitizer f)) + (value #`(#,sanitizer #,value))) + (cond ((thunked-field? f) + #`(lambda (x) + (syntax-parameterize ((#,this-identifier + (lambda (s) + (syntax-case s () + (id + (identifier? #'id) + #'x))))) + #,value))) + ((delayed-field? f) + #`(delay #,value)) + (else value)))) (define default-values ;; List of symbol/value tuples. @@ -291,6 +304,19 @@ can access the record it belongs to via the 'this-thing' identifier. A field can also be marked as \"delayed\" instead of \"thunked\", in which case its value is effectively wrapped in a (delay …) form. +A field can also have an associated \"sanitizer\", which is a procedure that +takes a user-supplied field value and returns a \"sanitized\" value for the +field: + + (define-record-type* thing make-thing + thing? + this-thing + (name thing-name + (sanitize (lambda (value) + (cond ((string? value) value) + ((symbol? value) (symbol->string value)) + (else (throw 'bad! value))))))) + It is possible to copy an object 'x' created with 'thing' like this: (thing (inherit x) (name \"bar\")) @@ -307,6 +333,14 @@ inherited." (field-default-value #'(field properties ...))) (_ #f))) + (define (field-sanitizer s) + (syntax-case s (sanitize) + ((field (sanitize proc) _ ...) + (list #'field #'proc)) + ((field _ properties ...) + (field-sanitizer #'(field properties ...))) + (_ #f))) + (define-field-property-predicate delayed-field? delayed) (define-field-property-predicate thunked-field? thunked) (define-field-property-predicate innate-field? innate) @@ -376,6 +410,8 @@ inherited." (innate (filter-map innate-field? field-spec)) (defaults (filter-map field-default-value #'((field properties ...) ...))) + (sanitizers (filter-map field-sanitizer + #'((field properties ...) ...))) (cookie (compute-abi-cookie field-spec))) (with-syntax (((field-spec* ...) (map field-spec->srfi-9 field-spec)) @@ -421,6 +457,7 @@ of a record instantiation" #:this-identifier #'this-identifier #:delayed #,delayed #:innate #,innate + #:sanitizers #,sanitizers #:defaults #,defaults))))) ((_ type syntactic-ctor ctor pred (field get properties ...) ...) diff --git a/tests/records.scm b/tests/records.scm index 706bb3dbfd..d014e7a995 100644 --- a/tests/records.scm +++ b/tests/records.scm @@ -283,6 +283,44 @@ (equal? (foo-bar y) 1)) ;promise was already forced (eq? (foo-baz y) 'b))))) +(test-assert "define-record-type* & sanitize" + (begin + (define-record-type* foo make-foo + foo? + (bar foo-bar + (default "bar") + (sanitize (lambda (x) (string-append x "!"))))) + + (let* ((p (foo)) + (q (foo (inherit p))) + (r (foo (inherit p) (bar "baz"))) + (s (foo (bar "baz")))) + (and (string=? (foo-bar p) "bar!") + (equal? q p) + (string=? (foo-bar r) "baz!") + (equal? s r))))) + +(test-assert "define-record-type* & sanitize & thunked" + (let ((sanitized 0)) + (define-record-type* foo make-foo + foo? + (bar foo-bar + (default "bar") + (sanitize (lambda (x) + (set! sanitized (+ 1 sanitized)) + (string-append x "!"))))) + + (let ((p (foo))) + (and (string=? (foo-bar p) "bar!") + (string=? (foo-bar p) "bar!") ;twice + (= sanitized 1) ;sanitizer was called at init time only + (let ((q (foo (bar "baz")))) + (and (string=? (foo-bar q) "baz!") + (string=? (foo-bar q) "baz!") ;twice + (= sanitized 2) + (let ((r (foo (inherit q)))) + (and (string=? (foo-bar r) "baz!") + (= sanitized 2))))))))) ;no re-sanitization (test-assert "define-record-type* & wrong field specifier" (let ((exp '(begin (define-record-type* foo make-foo -- 2.32.0