From ab20071d828c6541cebb233954cd748b32b1f2ba Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Mon, 2 Dec 2019 08:26:01 +0800 Subject: [PATCH] [report-utilities] strify hash-table to Hash(kvp-list) Hash tables are strified to "Hash()" "Hash(key=value,...)" --- .../report/report-system/report-utilities.scm | 21 ++++++++++++------- .../test/test-report-utilities.scm | 10 +++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/gnucash/report/report-system/report-utilities.scm b/gnucash/report/report-system/report-utilities.scm index 75b5afb061..459609e9be 100644 --- a/gnucash/report/report-system/report-utilities.scm +++ b/gnucash/report/report-system/report-utilities.scm @@ -1259,6 +1259,18 @@ flawed. see report-utilities.scm. please update reports.") (gnc-lot-get-notes lot) (gnc-lot-get-balance lot) (gnc-lot-count-splits lot))) + (define (record->str rec) + (let ((rtd (record-type-descriptor rec))) + (define (fld->str fld) + (format #f "~a=~a" fld (gnc:strify ((record-accessor rtd fld) rec)))) + (format #f "Rec:~a{~a}" + (record-type-name rtd) + (string-join (map fld->str (record-type-fields rtd)) ", ")))) + (define (hash-table->str hash) + (string-append + "Hash(" (string-join + (hash-map->list (lambda (k v) (format #f "~a=~a" k v)) hash) ",") + ")")) (define (try proc) ;; Try proc with d as a parameter, catching exceptions to return ;; #f to the (or) evaluator below. @@ -1294,13 +1306,8 @@ flawed. see report-utilities.scm. please update reports.") (try owner->str) (try invoice->str) (try lot->str) - (and (record? d) - (let ((rtd (record-type-descriptor d))) - (define (fld->str fld) - (format #f "~a=~a" fld (gnc:strify ((record-accessor rtd fld) d)))) - (format #f "Rec:~a{~a}" - (record-type-name rtd) - (string-join (map fld->str (record-type-fields rtd)) ", ")))) + (try hash-table->str) + (try record->str) (object->string d))) (define (pair->num pair) diff --git a/gnucash/report/report-system/test/test-report-utilities.scm b/gnucash/report/report-system/test/test-report-utilities.scm index b0f12da769..13921d47c9 100644 --- a/gnucash/report/report-system/test/test-report-utilities.scm +++ b/gnucash/report/report-system/test/test-report-utilities.scm @@ -152,6 +152,16 @@ (test-equal "gnc:strify " "coll<10>" (gnc:strify coll))) + + (let ((ht (make-hash-table))) + (test-equal "gnc:strify Hash()" + "Hash()" + (gnc:strify ht)) + (hash-set! ht 'one "uno") + (test-equal "gnc:strify Hash(one=uno)" + "Hash(one=uno)" + (gnc:strify ht))) + (test-end "debugging tools")) (define (test-commodity-collector)