On Sat, 17 Apr 2021 14:11:37 +0100
Christopher Baines <[email protected]> wrote:

Hi,

> I think that's better, but you're still taking the matched-outputs,
> matched-inputs and matched-sources alists, then later making the
> values vectors rather than lists. The code would be even simpler if
> you converted to a vector just after the map that produces the
> relevant lists.
> 
> I haven't looked to closely at the rest, but it looks like the
> environment-variables binding is unnecessary, given how simple the bit
> of code used is, it can just be inlined in the one place where the
> binding is used.

Done! :)
I even simplified some other stuff, based on what you said.

-- 
Best Regards,

Luciana Lima Brito
MSc. in Computer Science
>From 701c89e8f039a6bc7d9b616acded54eac26fb0a7 Mon Sep 17 00:00:00 2001
From: Luciana Brito <[email protected]>
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 | 100 ++++++++++++++++++-
 1 file changed, 97 insertions(+), 3 deletions(-)

diff --git a/guix-data-service/web/compare/controller.scm b/guix-data-service/web/compare/controller.scm
index a6aa198..14a25ee 100644
--- a/guix-data-service/web/compare/controller.scm
+++ b/guix-data-service/web/compare/controller.scm
@@ -588,9 +588,103 @@
                  '(application/json text/html)
                  mime-types)
             ((application/json)
-             (render-json
-              '((error . "unimplemented")) ; TODO
-              #:extra-headers http-headers-for-unchanging-content))
+             (let* ((outputs (assq-ref data 'outputs))
+                    (matched-outputs
+                     (map
+                      (lambda (label items)
+                        (cons label
+                              (list->vector
+                               (map
+                                (match-lambda
+                                  ((name path hash-alg hash recursive)
+                                   `(,@(if (null? name)
+                                           '()
+                                           `((name . ,name)))
+                                     ,@(if (null? path)
+                                           '()
+                                           `((path . ,path))
+                                           )
+                                     ,@(if (or (null? hash-alg) (not (string? hash-alg)))
+                                           '()
+                                           `((hash-algorithm . ,hash-alg))
+                                           )
+                                     ,@(if (or (null? hash) (not (string? hash)))
+                                           '()
+                                           `((hash . ,hash))
+                                           )
+                                     ,@(if (null? recursive)
+                                           '()
+                                           `((recursive . ,(string=? recursive "t")))))))
+                                (or items '())))))
+                      '(base target common)
+                      (list (assq-ref outputs 'base)
+                            (assq-ref outputs 'target)
+                            (assq-ref outputs 'common))))
+
+                    (inputs  (assq-ref data 'inputs))
+                    (matched-inputs
+                     (map
+                      (lambda (label items)
+                        (cons label
+                              (list->vector
+                               (map 
+                                (match-lambda
+                                  ((derivation output)
+                                   `(,@(if (null? derivation)
+                                           '()
+                                           `((derivation . ,derivation)))
+                                     ,@(if (null? output)
+                                           '()
+                                           `((output . ,output))))))
+                                (or items '())))))
+                      '(base target common)
+                      (list (assq-ref inputs 'base)
+                            (assq-ref inputs 'target))))
+                    
+                    (sources (assq-ref data 'sources))
+                    (matched-sources
+                     (map
+                      (lambda (label items)
+                        (cons label
+                              (list->vector
+                               (map
+                                (match-lambda
+                                  ((derivation)
+                                   `(,@(if (null? derivation)
+                                           '()
+                                           `((derivation . ,derivation))))))
+                                (or items '())))))
+                      '(base target common)
+                      (list (assq-ref sources 'base)
+                            (assq-ref sources 'target)
+                            (assq-ref sources 'common))))
+                    
+                    (args    (assq-ref data 'arguments))
+                    (base-args (assq-ref args 'base))
+                    (target-args (assq-ref args 'target)))
+               
+               (render-json
+                `((base
+                   . ((derivation . ,base-derivation)))
+                  (target
+                   . ((derivation . ,target-derivation)))
+                  (outputs
+                   . ,matched-outputs)
+                  (inputs
+                   . ,matched-inputs)
+                  (sources                   
+                   . ,matched-sources)
+                  (system
+                   . ,(assq-ref data 'system))
+                  (builder-and-arguments
+                   . ((builder . ,(assq-ref data 'builder))
+                      (arguments
+                       . ((base . ,(list->vector
+                                    base-args))
+                          (target . ,(list->vector
+                                      target-args))))))
+                  (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