Skip to content

Typed query params #61

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

nukisman
Copy link

@nukisman nukisman commented Dec 19, 2018

Now we match query parameters like this

 TypedParams <$ lit "typedParams" <*> paramInt "i" <*> paramBool "is" <*> paramNum "n"`

And create custom typed decoder using paramWith

@CLAassistant
Copy link

CLAassistant commented Dec 19, 2018

CLA assistant check
All committers have signed the CLA.

@garyb
Copy link
Member

garyb commented Feb 9, 2019

This looks like a pretty good addition to me. Anyone have any comments?

I think it'd perhaps be nicer to simplify paramWith to deal with Either? I would think the kind of parsing done in there typically would be simple enough that the extra structure in V (Free MatchError) isn't required.

paramWith :: forall a. (String -> Either MatchError a) -> String -> Match a

toNum s =
let n = readFloat s
in
if isNaN n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseFloat("Infinity") // Infinity so should also add && isFinite n

@safareli
Copy link
Contributor

We are getting a bit of a repetition here (look at toNum, toBool, ... and num, int, bool matchers)

Ideally same toNum, toBool, ... functions could be reused in definition of num, int, bool and param*. for example:

lit :: String -> Match Unit
lit input = eitherMatch $ path ExpectedPathPart <#> \i -> if i == input then Right unit else Left $ UnexpectedPath input

str :: Match String
str = path ExpectedString

path :: MatchError -> Match String
path err = Match \route ->
  case route of
    Cons (Path input) rs ->
      pure $ Tuple rs input
    _ ->
      invalid $ free err


num :: Match Number
num = eitherMatch $ path ExpectedNumber <#> asNum >>> note ExpectedNumber
paramNum :: Match Number
paramNum = paramWith $ asNum >>> note ExpectedNumber

bool :: Match Boolean
bool = eitherMatch $ path ExpectedNumber <#> asBool >>> note ExpectedBoolean
paramBool :: Match Boolean
paramBool = paramWith $ asBool >>> note ExpectedBoolean

We can do this in different PR too.

@thomashoneyman thomashoneyman changed the base branch from master to main October 6, 2020 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants