remove symbols as length notation

main
Matthew Butterick 6 years ago
parent 95a4d85cb8
commit a4255af04d

@ -26,14 +26,14 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x15\x05roxyb\x15")])
(check-equal?
(decode (x:dict 'name (x:string #:length uint8) 'age uint8
'nested (x:dict 'name (x:string #:length uint8) 'age uint8)))
'nested (x:dict 'name (x:string #:length uint8) 'age uint8)))
(mhasheq 'name "roxyb" 'age 21 'nested (mhasheq 'name "roxyb" 'age 21)))))
(test-case
"dict: decode with process hook"
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x20")])
(define struct (x:dict #:post-decode (λ (o) (hash-set! o 'canDrink (>= (hash-ref o 'age) 21)) o)
'name (x:string #:length uint8) 'age uint8))
'name (x:string #:length uint8) 'age uint8))
(check-equal? (decode struct)
(mhasheq 'name "roxyb" 'age 32 'canDrink #t))))
@ -52,9 +52,9 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(test-case
"dict: compute the correct size with pointers"
(check-equal? (send (x:dict 'name (x:string #:length uint8)
'age uint8
'ptr (x:pointer #:type uint8 #:dest-type (x:string #:length uint8)))
x:size
'age uint8
'ptr (x:pointer #:type uint8 #:dest-type (x:string #:length uint8)))
x:size
(mhash 'name "roxyb" 'age 21 'ptr "hello")) 14))
(test-case
@ -75,10 +75,11 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
"dict: support pre-encode hook"
(parameterize ([current-output-port (open-output-bytes)])
(define struct (x:dict #:pre-encode (λ (val)
(hash-set! val 'nameLength (string-length (hash-ref val 'name))) val)
'nameLength uint8
'name (x:string 'nameLength)
'age uint8))
(hash-set! val 'nameLength (string-length (hash-ref val 'name)))
val)
'nameLength uint8
'name (x:string (λ (this) (hash-ref this 'nameLength)))
'age uint8))
(encode struct (mhasheq 'name "roxyb" 'age 21))
(check-equal? (get-output-bytes (current-output-port)) #"\x05roxyb\x15")))
@ -86,7 +87,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
"dict: encode pointer data after structure"
(parameterize ([current-output-port (open-output-bytes)])
(define struct (x:dict 'name (x:string #:length uint8)
'age uint8
'ptr (x:pointer uint8 #:dest-type (x:string #:length uint8))))
'age uint8
'ptr (x:pointer uint8 #:dest-type (x:string #:length uint8))))
(encode struct (hasheq 'name "roxyb" 'age 21 'ptr "hello"))
(check-equal? (get-output-bytes (current-output-port)) #"\x05roxyb\x15\x08\x05hello")))

@ -80,8 +80,8 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'gender 0)))))
(test-case
"versioned dict: decode should support parent version key"
(let ([vstruct (x:versioned-dict x:version-key
"versioned dict: decode should support parent version key via procedure"
(let ([vstruct (x:versioned-dict (λ (p) (hash-ref p x:version-key))
(dictify
0 (dictify 'name (x:string #:length uint8 #:encoding 'ascii)
'age uint8)

@ -6,7 +6,6 @@
(or (not x)
(exact-nonnegative-integer? x)
(procedure? x)
(symbol? x)
(x:int? x)))
(define (resolve-length x input-port [parent #f])

@ -1,6 +1,7 @@
#lang debug racket/base
(require "base.rkt"
"dict.rkt"
"util.rkt"
racket/dict
racket/match
racket/class
@ -14,8 +15,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|#
(define (version-type? x)
(for/or ([proc (list integer? procedure? xenomorphic? symbol?)])
(proc x)))
(and x (length-resolvable? x)))
(define x:versioned-dict%
(class x:dict%
@ -25,7 +25,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
[(@version-key version-key)])
(unless (version-type? @type)
(raise-argument-error 'x:versioned-dict "integer, procedure, symbol, or xenomorphic" @type))
(raise-argument-error 'x:versioned-dict "integer, procedure, or xenomorphic" @type))
(unless (and (dict? @versions) (andmap (λ (v) (or (dict? v) (x:dict? v))) (dict-values @versions)))
(raise-argument-error 'x:versioned-dict "dict of dicts or structish" @versions))
@ -43,7 +43,6 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(define res (setup-private-fields port parent length))
(define which-version (match @type
[(? integer? int) int]
[(? symbol? key) #:when parent (dict-ref parent key)]
[(? procedure? proc) #:when parent (proc parent)]
[(or (? symbol?) (? procedure?))
(raise-argument-error 'decode "valid parent" parent)]
@ -77,14 +76,14 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(send @type x:encode (dict-ref field-data @version-key #f) port parent))
(for ([(key type) (in-dict (dict-ref @versions 'header null))])
(send type x:encode (dict-ref field-data key) port parent))
(send type x:encode (dict-ref field-data key) port parent))
(define fields (select-field-set field-data))
(unless (andmap (λ (key) (member key (dict-keys field-data))) (dict-keys fields))
(raise-argument-error 'encode (format "hash that contains superset of xversioned-dict keys: ~a" (dict-keys fields)) (dict-keys field-data)))
(for ([(key type) (in-dict fields)])
(send type x:encode (dict-ref field-data key) port parent))
(send type x:encode (dict-ref field-data key) port parent))
(let loop ([i 0])
(when (< i (length (dict-ref parent x:pointers-key)))
@ -107,11 +106,11 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(define header-size
(for/sum ([(key type) (in-dict (dict-ref @versions 'header null))])
(send type x:size (and val (dict-ref val key)) parent)))
(send type x:size (and val (dict-ref val key)) parent)))
(define fields-size
(for/sum ([(key type) (in-dict (select-field-set val))])
(send type x:size (and val (send type pre-encode (dict-ref val key))) parent)))
(send type x:size (and val (send type pre-encode (dict-ref val key))) parent)))
(define pointer-size (if include-pointers (dict-ref parent x:pointer-size-key) 0))

Loading…
Cancel
Save