agrafix / Spock

Another Haskell web framework for rapid development
https://www.spock.li
678 stars 56 forks source link

maybe var for routes #126

Closed nicemaker closed 2 years ago

nicemaker commented 7 years ago

If have following paths:

If I haven't miss something I have to create two path definitions for that, like:

get ("search"<//>var)-> searchAction
get ("search"<//>var<//>var)-> searchAction

can that somehow be combined? I was thinking of something like this: get("search"<//>var<//>maybevar)->searchAction

P.S. This is my first Spock App, I like it very much, please keep on! My haskell is still in baby shoes, if I would know more I would help you guys somehow.

brynedwards commented 7 years ago

I would do something like this: https://gist.github.com/brynedwards/f27b9df1fa32a879a535fce0335f1039

You could make the app routes more concise like so:

app :: SpockM () () () ()
app = do
  get ("search" <//> var) $ flip searchAction Nothing
  get ("search" <//> var <//> var) searchAction 

Either way I think it should do what you want.

nicemaker commented 7 years ago

Thanks a million, that gist also answered a few other haskell confusion I just had in my head. But theoretically, a maybevar approach would work, wouldn't it? then you would have var, wildcard and maybevar.

brynedwards commented 7 years ago

I guess so. agrafix would probably have a better idea, I'm not familiar with Spock's routing internals

agrafix commented 7 years ago

Yes, we could add maybevar to hook it to a function 'maybeHandler :: Maybe SomeType -> ...', but it would require support from the routing framework.

Alternatively you could also define such a combinator yourself (not tested, just a sketch):

optParamGet :: (Typeable p, FromHttpApiData p) => Path xs 'Open -> (Maybe p -> HVect Elim xs (SpockActionCtx ctx conn sess st ())) -> SpockCtxM ctx conn sess st ()
optParamGet basePath handle =
    do get basePath (handle Nothing)
       get (basePath <//> var) (\x -> handle (Just x))
nicemaker commented 7 years ago

thanks agrafix, going to try it out.