diff mbox series

[bug#36948,1/2] import: utils: Add hash-ref*.

Message ID 20190806191728.22923-1-mail@cbaines.net
State Accepted
Headers show
Series Fix the CPAN importer | expand

Commit Message

Christopher Baines Aug. 6, 2019, 7:17 p.m. UTC
With the change to guile-json version 3, JSON objects are represented as hash
tables, rather than alists. The cpan importer uses assoc-ref* on a hash table,
so add an equivalent function for hash tables.

* guix/import/utils.scm (hash-ref*): New procedure.
---
 guix/import/utils.scm | 10 ++++++++++
 1 file changed, 10 insertions(+)

Comments

Ludovic Courtès Aug. 22, 2019, 12:04 p.m. UTC | #1
Hello Chris,

Christopher Baines <mail@cbaines.net> skribis:

> With the change to guile-json version 3, JSON objects are represented as hash
> tables, rather than alists. The cpan importer uses assoc-ref* on a hash table,
> so add an equivalent function for hash tables.
>
> * guix/import/utils.scm (hash-ref*): New procedure.

[...]

> In guile-json version 3, JSON objects are represented as hash tables, rather
> than alists.

I seems to me that this is adapting for Guile-JSON v1, not v3: in v3,
JSON arrays map to Scheme vectors, and JSON dictionaries map to alists;
JSON dictionaries used to map to hash tables in v1.

Indeed, ‘tests/cpan.scm’ now fails for me:

--8<---------------cut here---------------start------------->8---
actual-error:
+ (wrong-type-arg
+   "scm_hash_fn_get_handle"
+   "Wrong type argument in position ~A (expecting ~A): ~S"
+   (1
+    "hash-table"
+    (("version" . "0.1")
+     ("author" . "Guix")
+     ("download_url"
+      .
+      "http://example.com/Foo-Bar-0.1.tar.gz")
[…]
--8<---------------cut here---------------end--------------->8---

Could it be that you were testing this in an environment containing v1
and not v3?

Sorry for not noticing earlier!

Thanks,
Ludo’.
diff mbox series

Patch

diff --git a/guix/import/utils.scm b/guix/import/utils.scm
index 2a3b7341fb..ed6c3ce6af 100644
--- a/guix/import/utils.scm
+++ b/guix/import/utils.scm
@@ -47,6 +47,7 @@ 
 
             flatten
             assoc-ref*
+            hash-ref*
 
             url-fetch
             guix-hash-url
@@ -116,6 +117,15 @@  recursively apply the procedure to the sub-list."
       (assoc-ref alist key)
       (apply assoc-ref* (assoc-ref alist key) rest)))
 
+(define (hash-ref* hash-table key . rest)
+  "Return the value for KEY from HASH-TABLE.  For each additional key specified,
+recursively apply the procedure to the sub-hash-table."
+  (if (hash-table? hash-table)
+      (if (null? rest)
+          (hash-ref hash-table key)
+          (apply hash-ref* (hash-ref hash-table key) rest))
+      #f))                              ; For consistency with assoc-ref*
+
 (define (url-fetch url file-name)
   "Save the contents of URL to FILE-NAME.  Return #f on failure."
   (parameterize ((current-output-port (current-error-port)))