unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
* bug#52749: G-expressions don't consistently preserve #nil
@ 2021-12-23  4:25 Philip McGrath
  2021-12-23  6:59 ` Maxime Devos
  2021-12-23 17:58 ` Maxime Devos
  0 siblings, 2 replies; 8+ messages in thread
From: Philip McGrath @ 2021-12-23  4:25 UTC (permalink / raw)
  To: 52749

G-expressions currently do not consistently preserve the distinction 
between #nil and '(), which causes trouble for programs that rely on 
that distinction. In particular, the issue affects programs that use 
(guix build json), because that library uses #nil to represent the JSON 
value `null', whereas it uses '() to represent an empty JSON array.

The following program exposes the error:

--8<---------------cut here---------------start------------->8---
(use-modules (guix build json)
              (guix gexp)
              (guix monads)
              (guix store)
              (guix derivations)
              (rnrs conditions)
              (ice-9 textual-ports))

(define (check-equal? actual expected message)
   (define who 'check-equal?)
   (unless (equal? actual expected)
     (raise-exception
      (condition
       (make-assertion-violation)
       (make-who-condition 'check-equal?)
       (make-irritants-condition (list actual))
       (make-message-condition
        (format #f "~a: ~a\n  ~a: ~s\n  ~a: ~s\n  ~a: ~s"
                who "test failed"
                "message" message
                "expected" expected
                "actual" actual))))))

(define (sexp->json-string sx)
   (call-with-output-string
     (lambda (out)
       (write-json sx out))))

(define (gexp->json-string gx)
   (run-with-store (open-connection)
     (mlet* %store-monad ((drv (gexp->derivation "example.json"
                                 (with-imported-modules `((guix build json))
                                   #~(begin
                                       (use-modules (guix build json))
                                       (call-with-output-file #$output
                                         (lambda (out)
                                           (write-json #$gx out)))))))
                          (_built (built-derivations (list drv))))
       (return (call-with-input-file (derivation->output-path drv)
                 get-string-all)))))

(check-equal? (sexp->json-string '())
               "[]"
               "sexp: empty array")

(check-equal? (gexp->json-string #~'())
               "[]"
               "gexp: empty array")

(check-equal? (sexp->json-string #nil)
               "null"
               "sexp: null")

(check-equal? (gexp->json-string #~#nil)
               "null"
               "gexp: null")

(check-equal? (sexp->json-string '(@ ("k" . #nil)))
               "{\"k\":null}"
               "sexp: null in object")

;; This one fails!
(check-equal? (gexp->json-string #~'(@ ("k" . #nil)))
               "{\"k\":null}"
               "gexp: null in object")
--8<---------------cut here---------------end--------------->8---

-Philip




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-01-03 10:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-23  4:25 bug#52749: G-expressions don't consistently preserve #nil Philip McGrath
2021-12-23  6:59 ` Maxime Devos
2021-12-25 11:13   ` Maxime Devos
2021-12-27 18:38     ` Philip McGrath
2021-12-27 20:24       ` Maxime Devos
2022-01-03 10:28       ` Maxime Devos
2022-01-03 10:49       ` Maxime Devos
2021-12-23 17:58 ` Maxime Devos

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).