Changeset 27887 in project


Ignore:
Timestamp:
11/30/12 22:05:18 (7 years ago)
Author:
Mario Domenech Goulart
Message:

spiffy-request-vars (trunk): add `compound-variable-separator', as suggested by Taylor Venable

Location:
release/4/spiffy-request-vars/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • release/4/spiffy-request-vars/trunk/spiffy-request-vars.scm

    r25425 r27887  
    33
    44   ;; Parameters
    5    true-boolean-values
     5   true-boolean-values compound-variable-separator
    66
    77   ;; Converters
     
    1717  (make-parameter
    1818   '("y" "yes" "1" "on" "true")))
     19
     20(define compound-variable-separator
     21  ;; a string that separates names in compound objects
     22  (make-parameter "."))
    1923
    2024(define (req-vars/vals var vars/vals)
     
    5963                (let* ((var/val (car vars/vals))
    6064                       (current-var (symbol->string (car var/val)))
    61                        (tokens (string-split current-var ".")))
     65                       (tokens (string-split current-var (compound-variable-separator))))
    6266                  (if (and (not (null? (cdr tokens)))
    6367                           (equal? var (car tokens)))
  • release/4/spiffy-request-vars/trunk/spiffy-request-vars.setup

    r26716 r27887  
    66  'spiffy-request-vars
    77  '("spiffy-request-vars.so" "spiffy-request-vars.import.so")
    8   `((version 0.16)))
     8  `((version 0.17)))
  • release/4/spiffy-request-vars/trunk/tests/client.scm

    r26714 r27887  
    227227(test '("1" "2" "3" #f) (post "/test9" '((foo . 1) (bar . 2) (baz . 3)) '((blah . 4))))
    228228
     229
     230
     231;;;
     232;;; Compound variable names
     233;;;
     234
     235;; Vectors
     236(test #f (get "/compound/as-vector"))
     237(test #f (get "/compound/wrv-as-vector"))
     238(test '#("") (get "/compound/wrv-as-vector?var_0="))
     239
     240(test '#("a" "b") (get "/compound/as-vector?var_0=a;var_1=b"))
     241(test '#("a" "b") (get "/compound/wrv-as-vector?var_0=a;var_1=b"))
     242
     243(test 3 (vector-length (get "/compound/as-vector?var_0=a;var_1=b;var_2=4;varc=4")))
     244(test 3 (vector-length (get "/compound/wrv-as-vector?var_0=a;var_1=b;var_2=4;varc=4")))
     245
     246(test #f (get "/compound/as-vector?var=c"))
     247(test #f (get "/compound/wrv-as-vector?var=c"))
     248
     249(test '#("a" "b") (get "/compound/as-vector?var_0=a;var_1=b;var3=c"))
     250(test '#("a" "b") (get "/compound/wrv-as-vector?var_0=a;var_1=b;var3=c"))
     251
     252
     253;; Alists
     254(test #f (get "/compound/as-list"))
     255(test #f (get "/compound/wrv-as-alist"))
     256(test '((A . "")) (get "/compound/wrv-as-alist?var_A="))
     257
     258(test '((A . "a") (B . "b")) (get "/compound/as-alist?var_A=a;var_B=b"))
     259(test '((A . "a") (B . "b")) (get "/compound/wrv-as-alist?var_A=a;var_B=b"))
     260
     261(test #f (get "/compound/as-alist?var=c"))
     262(test #f (get "/compound/wrv-as-alist?var=c"))
     263
     264
     265;; Hash-tables
     266(test #f (get "/compound/as-hash-table"))
     267(test #f (get "/compound/wrv-as-hash-table"))
     268
     269(test #t (let ((res (get "/compound/as-hash-table?var_A=a;var_B=b")))
     270           (lset= equal? res '((A . "a") (B . "b")))))
     271
     272(test #t (let ((res (get "/compound/wrv-as-hash-table?var_A=a;var_B=b")))
     273           (lset= equal? res '((A . "a") (B . "b")))))
     274
     275(test '((A . "")) (get "/compound/wrv-as-hash-table?var_A="))
     276
     277(test #f (get "/compound/as-hash-table?var=c"))
     278(test #f (get "/compound/wrv-as-hash-table?var=c"))
     279
     280
     281;; test3
     282(test '(#f #f #f #f) (get "/compound/test3"))
     283
     284(test '(((A . "0") (B . "1")) 0 #("a" "b") #t)
     285      (get "/compound/test3?foo_A=0&foo_B=1&bar=0&baz_0=a&baz_1=b&bool=yes"))
     286
     287(test '(#f #f #f #f)
     288      (get "/compound/test3?foo=0&bar=a&baz=0&bool=3"))
     289
     290
     291;; test4
     292(test '(#f #f #f #f) (get "/compound/test4"))
     293
     294(test '(((A . "0") (B . "1")) 0 #("a" "b") #t)
     295      (get "/compound/test4?foo_A=0&foo_B=1&bar=0&baz_0=a&baz_1=b&bool=yes"))
     296
     297(test '(#f #f #f #f)
     298      (get "/compound/test4?foo=0&bar=a&baz=0&bool=3"))
     299
    229300(test-end "spiffy-request-vars")
  • release/4/spiffy-request-vars/trunk/tests/server.scm

    r25074 r27887  
    1 (use srfi-69 awful spiffy-request-vars spiffy intarweb uri-common html-tags)
     1(use srfi-69 srfi-13 awful spiffy-request-vars spiffy intarweb uri-common html-tags)
    22
    33(page-template (lambda (contents . more) contents))
     
    1414  (with-output-to-string (cut write var)))
    1515
    16 (define-page "as-string" (lambda () (show ($ 'var))))
     16(define (define-page* path-suffix proc #!key (method 'get))
     17  (define-page path-suffix proc method: method)
     18  (define-page (make-pathname "compound" path-suffix) proc method: method))
    1719
    18 (define-page "as-symbol" (lambda () (show ($ 'var as-symbol))))
     20;;; Compound variable name separator
     21(add-request-handler-hook!
     22 'compound-variable-names
     23 (lambda (path handler)
     24   (when (string-prefix? "/compound/" path)
     25     (parameterize ((compound-variable-separator "_"))
     26       (handler)))))
    1927
    20 (define-page "as-number" (lambda () (show ($ 'var as-number))))
     28;;; Tests
     29(define-page* "as-string" (lambda () (show ($ 'var))))
    2130
    22 (define-page "as-boolean" (lambda () (show ($ 'var as-boolean))))
     31(define-page* "as-symbol" (lambda () (show ($ 'var as-symbol))))
    2332
    24 (define-page "as-list" (lambda () (show ($ 'var as-list))))
     33(define-page* "as-number" (lambda () (show ($ 'var as-number))))
    2534
    26 (define-page "as-vector" (lambda () (show ($ 'var as-vector))))
     35(define-page* "as-boolean" (lambda () (show ($ 'var as-boolean))))
    2736
    28 (define-page "as-alist" (lambda () (show ($ 'var as-alist))))
     37(define-page* "as-list" (lambda () (show ($ 'var as-list))))
    2938
    30 (define-page "as-hash-table"
     39(define-page* "as-vector" (lambda () (show ($ 'var as-vector))))
     40
     41(define-page* "as-alist" (lambda () (show ($ 'var as-alist))))
     42
     43(define-page* "as-hash-table"
    3144  (lambda ()
    3245    (show (and-let* ((var ($ 'var as-hash-table)))
    3346            (hash-table->alist var)))))
    3447
    35 (define-page "as-nonempty-string" (lambda () (show ($ 'var (nonempty as-string)))))
     48(define-page* "as-nonempty-string" (lambda () (show ($ 'var (nonempty as-string)))))
    3649
    37 (define-page "as-nonempty-symbol" (lambda () (show ($ 'var (nonempty as-symbol)))))
     50(define-page* "as-nonempty-symbol" (lambda () (show ($ 'var (nonempty as-symbol)))))
    3851
    39 (define-page "as-nonempty-number" (lambda () (show ($ 'var (nonempty as-number)))))
     52(define-page* "as-nonempty-number" (lambda () (show ($ 'var (nonempty as-number)))))
    4053
    4154
    4255;;; with-request-vars
    43 (define-page "wrv-as-string"
     56(define-page* "wrv-as-string"
    4457  (lambda ()
    4558    (with-request-vars* $ (var)
    4659        (show var))))
    4760
    48 (define-page "wrv-as-symbol"
     61(define-page* "wrv-as-symbol"
    4962  (lambda ()
    5063    (with-request-vars ((var as-symbol))
    5164        (show var))))
    5265
    53 (define-page "wrv-as-number"
     66(define-page* "wrv-as-number"
    5467  (lambda ()
    5568    (with-request-vars ((var as-number))
    5669        (show var))))
    5770
    58 (define-page "wrv-as-boolean"
     71(define-page* "wrv-as-boolean"
    5972  (lambda ()
    6073    (with-request-vars ((var as-boolean))
    6174        (show var))))
    6275
    63 (define-page "wrv-as-list"
     76(define-page* "wrv-as-list"
    6477  (lambda ()
    6578    (with-request-vars ((var as-list))
    6679        (show var))))
    6780
    68 (define-page "wrv-as-vector"
     81(define-page* "wrv-as-vector"
    6982  (lambda ()
    7083    (with-request-vars ((var as-vector))
    7184        (show var))))
    7285
    73 (define-page "wrv-as-alist"
     86(define-page* "wrv-as-alist"
    7487  (lambda ()
    7588    (with-request-vars ((var as-alist))
    7689        (show var))))
    7790
    78 (define-page "wrv-as-hash-table"
     91(define-page* "wrv-as-hash-table"
    7992  (lambda ()
    8093    (with-request-vars ((var as-hash-table))
     
    8396
    8497;;; test1
    85 (define-page "test1"
     98(define-page* "test1"
    8699  (lambda ()
    87100    (with-request-vars (foo (bar as-list) (baz 5))
     
    90103
    91104;;; test2
    92 (define-page "test2"
     105(define-page* "test2"
    93106  (lambda ()
    94107    (with-request-vars* $ (foo (bar as-list) (baz 5))
     
    97110
    98111;;; test3
    99 (define-page "test3"
     112(define-page* "test3"
    100113  (lambda ()
    101114    (with-request-vars ((foo as-alist) (bar as-number) (baz as-vector) (bool as-boolean))
     
    104117
    105118;;; test4
    106 (define-page "test4"
     119(define-page* "test4"
    107120  (lambda ()
    108121    (with-request-vars* $ ((foo as-alist) (bar as-number) (baz as-vector) (bool as-boolean))
     
    111124
    112125;;; test5
    113 (define-page "test5"
     126(define-page* "test5"
    114127  (lambda ()
    115128    (with-request-vars (foo bar baz)
     
    118131
    119132;;; test6
    120 (define-page "test6"
     133(define-page* "test6"
    121134  (lambda ()
    122135    (with-request-vars* $ (foo bar baz)
     
    125138
    126139;;; test7 (POST)
    127 (define-page "test7"
     140(define-page* "test7"
    128141  (lambda ()
    129142    (with-request-vars* $ (foo bar baz blah)
     
    132145
    133146;;; test8
    134 (define-page "test8"
     147(define-page* "test8"
    135148  (lambda ()
    136149    (with-request-vars* (request-vars source: 'query-string) (foo bar baz blah)
     
    139152
    140153;;; test9
    141 (define-page "test9"
     154(define-page* "test9"
    142155  (lambda ()
    143156    (with-request-vars* (request-vars source: 'request-body) (foo bar baz blah)
    144157      (show (list foo bar baz blah))))
    145158  method: 'POST)
     159
     160
Note: See TracChangeset for help on using the changeset viewer.