Remove QueryParams parser in favor of a plain Dict String (List String).

This commit is contained in:
Dillon Kearns 2023-03-03 10:02:51 -08:00
parent d48485e541
commit ecf2dc897c
7 changed files with 29 additions and 144 deletions

View File

@ -1069,7 +1069,7 @@ otherFile routes phaseString =
, ( "host", record |> Elm.get "host" )
, ( "port_", record |> Elm.get "port_" )
, ( "path", record |> Elm.get "path" )
, ( "query", record |> Elm.get "query" |> Gen.Maybe.map Gen.QueryParams.call_.fromString )
, ( "query", record |> Elm.get "query" |> Gen.Maybe.map Gen.QueryParams.call_.fromString |> Gen.Maybe.withDefault Gen.Dict.empty )
, ( "fragment", record |> Elm.get "fragment" )
]
|> Elm.just

File diff suppressed because one or more lines are too long

View File

@ -11,7 +11,6 @@
"Path",
"Pages.PageUrl",
"FatalError",
"QueryParams",
"Pages.Url",
"BackendTask",
"BackendTask.Glob",

View File

@ -226,7 +226,7 @@ init config flags url key =
, host = url.host
, port_ = url.port_
, path = pagePath
, query = url.query |> Maybe.map QueryParams.fromString
, query = url.query |> Maybe.map QueryParams.fromString |> Maybe.withDefault Dict.empty
, fragment = url.fragment
}
}
@ -829,7 +829,7 @@ update config appMsg model =
, host = model.url.host
, port_ = model.url.port_
, path = pagePath
, query = model.url.query |> Maybe.map QueryParams.fromString
, query = model.url.query |> Maybe.map QueryParams.fromString |> Maybe.withDefault Dict.empty
, fragment = model.url.fragment
}
}

View File

@ -10,6 +10,7 @@ to parsing query params individually, which is why the structure of these types
-}
import Dict exposing (Dict)
import Path exposing (Path)
import QueryParams exposing (QueryParams)
import Url
@ -21,7 +22,7 @@ type alias PageUrl =
, host : String
, port_ : Maybe Int
, path : Path
, query : Maybe QueryParams
, query : Dict String (List String)
, fragment : Maybe String
}
@ -33,6 +34,11 @@ toUrl url =
, host = url.host
, port_ = url.port_
, path = url.path |> Path.toRelative
, query = url.query |> Maybe.map QueryParams.toString
, query =
if url.query |> Dict.isEmpty then
Nothing
else
url.query |> QueryParams.toString |> Just
, fragment = url.fragment
}

View File

@ -1,9 +1,7 @@
module QueryParams exposing
( QueryParams
, Parser
, andThen, fail, fromResult, fromString, optionalString, parse, string, strings, succeed
, map2, oneOf
, toDict, toString
, fromString
, toString
)
{-| Represents the query portion of a URL. You can use `toDict` or `toString` to turn it into basic types, or you can
@ -35,144 +33,29 @@ import Url
{-| -}
type QueryParams
= QueryParams String
{-| -}
type Parser a
= Parser (Dict String (List String) -> Result String a)
{-| -}
succeed : a -> Parser a
succeed value =
Parser (\_ -> Ok value)
{-| -}
fail : String -> Parser a
fail errorMessage =
Parser (\_ -> Err errorMessage)
{-| -}
fromResult : Result String a -> Parser a
fromResult result =
Parser (\_ -> result)
{-| -}
andThen : (a -> Parser b) -> Parser a -> Parser b
andThen andThenFn (Parser parser) =
Parser
(\dict ->
case Result.map andThenFn (parser dict) of
Ok (Parser result) ->
result dict
Err error ->
Err error
)
{-| -}
oneOf : List (Parser a) -> Parser a
oneOf parsers =
Parser
(tryParser parsers)
{-| -}
tryParser : List (Parser a) -> Dict String (List String) -> Result String a
tryParser parsers dict =
case parsers of
[] ->
Err ""
(Parser nextParser) :: otherParsers ->
case nextParser dict of
Ok okValue ->
Ok okValue
Err _ ->
tryParser otherParsers dict
{-| -}
map2 : (a -> b -> combined) -> Parser a -> Parser b -> Parser combined
map2 func (Parser a) (Parser b) =
Parser <|
\dict ->
Result.map2 func (a dict) (b dict)
{-| -}
optionalString : String -> Parser (Maybe String)
optionalString key =
custom key
(\stringList ->
case stringList of
str :: _ ->
Ok (Just str)
_ ->
Ok Nothing
)
{-| -}
string : String -> Parser String
string key =
custom key
(\stringList ->
case stringList of
[ str ] ->
Ok str
_ ->
Err ("Missing key " ++ key)
)
{-| -}
custom : String -> (List String -> Result String a) -> Parser a
custom key customFn =
Parser <|
\dict ->
customFn (Maybe.withDefault [] (Dict.get key dict))
{-| -}
strings : String -> Parser (List String)
strings key =
custom key
(\stringList -> Ok stringList)
{-| -}
fromString : String -> QueryParams
fromString =
QueryParams
type alias QueryParams =
Dict String (List String)
{-| -}
toString : QueryParams -> String
toString (QueryParams queryParams) =
toString queryParams =
queryParams
|> Dict.toList
|> List.concatMap
(\( key, values ) ->
values
|> List.map
(\value ->
key ++ "=" ++ value
)
)
|> String.join "&"
{-| -}
parse : Parser a -> QueryParams -> Result String a
parse (Parser queryParser) queryParams =
queryParams
|> toDict
|> queryParser
{-| -}
toDict : QueryParams -> Dict String (List String)
toDict (QueryParams queryParams) =
fromString : String -> Dict String (List String)
fromString queryParams =
prepareQuery (Just queryParams)

View File

@ -651,7 +651,6 @@ expectQueryParam name =
maybeParamValue =
queryString
|> QueryParams.fromString
|> QueryParams.toDict
|> Dict.get name
|> Maybe.andThen List.head
in
@ -690,7 +689,6 @@ findFirstQueryParam : String -> String -> Maybe String
findFirstQueryParam name queryString =
queryString
|> QueryParams.fromString
|> QueryParams.toDict
|> Dict.get name
|> Maybe.andThen List.head
@ -705,7 +703,6 @@ queryParams =
|> Url.fromString
|> Maybe.andThen .query
|> Maybe.map QueryParams.fromString
|> Maybe.map QueryParams.toDict
|> Maybe.withDefault Dict.empty
)