Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nullable fields in define-json-type macro #87

Open
ebeem opened this issue Jun 8, 2024 · 1 comment
Open

nullable fields in define-json-type macro #87

ebeem opened this issue Jun 8, 2024 · 1 comment

Comments

@ebeem
Copy link

ebeem commented Jun 8, 2024

Thanks for the awesome work.
I was wondering if it's possible to somehow allow nullable fields in the define-json-type macro.
I think that this is indeed the expected behavior.

Example

(define-json-type <student>
  (id)
  (name)
  (major "major" <major>))

(define-json-type <major>
  (id)
  (name)
  (parent "parent" <major>))

(define json-str "{ \"id\": 1, \"name\": null, \"major\": null}")
(json->student json-str)

error

In procedure assoc: Wrong type argument in position 2 (expecting association list): null

changing the json-str to the below

builds a correct (not unspecified) scheme object

(define json-str "{ \"id\": 1, \"name\": null, \"major\": {}}")
(json->student json-str)

output

=> #<<student> id: 1 name: null major: #<<major> id: #<unspecified> name: #<unspecified> parent: #<unspecified>>>

also emitting the field major will produce correct results.

(define json-str "{ \"id\": 1, \"name\": null }")
(json->student json-str)

output

=> #<<student> id: 1 name: null major: #<unspecified>>

Anything wrong with my approach?
Thanks again for your great work.

@ebeem ebeem changed the title nullable filed in define-json-type macro nullable fields in define-json-type macro Jun 8, 2024
@ebeem
Copy link
Author

ebeem commented Jun 8, 2024

I could solve it by modifying the extract-field macro to check for null values in addition to the pair check

      (let-syntax ((extract-field (syntax-rules ()
                                    ((_ table (field key scm->value value->scm))
                                     (scm->value (if (and (pair? (assoc key table)) (not (equal? 'null (cdr (assoc key table)))))
                                                     (cdr (assoc key table)) *unspecified*)))
                                    ((_ table (field key scm->value))
                                     (scm->value (if (pair? (assoc key table)) (cdr (assoc key table)) *unspecified*)))
                                    ((_ table (field key))
                                     (if (pair? (assoc key table)) (cdr (assoc key table)) *unspecified*))
                                    ((_ table (field))
                                     (if (pair? (assoc (symbol->string 'field) table)) (cdr (assoc (symbol->string 'field) table)) *unspecified*)))))
        (ctor (extract-field table spec) ...))
;; before
(scm->value (if (pair? (assoc key table)) (cdr (assoc key table)) *unspecified*))

;; after
(scm->value (if (and (pair? (assoc key table)) (not (equal? 'null (cdr (assoc key table)))))
                                                     (cdr (assoc key table)) *unspecified*))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant