Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yfzhe committed Jan 17, 2024
1 parent cc55737 commit 9bed2e1
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions telebot/private/schema.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@
#'jsexpr->name
#'name->jsexpr))

(define-jsexpr->schema jsexpr->name name)
(define-schema->jsexpr name->jsexpr name))]))
(define-jsexpr->schema name)
(define-schema->jsexpr name))]))

(define-syntax (define-jsexpr->schema stx)
(syntax-parse stx
[(_ id:id schema:schema-id)
[(_ schema:schema-id)
#:do [(define schema-info (attribute schema.info))
(define fields (schema-info-fields schema-info))]
#:with jsexpr->schema (schema-info-from-jsexpr schema-info)
#:with struct-id (schema-info-struct-id schema-info)
#:with (field-value ...)
(for/list ([fld (in-list fields)])
Expand All @@ -159,15 +160,16 @@
#'(let ([val (hash-ref jsexpr 'key json-undefined)])
(if (json-undefined? val) val (fld.from-jsexpr val)))
#'(hash-ref jsexpr 'key
(lambda () (raise-argument-error 'id "field \"~a\" is missed" 'key))))]))
#'(define (id jsexpr)
(lambda () (raise-argument-error 'jsexpr->schema "field \"~a\" is missed" 'key))))]))
#'(define (jsexpr->schema jsexpr)
(struct-id field-value ...))]))

(define-syntax (define-schema->jsexpr stx)
(syntax-parse stx
[(_ id:id schema:schema-id)
[(_ schema:schema-id)
#:do [(define schema-info (attribute schema.info))
(define fields (schema-info-fields schema-info))]
#:with schema->jsexpr (schema-info-to-jsexpr schema-info)
#:with struct-id (schema-info-struct-id schema-info)
#:with (set-field-value ...)
(for/list ([fld (in-list fields)])
Expand All @@ -179,7 +181,7 @@
(unless (json-undefined? fld-v)
(hash-set! jsexpr 'fld.key (fld.to-jsexpr fld-v))))
#'(hash-set! jsexpr 'fld.key (fld.to-jsexpr (accessor data))))]))
#'(define (id data)
#'(define (schema->jsexpr data)
(let ([jsexpr (make-hash)])
set-field-value ...
jsexpr))]))
Expand Down

0 comments on commit 9bed2e1

Please sign in to comment.