|
|
|
@ -6,6 +6,8 @@
|
|
|
|
|
approximates
|
|
|
|
|
https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
|
|
|
|
|#
|
|
|
|
|
(define key? symbol?)
|
|
|
|
|
(define (keys? x) (and (pair? x) (andmap key? x)))
|
|
|
|
|
|
|
|
|
|
(define-subclass Struct (VersionedStruct type [versions (dictify)])
|
|
|
|
|
|
|
|
|
@ -20,17 +22,22 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
|
|
|
|
[versionGetter void]
|
|
|
|
|
[versionSetter void])
|
|
|
|
|
|
|
|
|
|
(when (symbol? type) ; instead of string
|
|
|
|
|
(set-field! versionGetter this (λ (parent) (ref parent type)))
|
|
|
|
|
(set-field! versionSetter this (λ (parent version) (ref-set! parent type version))))
|
|
|
|
|
(when (or (key? type) (procedure? type))
|
|
|
|
|
(set-field! versionGetter this (if (procedure? type)
|
|
|
|
|
type
|
|
|
|
|
(λ (parent) (ref parent type))))
|
|
|
|
|
(set-field! versionSetter this (if (procedure? type)
|
|
|
|
|
type
|
|
|
|
|
(λ (parent version) (ref-set! parent type version)))))
|
|
|
|
|
|
|
|
|
|
(define/override (decode stream [parent #f] [length 0])
|
|
|
|
|
(define res (_setup stream parent length))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(ref-set! res 'version
|
|
|
|
|
(cond
|
|
|
|
|
[forced-version] ; for testing purposes: pass an explicit version
|
|
|
|
|
[(symbol? type) (unless parent
|
|
|
|
|
[(or (key? type) (procedure? type))
|
|
|
|
|
(unless parent
|
|
|
|
|
(raise-argument-error 'VersionedStruct:decode "valid parent" parent))
|
|
|
|
|
(versionGetter parent)]
|
|
|
|
|
[else (send type decode stream)]))
|
|
|
|
@ -40,6 +47,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
|
|
|
|
|
|
|
|
|
(define fields (or (ref versions (ref res 'version)) (raise-argument-error 'VersionedStruct:decode "valid version key" (cons version (· this versions)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
[(VersionedStruct? fields) (send fields decode stream parent)]
|
|
|
|
|
[else
|
|
|
|
@ -64,23 +72,23 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
|
|
|
|
|
|
|
|
|
(ref-set! ctx 'pointerOffset (+ (· stream pos) (size val ctx #f)))
|
|
|
|
|
|
|
|
|
|
(when (not (symbol? type))
|
|
|
|
|
(send type encode stream (· val version)))
|
|
|
|
|
(when (not (or (key? type) (procedure? type)))
|
|
|
|
|
(send type encode stream (or forced-version (· val version))))
|
|
|
|
|
|
|
|
|
|
(when (ref versions 'header)
|
|
|
|
|
(for ([(key type) (in-dict (ref versions 'header))])
|
|
|
|
|
(send type encode stream (ref val key) ctx)))
|
|
|
|
|
(send type encode stream (ref val key) ctx)))
|
|
|
|
|
|
|
|
|
|
(define fields (or (ref versions (· val version)) (raise-argument-error 'VersionedStruct:encode "valid version key" version)))
|
|
|
|
|
(define fields (or (ref versions (or forced-version (· val version))) (raise-argument-error 'VersionedStruct:encode "valid version key" version)))
|
|
|
|
|
|
|
|
|
|
(unless (andmap (λ (key) (member key (ref-keys val))) (ref-keys fields))
|
|
|
|
|
(raise-argument-error 'VersionedStruct:encode (format "hash that contains superset of Struct keys: ~a" (dict-keys fields)) (hash-keys val)))
|
|
|
|
|
|
|
|
|
|
(for ([(key type) (in-dict fields)])
|
|
|
|
|
(send type encode stream (ref val key) ctx))
|
|
|
|
|
(send type encode stream (ref val key) ctx))
|
|
|
|
|
|
|
|
|
|
(for ([ptr (in-list (ref ctx 'pointers))])
|
|
|
|
|
(send (ref ptr 'type) encode stream (ref ptr 'val) (ref ptr 'parent))))
|
|
|
|
|
(send (ref ptr 'type) encode stream (ref ptr 'val) (ref ptr 'parent))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/override (size [val (mhash)] [parent #f] [includePointers #t])
|
|
|
|
@ -92,19 +100,19 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
|
|
|
|
'pointerSize 0))
|
|
|
|
|
|
|
|
|
|
(define size 0)
|
|
|
|
|
(when (not (symbol? type))
|
|
|
|
|
(increment! size (send type size (ref val 'version) ctx)))
|
|
|
|
|
|
|
|
|
|
(when (not (or (key? type) (procedure? type)))
|
|
|
|
|
(increment! size (send type size (or forced-version (ref val 'version)) ctx)))
|
|
|
|
|
|
|
|
|
|
(when (ref versions 'header)
|
|
|
|
|
(increment! size
|
|
|
|
|
(for/sum ([(key type) (in-dict (ref versions 'header))])
|
|
|
|
|
(send type size (ref val key) ctx))))
|
|
|
|
|
(send type size (ref val key) ctx))))
|
|
|
|
|
|
|
|
|
|
(define fields (or (ref versions (ref val 'version)) (raise-argument-error 'VersionedStruct:encode "valid version key" version)))
|
|
|
|
|
(define fields (or (ref versions (or forced-version (ref val 'version))) (raise-argument-error 'VersionedStruct:encode "valid version key" version)))
|
|
|
|
|
|
|
|
|
|
(increment! size
|
|
|
|
|
(for/sum ([(key type) (in-dict fields)])
|
|
|
|
|
(send type size (ref val key) ctx)))
|
|
|
|
|
(send type size (ref val key) ctx)))
|
|
|
|
|
|
|
|
|
|
(when includePointers
|
|
|
|
|
(increment! size (ref ctx 'pointerSize)))
|
|
|
|
@ -118,21 +126,19 @@ 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 1)])
|
|
|
|
|
(define field-types (for/list ([i (in-range 1)])
|
|
|
|
|
(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))
|
|
|
|
|
(report* vs (send vs size))
|
|
|
|
|
(error 'stop)
|
|
|
|
|
(define struct-size (for/sum ([num-type (in-list (map cdr (ref struct-versions which-struct)))])
|
|
|
|
|
(send num-type size)))
|
|
|
|
|
(define bs (apply bytes (for/list ([i (in-range struct-size)])
|
|
|
|
|
(random 256))))
|
|
|
|
|
(check-equal? (send vs encode #f (send vs decode bs)) bs))
|
|
|
|
|
(define field-types (for/list ([i (in-range 1)])
|
|
|
|
|
(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 (ref struct-versions which-struct)))])
|
|
|
|
|
(send num-type size)))
|
|
|
|
|
(define bs (apply bytes (for/list ([i (in-range struct-size)])
|
|
|
|
|
(random 256))))
|
|
|
|
|
(check-equal? (send vs encode #f (send vs decode bs)) bs))
|
|
|
|
|
|
|
|
|
|
(define s (+Struct (dictify 'a uint8 'b uint8 'c uint8)))
|
|
|
|
|
(check-equal? (send s size) 3)
|
|
|
|
|