AvisoNovate / rook

Smart namespace-driven routing for Pedestal
http://rook.readthedocs.io/en/latest/index.html
Apache License 2.0
75 stars 11 forks source link

Optional custom coercions #45

Closed clyfe closed 9 years ago

clyfe commented 9 years ago

This PR adds an optional :coercions metadata key that holds a map containing coercions. When present, the coercions are merged into the default coercions map. Custom coercions can be useful for transforming incoming params. For more info on usage see the documentation changes in the PR.

Rationale

When a key is present, ring's nested-params middleware turns it into a map:

?foo[]=1&foo[]=2 => [1 2]
?foo[0]=1&foo[1]=2 => {0 1 1 2}

Since the ring/nested-params middleware doesn't do parts matching like rack's nested params, a key is mandatory to match parts:

?foo[][x]=1&foo[][y]=2&foo[][x]=3&foo[][y]=4 => 
ring's: [{:x 1} {:y 2} {:x 3} {:y 4}] 
vs rack's: [{:x 1 :y 2} {:x 3 :y 4}]

The mandatory key turns the result into maps:

?foo[0][x]=1&foo[0][y]=2&foo[1][x]=3&foo[1][y]=4 => {0 {:x 1 :y 2} 1 {:x 3 :y 4}}

In this case we only care about the values as a vector. Schema coercion map->vector (via, say, vals) would be helpful in this case.