Use fixed guile-json for optional mapping
Makefile.am
| 27 | 27 | $(top_srcdir)/build-aux/test-driver.scm | |
| 28 | 28 | ||
| 29 | 29 | TEST_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) $(top_srcdir)/tap-driver.sh --ignore-exit | |
| 30 | - | TESTS = tests/asn1.scm | |
| 30 | + | TESTS = tests/asn1.scm \ | |
| 31 | + | tests/webfinger.scm | |
| 31 | 32 | EXTRA_DIST += $(TESTS) | |
| 32 | 33 | ||
| 33 | 34 | schema.org/vocabulary.scm: schema.org/generate-vocabulary.scm $(SCHEMA_ORG_FILE) |
guix.scm
| 30 | 30 | (gnu packages texinfo) | |
| 31 | 31 | (gnu packages tls)) | |
| 32 | 32 | ||
| 33 | + | (define my-guile-json | |
| 34 | + | (package | |
| 35 | + | (inherit guile-json-4) | |
| 36 | + | (source (origin | |
| 37 | + | (method git-fetch) | |
| 38 | + | (uri (git-reference | |
| 39 | + | (url "https://github.com/aconchillo/guile-json") | |
| 40 | + | (commit "2e9512b43e31bb697efd359509e986321a903f69"))) | |
| 41 | + | (file-name (git-file-name "guile-json" | |
| 42 | + | (string-append | |
| 43 | + | (package-version guile-json-4) | |
| 44 | + | "-2e9512b"))) | |
| 45 | + | (sha256 | |
| 46 | + | (base32 | |
| 47 | + | "1al7pa9726fsj4hki0zqzgknn3v2kn3d01wp6fmkdc3cwpyamanc")))) | |
| 48 | + | (native-inputs | |
| 49 | + | `(("autoconf" ,autoconf) | |
| 50 | + | ("automake" ,automake) | |
| 51 | + | ("pkg-config" ,pkg-config) | |
| 52 | + | ,@(package-native-inputs guile-json-4))))) | |
| 53 | + | ||
| 33 | 54 | (package | |
| 34 | 55 | (name "guile-fediverse") | |
| 35 | 56 | (version "0.1.0") | |
… | |||
| 48 | 69 | `(#:make-flags '("SCHEMA_ORG_FILE=schema.org/schema.jsonld"))) | |
| 49 | 70 | (inputs | |
| 50 | 71 | `(("guile" ,guile-3.0) | |
| 51 | - | ("guile-json" ,guile-json-4) | |
| 72 | + | ("guile-json" ,my-guile-json) | |
| 52 | 73 | ("guile-jsonld" ,guile-jsonld) | |
| 53 | 74 | ("guile-gcrypt" ,guile-gcrypt))) | |
| 54 | 75 | (native-inputs | |
tests/webfinger.scm
| 17 | 17 | ||
| 18 | 18 | (define-module (tests webfinger) | |
| 19 | 19 | #:use-module (ice-9 format) | |
| 20 | + | #:use-module (ice-9 match) | |
| 20 | 21 | #:use-module (json) | |
| 21 | 22 | #:use-module (rnrs bytevectors) | |
| 23 | + | #:use-module (srfi srfi-1) | |
| 22 | 24 | #:use-module (srfi srfi-64) | |
| 23 | 25 | #:use-module (webfinger webfinger)) | |
| 24 | 26 | ||
| 25 | 27 | (test-begin "webfinger") | |
| 26 | 28 | ||
| 29 | + | (define (json-has-key? json key) | |
| 30 | + | (match json | |
| 31 | + | (((k . v) json ...) | |
| 32 | + | (or (equal? key k) (json-has-key? json key))) | |
| 33 | + | (_ #f))) | |
| 34 | + | ||
| 35 | + | (define (alist-include? j1 j2) | |
| 36 | + | (fold | |
| 37 | + | (lambda (elem acc) | |
| 38 | + | (if acc | |
| 39 | + | (match elem | |
| 40 | + | ((key . value) | |
| 41 | + | (and | |
| 42 | + | (json-has-key? j2 key) | |
| 43 | + | (json-equal? value (assoc-ref j2 key)))) | |
| 44 | + | (_ #f)) | |
| 45 | + | #f)) | |
| 46 | + | #t | |
| 47 | + | j1)) | |
| 48 | + | ||
| 49 | + | (define (json-equal? j1 j2) | |
| 50 | + | (cond | |
| 51 | + | ((list? j1) | |
| 52 | + | (and (list? j2) | |
| 53 | + | (alist-include? j1 j2) | |
| 54 | + | (alist-include? j2 j1))) | |
| 55 | + | ((string? j1) | |
| 56 | + | (equal? j1 j2)) | |
| 57 | + | ((array? j1) | |
| 58 | + | (let loop ((j1 j1) (j2 j2)) | |
| 59 | + | (match (cons j1 j2) | |
| 60 | + | ((#() . #()) | |
| 61 | + | #t) | |
| 62 | + | ((#(v1 j1 ...) . #(v2 j2 ...)) | |
| 63 | + | (and (json-equal? v1 v2) | |
| 64 | + | (loop `#(,@j1) `#(,@j2)))) | |
| 65 | + | (_ #f)))) | |
| 66 | + | (else (equal? j1 j2)))) | |
| 67 | + | ||
| 27 | 68 | (define simple-record-scm | |
| 28 | 69 | '(("subject" . "acct:carol@example.com") | |
| 29 | 70 | ("links" . #((("rel" . "http://openid.net/specs/connect/1.0/issuer") | |
… | |||
| 32 | 73 | (make-jrd-record | |
| 33 | 74 | "acct:carol@example.com" | |
| 34 | 75 | '() | |
| 35 | - | #f | |
| 76 | + | *unspecified* | |
| 36 | 77 | (list | |
| 37 | 78 | (make-link-record | |
| 38 | 79 | "http://openid.net/specs/connect/1.0/issuer" | |
| 39 | - | #f | |
| 80 | + | *unspecified* | |
| 40 | 81 | "https://openid.example.com" | |
| 41 | - | #f | |
| 42 | - | #f)))) | |
| 82 | + | *unspecified* | |
| 83 | + | *unspecified*)))) | |
| 84 | + | ||
| 85 | + | (test-assert "json-equality" | |
| 86 | + | (and (json-equal? #t #t) | |
| 87 | + | (json-equal? #f #f) | |
| 88 | + | (json-equal? "abc" "abc") | |
| 89 | + | (not (json-equal? "abc" "cba")) | |
| 90 | + | (json-equal? #() #()) | |
| 91 | + | (json-equal? #(1 2) #(1 2)) | |
| 92 | + | (not (json-equal? #(1 2) #(2 1))) | |
| 93 | + | (json-equal? '(("a" . 1) ("b" . 2)) | |
| 94 | + | '(("a" . 1) ("b" . 2))) | |
| 95 | + | (json-equal? '(("a" . 1) ("b" . 2)) | |
| 96 | + | '(("b" . 2) ("a" . 1))))) | |
| 43 | 97 | ||
| 44 | 98 | (test-assert "simple example decode" | |
| 45 | 99 | (let ((converted-simple-record (json->jrd-record simple-record-scm))) | |
… | |||
| 48 | 102 | (pk 'fail converted-simple-record #f)))) | |
| 49 | 103 | ||
| 50 | 104 | (test-assert "simple example encode" | |
| 51 | - | (let ((converted-simple-record-scm (jrd->string simple-record))) | |
| 52 | - | (if (equal? (scm->json-string simple-record-scm) | |
| 53 | - | converted-simple-record-scm) | |
| 105 | + | (let ((converted-simple-record-scm (jrd-record->json simple-record))) | |
| 106 | + | (if (json-equal? simple-record-scm | |
| 107 | + | (json-string->scm converted-simple-record-scm)) | |
| 54 | 108 | #t | |
| 55 | - | (pk 'fail converted-simple-record-scm #f)))) | |
| 109 | + | (pk 'fail converted-simple-record-scm | |
| 110 | + | (scm->json-string simple-record-scm) #f)))) | |
| 56 | 111 | ||
| 57 | 112 | (test-end "webfinger") | |
webfinger/webfinger.scm
| 58 | 58 | (aliases jrd-record-aliases "aliases" ; string list | |
| 59 | 59 | (lambda (val) | |
| 60 | 60 | (if val (array->list val) '())) | |
| 61 | - | (lambda (val) (list->array 1 val))) | |
| 61 | + | (lambda (val) | |
| 62 | + | (if (null? val) *unspecified* (list->array 1 val)))) | |
| 62 | 63 | (properties jrd-record-properties) ; alist whose keys and values are strings | |
| 63 | 64 | (links jrd-record-links "links" ; list of links | |
| 64 | 65 | (lambda (val) | |
| 65 | 66 | (if val (map json->link-record (array->list val)) '())) | |
| 66 | - | (lambda (val) (list->array 1 (map link-record->json val))))) | |
| 67 | + | (lambda (val) (list->array 1 (map (lambda (v) (json-string->scm (link-record->json v))) val))))) | |
| 67 | 68 | ||
| 68 | 69 | (define (download-json uri) | |
| 69 | 70 | (json-string->scm | |
… | |||
| 109 | 110 | (equal? (link-record-type link) "application/activity+json")) | |
| 110 | 111 | links))))) | |
| 111 | 112 | ||
| 112 | - | (define (jrd->string jrd) | |
| 113 | - | (scm->json-string | |
| 114 | - | (let ((subject (jrd-record-subject jrd)) | |
| 115 | - | (aliases (jrd-record-aliases jrd)) | |
| 116 | - | (properties (jrd-record-properties jrd)) | |
| 117 | - | (links (jrd-record-links jrd))) | |
| 118 | - | `(("subject" . ,subject) | |
| 119 | - | ,@(if (and aliases (not (null? aliases))) | |
| 120 | - | `(("aliases" . ,(list->array 1 aliases))) | |
| 121 | - | '()) | |
| 122 | - | ,@(if (and properties (not (null? properties))) | |
| 123 | - | `(("properties" . ,properties)) | |
| 124 | - | '()) | |
| 125 | - | ("links" . ,(list->array 1 (map link->scm links))))))) | |
| 126 | - | ||
| 127 | - | (define (link->scm link) | |
| 128 | - | (let ((rel (link-record-rel link)) | |
| 129 | - | (type (link-record-type link)) | |
| 130 | - | (href (link-record-href link)) | |
| 131 | - | (titles (link-record-titles link)) | |
| 132 | - | (properties (link-record-properties link))) | |
| 133 | - | `(("rel" . ,rel) | |
| 134 | - | ,@(if type `(("href" . ,href)) '()) | |
| 135 | - | ("href" . ,href) | |
| 136 | - | ,@(if type `(("titles" . ,titles)) '()) | |
| 137 | - | ,@(if type `(("properties" . ,properties)) '())))) | |
| 113 | + | (define jrd->string jrd-record->json) | |