Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an updateQueryParams function #31

Closed
joelmccracken opened this issue Sep 30, 2020 · 3 comments
Closed

Add an updateQueryParams function #31

joelmccracken opened this issue Sep 30, 2020 · 3 comments

Comments

@joelmccracken
Copy link

Heya, thanks for the great library. Working with it I need to "update" the value of an existing QueryParam from a URI, and while writing a function to do this, it surprised me that something like it didn't exist in the library.

I'm not sure if you think this would be a nice thing to add to this library, or perhaps this is easy to do somehow with lenses (I do not know lenses very well, so it might be and I just don't know).

@mrkkrp
Copy link
Owner

mrkkrp commented Sep 30, 2020

I'd say that the easiest way to do it is with lenses. Can you show definition of the function you want to add?

@joelmccracken
Copy link
Author

joelmccracken commented Sep 30, 2020

Something like this:

queryParamKey :: QueryParam -> RText 'QueryKey
queryParamKey qp =
  case qp of
    QueryFlag qk    -> qk
    QueryParam qk _ -> qk

updateURIQueryParam :: URI -> QueryParam -> URI
updateURIQueryParam uri qp = do
  let sameKey :: QueryParam -> QueryParam -> Bool
      sameKey qParam qParam' = queryParamKey qParam /= queryParamKey qParam'
  let replaceParam :: QueryParam -> [QueryParam] -> [QueryParam]
      replaceParam qParam qps = qParam : filter (sameKey qParam) qps
  uri & L.uriQuery %~ replaceParam qp

(I figured out the lenses stuff though I think this would actually add a dependency on the lens library so I assume you will want to change it)

@mrkkrp
Copy link
Owner

mrkkrp commented Oct 1, 2020

This is really something that is best done via lenses. I'd like to try to avoid adding ad-hoc functions like this.

You can use the microlens package which has no dependencies and includes the commonly useful functions of lens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants