On Mon, 19 Apr 2021 09:26:13 +0100
Christopher Baines <m...@cbaines.net> wrote:

> As an example, if you have this structure.
> 
>   '((foo . 1)
>     (bar . 2))
> 
> and you want the numbers to be strings, you can do something like:
> 
>   (map (match-lambda
>         ((name . number)
>          (cons name (number->string number))))
>        data)
> 
> and I think a similar approach for the transformation you're trying to
> perform to the arguments will more cleanly represent what you're
> trying to do.

It helped a lot!

> One different thing I noticed:
> 
>   ,@(if (not (string? hash-alg))
>         '()
>         `((hash-algorithm . ,hash-alg)))
> 
> I'd suggest simplifying this by flipping the different parts of the
> if, and removing the not.

Done.

-- 
Best Regards,

Luciana Lima Brito
MSc. in Computer Science
>From ce5d97dd490f6ffb58548e83be78af576404b01f Mon Sep 17 00:00:00 2001
From: Luciana Brito <lubr...@posteo.net>
Date: Sun, 11 Apr 2021 11:06:06 -0300
Subject: [PATCH] Implement basic json output for the derivation comparison
 page

---
 guix-data-service/web/compare/controller.scm | 85 +++++++++++++++++++-
 1 file changed, 82 insertions(+), 3 deletions(-)

diff --git a/guix-data-service/web/compare/controller.scm b/guix-data-service/web/compare/controller.scm
index a6aa198..85fcbe9 100644
--- a/guix-data-service/web/compare/controller.scm
+++ b/guix-data-service/web/compare/controller.scm
@@ -588,9 +588,88 @@
                  '(application/json text/html)
                  mime-types)
             ((application/json)
-             (render-json
-              '((error . "unimplemented")) ; TODO
-              #:extra-headers http-headers-for-unchanging-content))
+             (let ((outputs
+                    (map
+                     (lambda (label items)
+                       (cons label
+                             (list->vector
+                              (map
+                               (match-lambda
+                                 ((name path hash-alg hash recursive)
+                                  `((name . ,name)
+                                    (path . ,path)
+                                    ,@(if (string? hash-alg)
+                                          `((hash-algorithm . ,hash-alg))
+                                          '())
+                                    ,@(if (string? hash)
+                                          `((hash . ,hash))
+                                          '())
+                                    (recursive . ,(string=? recursive "t")))))
+                               (or items '())))))
+                     '(base target common)
+                     (let ((output-groups (assq-ref data 'outputs)))
+                       (list (assq-ref output-groups 'base)
+                             (assq-ref output-groups 'target)
+                             (assq-ref output-groups 'common)))))
+
+                   (inputs
+                    (map
+                     (lambda (label items)
+                       (cons label
+                             (list->vector
+                              (map 
+                               (match-lambda
+                                 ((derivation output)
+                                  `((derivation . ,derivation)
+                                    (output . ,output))))
+                               (or items '())))))
+                     '(base target common)
+                     (let ((input-groups (assq-ref data 'inputs)))
+                       (list (assq-ref input-groups 'base)
+                             (assq-ref input-groups 'target)
+                             (assq-ref input-groups 'common)))))
+
+                   (sources
+                    (map
+                     (lambda (label items)
+                       (cons label
+                             (list->vector
+                              (map
+                               (match-lambda
+                                 ((derivation)
+                                  `((derivation . ,derivation))))
+                               (or items '())))))
+                     '(base target common)
+                     (let ((source-groups (assq-ref data 'sources)))
+                       (list (assq-ref source-groups 'base)
+                             (assq-ref source-groups 'target)
+                             (assq-ref source-groups 'common)))))
+
+                   (arguments
+                    (map
+                     (match-lambda
+                       ((label args ...)
+                        `(,label . ,(list->vector args))))
+                     (assq-ref data 'arguments))))
+               
+               (render-json
+                `((base
+                   . ((derivation . ,base-derivation)))
+                  (target
+                   . ((derivation . ,target-derivation)))
+                  (outputs
+                   . ,outputs)
+                  (inputs
+                   . ,inputs)
+                  (sources                   
+                   . ,sources)
+                  (system
+                   . ,(assq-ref data 'system))
+                  (builder
+                   . ,(assq-ref data 'builder))
+                  (arguments . ,arguments)
+                  (environment-variables . ,(assq-ref data 'environment-variables)))
+                #:extra-headers http-headers-for-unchanging-content)))
             (else
              (render-html
               #:sxml (compare/derivation
-- 
2.30.2

Reply via email to