On Fri, 16 Apr 2021 20:17:45 +0100
Christopher Baines <m...@cbaines.net> wrote:

Hi,

I hope the patch is correct this time.
I considered all you said, so I separated the
functions to get outputs, inputs and sources. I also implemented
everything inside the case of the json/application.

> While a flatter list is what you want when building an HTML table, I
> think you were looking to get a JSON object separating the common,
> base and target elements, right? If so, then map, rather than
> append-map should be more useful to you here. Since above you're
> passing in two lists of three things, if the procedure passed to map
> returns a pair with a string in the first position, you'll end up
> producing the scheme version of a JSON object (an alist).

You were right about that, I'm using map now.

Please, let me know if I missed something.
Thanks in advance, I'm learning a great deal! :)
-- 
Best Regards,

Luciana Lima Brito
MSc. in Computer Science
Federal University of Uberlândia
>From b51dd007180e69f7da479a857afd48dfa60e32e7 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 | 122 ++++++++++++++++++-
 1 file changed, 119 insertions(+), 3 deletions(-)

diff --git a/guix-data-service/web/compare/controller.scm b/guix-data-service/web/compare/controller.scm
index a6aa198..1b0fc2f 100644
--- a/guix-data-service/web/compare/controller.scm
+++ b/guix-data-service/web/compare/controller.scm
@@ -588,9 +588,125 @@
                  '(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
+                              (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 '()))))
+                      (list "base" "target" "common")
+                      (list (assq-ref outputs 'base)
+                            (assq-ref outputs 'target)
+                            (assq-ref outputs 'common))))
+                    (base-outputs (list->vector (cdr (first matched-outputs))))
+                    (target-outputs (list->vector (cdr (second matched-outputs))))
+                    (common-outputs (list->vector (cdr (third matched-outputs))))
+
+                    (inputs  (assq-ref data 'inputs))
+                    (matched-inputs
+                     (map
+                      (lambda (label items)
+                        (cons label
+                              (map 
+                               (match-lambda
+                                 ((derivation output)
+                                  `(,@(if (null? derivation)
+                                          '()
+                                          `((derivation . ,derivation)))
+                                    ,@(if (null? output)
+                                          '()
+                                          `((output . ,output))))))
+                               (or items '()))))
+                      (list "base" "target" "common")
+                      (list (assq-ref inputs 'base)
+                            (assq-ref inputs 'target))))
+                    (base-inputs (list->vector (cdr (first matched-inputs))))
+                    (target-inputs (list->vector (cdr (second matched-inputs))))
+                    
+                    (sources (assq-ref data 'sources))
+                    (matched-sources
+                     (map
+                      (lambda (label items)
+                        (cons label
+                              (map
+                               (match-lambda
+                                 ((derivation)
+                                  `(,@(if (null? derivation)
+                                          '()
+                                          `((derivation . ,derivation))))))
+                               (or items '())))) 
+                      (list "base" "target" "common")
+                      (list (assq-ref sources 'base)
+                            (assq-ref sources 'target)
+                            (assq-ref sources 'common))))
+                    (base-sources (list->vector (cdr (first matched-sources))))
+                    (target-sources (list->vector (cdr (second matched-sources))))
+                    (common-sources (list->vector (cdr (third matched-sources))))
+                    
+                    (system  (assq-ref data 'system))
+                    (base-system (assq-ref system 'base))
+                    (target-system (assq-ref system 'target))
+                    (common-system (assq-ref system 'common))
+                    
+                    (builder (assq-ref data 'builder))
+                    (base-builder (assq-ref builder 'base))
+                    (target-builder (assq-ref builder 'target))
+                    (common-builder (assq-ref builder 'common))
+                    
+                    (args    (assq-ref data 'arguments))
+                    (base-args (assq-ref args 'base))
+                    (target-args (assq-ref args 'target))
+                    (common-args (assq-ref args 'common))
+                    (environment-variables (assq-ref data 'environment-variables)))
+               
+               (render-json
+                `((base
+                   . ((derivation . ,base-derivation)))
+                  (target
+                   . ((derivation . ,target-derivation)))
+                  (outputs
+                   . ((,(first (first matched-outputs)) . ,base-outputs)
+                      (,(first (second matched-outputs)) . ,target-outputs)
+                      (,(first (third matched-outputs)) . ,common-outputs)))
+                  (inputs
+                   . ((,(first (first matched-inputs)) . ,base-inputs)
+                      (,(first (second matched-inputs)) . ,target-inputs)))
+                  (sources                   
+                   . ((,(first (first matched-sources)) . ,base-sources)
+                      (,(first (second matched-sources)) . ,target-sources)
+                      (,(first (third matched-sources)) . ,common-sources)))
+                  (system
+                   . ((common . ,common-system)))
+                  (builder-and-arguments
+                   . ((builder . ,common-builder)
+                      (arguments
+                       . ((base . ,(list->vector
+                                    base-args))
+                          (target . ,(list->vector
+                                      target-args))))))
+                  (environment-variables . ,environment-variables))
+                #:extra-headers http-headers-for-unchanging-content)))
             (else
              (render-html
               #:sxml (compare/derivation
-- 
2.30.2

Reply via email to