parameter with list of values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

parameter with list of values

Corentin Dupont
Hi guys,
I hope you are doing well.
I want to parse a parameter that accepts a list of values:


I tried:

:> QueryParam "foo"  [Foo]

But it complains:

No instance for (FromHttpApiData [Foo])

I could try to provide this instance of FromHttpApiData, and write a parser for parameters value list, but I was wondering if there is some kind of support already in Servant? Any example?

Thanks a lot and good vacations!
Corentin

--
Reply | Threaded
Open this post in threaded view
|

Re: parameter with list of values

Arian van Putten
Hey,



:> QueryParams "foo" Foo

The syntax will be:
 ?foo[]=<foo1>&foo[]=<foo2>&...

The [] part is actually ignored, so this will work too:

?foo=<foo1>&foo=<foo2>&..

The comma-separated syntax we currently do not support I think

On Wed, Aug 14, 2019 at 11:52 AM Corentin Dupont <[hidden email]> wrote:
Hi guys,
I hope you are doing well.
I want to parse a parameter that accepts a list of values:


I tried:

:> QueryParam "foo"  [Foo]

But it complains:

No instance for (FromHttpApiData [Foo])

I could try to provide this instance of FromHttpApiData, and write a parser for parameters value list, but I was wondering if there is some kind of support already in Servant? Any example?

Thanks a lot and good vacations!
Corentin

--


--
Groetjes,

Arian

--
Reply | Threaded
Open this post in threaded view
|

Re: parameter with list of values

Alp Mestanogullari
In reply to this post by Corentin Dupont
There isn't support for this in servant, as (as far as I know) this comma-separated format isn't really standard. You could however indeed provide a general combinator + {From,To}HttpApiData instances.

newtype CommaSep a = CommaSep [a]
instance FromHttpApiData a => FromHttpApiData (CommaSep a) where ...
instance ToHttpApiData a => ToHttpApiData (CommaSep a) where ...

where the From instance would split on each comma character and try to parse every "component" using the supplied FromHttpApiData instance for 'a'.

If you don't care about the syntax/etc, then Arian's QueryParams suggestion would probably be the simplest solution.


On Wed, Aug 14, 2019 at 11:52 AM Corentin Dupont <[hidden email]> wrote:
Hi guys,
I hope you are doing well.
I want to parse a parameter that accepts a list of values:


I tried:

:> QueryParam "foo"  [Foo]

But it complains:

No instance for (FromHttpApiData [Foo])

I could try to provide this instance of FromHttpApiData, and write a parser for parameters value list, but I was wondering if there is some kind of support already in Servant? Any example?

Thanks a lot and good vacations!
Corentin

--


--
Alp Mestanogullari

--