summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2013-07-10 18:04:08 +0200
committerLudovic Courtès <ludo@gnu.org>2013-07-10 21:52:51 +0200
commitc8772a7a21f954b5e75746529e70edc3a1017249 (patch)
tree03792f0a3dd41d5af5d4bf833bf6bfcebb992ae8
parentb7b88288011aa41791b6634ae229f426bacc55ce (diff)
records: `alist->record' supports multiple-field occurrences.
* guix/records.scm (alist->record): Add `multiple-value-keys' parameter. Update docstring, and honor it. * tests/records.scm ("alist->record"): New record.
-rw-r--r--guix/records.scm16
-rw-r--r--tests/records.scm6
2 files changed, 19 insertions, 3 deletions
diff --git a/guix/records.scm b/guix/records.scm
index 57664df5a61..8dc733b8ff5 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -198,9 +198,19 @@ thunked fields."
198 #'((field options ...) 198 #'((field options ...)
199 ...)))))))))) 199 ...))))))))))
200 200
201(define (alist->record alist make keys) 201(define* (alist->record alist make keys
202 "Apply MAKE to the values associated with KEYS in ALIST." 202 #:optional (multiple-value-keys '()))
203 (let ((args (map (cut assoc-ref alist <>) keys))) 203 "Apply MAKE to the values associated with KEYS in ALIST. Items in KEYS that
204are also in MULTIPLE-VALUE-KEYS are considered to occur possibly multiple
205times in ALIST, and thus their value is a list."
206 (let ((args (map (lambda (key)
207 (if (member key multiple-value-keys)
208 (filter-map (match-lambda
209 ((k . v)
210 (and (equal? k key) v)))
211 alist)
212 (assoc-ref alist key)))
213 keys)))
204 (apply make args))) 214 (apply make args)))
205 215
206(define (object->fields object fields port) 216(define (object->fields object fields port)
diff --git a/tests/records.scm b/tests/records.scm
index d0635ebb1f1..712eb83a096 100644
--- a/tests/records.scm
+++ b/tests/records.scm
@@ -158,6 +158,12 @@ Version: 1.5
158 (list (recutils->alist p) 158 (list (recutils->alist p)
159 (recutils->alist p)))) 159 (recutils->alist p))))
160 160
161(test-equal "alist->record" '((1 2) b c)
162 (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))
163 list
164 '("a" "b" "c")
165 '("a")))
166
161(test-end) 167(test-end)
162 168
163 169