|
|
|
@ -25,7 +25,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
|
|
|
|
|
(raise-argument-error 'Struct "assocs or hash" assocs))
|
|
|
|
|
(update-key-index! assocs)
|
|
|
|
|
(for ([(k v) (in-dict assocs)])
|
|
|
|
|
(hash-set! fields k v)))
|
|
|
|
|
(hash-set! fields k v)))
|
|
|
|
|
|
|
|
|
|
(update-fields! assocs)
|
|
|
|
|
|
|
|
|
@ -36,20 +36,22 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
|
|
|
|
|
(send this process res stream)
|
|
|
|
|
res)
|
|
|
|
|
|
|
|
|
|
(define/augride (encode stream input-hash [parent #f])
|
|
|
|
|
(define/augment (encode stream input-hash [parent #f])
|
|
|
|
|
|
|
|
|
|
(unless (hash? input-hash)
|
|
|
|
|
(raise-argument-error 'Struct:encode "hash" input-hash))
|
|
|
|
|
|
|
|
|
|
(send this preEncode input-hash stream) ; preEncode goes first, because it might bring input hash into compliance
|
|
|
|
|
|
|
|
|
|
(inner (void) encode stream input-hash parent)
|
|
|
|
|
|
|
|
|
|
(unless (andmap (λ (key) (member key (hash-keys input-hash))) key-index)
|
|
|
|
|
(raise-argument-error 'Struct:encode (format "hash that contains superset of Struct keys: ~a" key-index) (hash-keys input-hash)))
|
|
|
|
|
|
|
|
|
|
(for* ([key (in-list key-index)] ; iterate over original keys in order
|
|
|
|
|
[struct-type (in-value (hash-ref fields key))]
|
|
|
|
|
[value-to-encode (in-value (hash-ref input-hash key))])
|
|
|
|
|
(send struct-type encode stream value-to-encode)))
|
|
|
|
|
(send struct-type encode stream value-to-encode)))
|
|
|
|
|
|
|
|
|
|
(define/public-final (_setup stream parent length)
|
|
|
|
|
(define res (mhasheq))
|
|
|
|
@ -64,16 +66,16 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
|
|
|
|
|
|
|
|
|
|
(define/public-final (_parseFields stream res fields)
|
|
|
|
|
(for ([key (in-list key-index)])
|
|
|
|
|
(define dictvalue (dict-ref fields key))
|
|
|
|
|
(define val
|
|
|
|
|
(if (procedure? dictvalue)
|
|
|
|
|
(dictvalue res)
|
|
|
|
|
(send dictvalue decode stream res)))
|
|
|
|
|
(hash-set! res key val)))
|
|
|
|
|
(define dictvalue (dict-ref fields key))
|
|
|
|
|
(define val
|
|
|
|
|
(if (procedure? dictvalue)
|
|
|
|
|
(dictvalue res)
|
|
|
|
|
(send dictvalue decode stream res)))
|
|
|
|
|
(hash-set! res key val)))
|
|
|
|
|
|
|
|
|
|
(define/override (size [val (mhash)] [parent #f] [includePointers #t])
|
|
|
|
|
(for/sum ([(key type) (in-hash fields)])
|
|
|
|
|
(send type size (hash-ref val key #f)))))
|
|
|
|
|
(send type size (hash-ref val key #f)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(test-module
|
|
|
|
@ -83,17 +85,17 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
|
|
|
|
|
|
|
|
|
|
;; make random structs and make sure we can round trip
|
|
|
|
|
(for ([i (in-range 100)])
|
|
|
|
|
(define field-types (for/list ([i (in-range 200)])
|
|
|
|
|
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
|
|
|
|
|
(define size-num-types (for/sum ([num-type (in-list field-types)])
|
|
|
|
|
(send num-type size)))
|
|
|
|
|
(define s (+Struct (for/list ([num-type (in-list field-types)])
|
|
|
|
|
(cons (gensym) num-type))))
|
|
|
|
|
(define bs (apply bytes (for/list ([i (in-range size-num-types)])
|
|
|
|
|
(random 256))))
|
|
|
|
|
(define es (+EncodeStream))
|
|
|
|
|
(send s encode es (send s decode bs))
|
|
|
|
|
(check-equal? (send es dump) bs)))
|
|
|
|
|
(define field-types (for/list ([i (in-range 200)])
|
|
|
|
|
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
|
|
|
|
|
(define size-num-types (for/sum ([num-type (in-list field-types)])
|
|
|
|
|
(send num-type size)))
|
|
|
|
|
(define s (+Struct (for/list ([num-type (in-list field-types)])
|
|
|
|
|
(cons (gensym) num-type))))
|
|
|
|
|
(define bs (apply bytes (for/list ([i (in-range size-num-types)])
|
|
|
|
|
(random 256))))
|
|
|
|
|
(define es (+EncodeStream))
|
|
|
|
|
(send s encode es (send s decode bs))
|
|
|
|
|
(check-equal? (send es dump) bs)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -130,7 +132,11 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
|
|
|
|
[else
|
|
|
|
|
(send this _parseFields stream res fields)
|
|
|
|
|
(send this process res stream)
|
|
|
|
|
res])))
|
|
|
|
|
res]))
|
|
|
|
|
|
|
|
|
|
(define/augment (encode stream input-hash [parent #f])
|
|
|
|
|
(define assocs (dict-ref versions (· input-hash version) (λ () (raise-argument-error 'VersionedStruct:encode "valid version key" version))))
|
|
|
|
|
(send this update-fields! assocs)))
|
|
|
|
|
|
|
|
|
|
(test-module
|
|
|
|
|
(require "number.rkt")
|
|
|
|
@ -138,18 +144,18 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
|
|
|
|
|
|
|
|
|
;; make random versioned structs and make sure we can round trip
|
|
|
|
|
(for ([i (in-range 20)])
|
|
|
|
|
(define field-types (for/list ([i (in-range 200)])
|
|
|
|
|
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
|
|
|
|
|
(define num-versions 20)
|
|
|
|
|
(define which-struct (random num-versions))
|
|
|
|
|
(define struct-versions (for/list ([v (in-range num-versions)])
|
|
|
|
|
(cons v (for/list ([num-type (in-list field-types)])
|
|
|
|
|
(cons (gensym) num-type)))))
|
|
|
|
|
(define vs (+VersionedStruct which-struct struct-versions))
|
|
|
|
|
(define struct-size (for/sum ([num-type (in-list (map cdr (dict-ref struct-versions which-struct)))])
|
|
|
|
|
(send num-type size)))
|
|
|
|
|
(define bs (apply bytes (for/list ([i (in-range struct-size)])
|
|
|
|
|
(random 256))))
|
|
|
|
|
(define es (+EncodeStream))
|
|
|
|
|
(send vs encode es (send vs decode bs))
|
|
|
|
|
(check-equal? (send es dump) bs)))
|
|
|
|
|
(define field-types (for/list ([i (in-range 200)])
|
|
|
|
|
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
|
|
|
|
|
(define num-versions 20)
|
|
|
|
|
(define which-struct (random num-versions))
|
|
|
|
|
(define struct-versions (for/list ([v (in-range num-versions)])
|
|
|
|
|
(cons v (for/list ([num-type (in-list field-types)])
|
|
|
|
|
(cons (gensym) num-type)))))
|
|
|
|
|
(define vs (+VersionedStruct which-struct struct-versions))
|
|
|
|
|
(define struct-size (for/sum ([num-type (in-list (map cdr (dict-ref struct-versions which-struct)))])
|
|
|
|
|
(send num-type size)))
|
|
|
|
|
(define bs (apply bytes (for/list ([i (in-range struct-size)])
|
|
|
|
|
(random 256))))
|
|
|
|
|
(define es (+EncodeStream))
|
|
|
|
|
(send vs encode es (send vs decode bs))
|
|
|
|
|
(check-equal? (send es dump) bs)))
|
|
|
|
|