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.
This commit is contained in:
parent
b7b8828801
commit
c8772a7a21
|
@ -198,9 +198,19 @@ thunked fields."
|
||||||
#'((field options ...)
|
#'((field options ...)
|
||||||
...))))))))))
|
...))))))))))
|
||||||
|
|
||||||
(define (alist->record alist make keys)
|
(define* (alist->record alist make keys
|
||||||
"Apply MAKE to the values associated with KEYS in ALIST."
|
#:optional (multiple-value-keys '()))
|
||||||
(let ((args (map (cut assoc-ref alist <>) keys)))
|
"Apply MAKE to the values associated with KEYS in ALIST. Items in KEYS that
|
||||||
|
are also in MULTIPLE-VALUE-KEYS are considered to occur possibly multiple
|
||||||
|
times in ALIST, and thus their value is a list."
|
||||||
|
(let ((args (map (lambda (key)
|
||||||
|
(if (member key multiple-value-keys)
|
||||||
|
(filter-map (match-lambda
|
||||||
|
((k . v)
|
||||||
|
(and (equal? k key) v)))
|
||||||
|
alist)
|
||||||
|
(assoc-ref alist key)))
|
||||||
|
keys)))
|
||||||
(apply make args)))
|
(apply make args)))
|
||||||
|
|
||||||
(define (object->fields object fields port)
|
(define (object->fields object fields port)
|
||||||
|
|
|
@ -158,6 +158,12 @@ Version: 1.5
|
||||||
(list (recutils->alist p)
|
(list (recutils->alist p)
|
||||||
(recutils->alist p))))
|
(recutils->alist p))))
|
||||||
|
|
||||||
|
(test-equal "alist->record" '((1 2) b c)
|
||||||
|
(alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))
|
||||||
|
list
|
||||||
|
'("a" "b" "c")
|
||||||
|
'("a")))
|
||||||
|
|
||||||
(test-end)
|
(test-end)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue