Use Set instead of Dict with unused value.

This commit is contained in:
Dillon Kearns 2022-02-04 10:14:43 -08:00
parent 6da67d070d
commit 3c5b171458
4 changed files with 70 additions and 89 deletions

View File

@ -87,8 +87,9 @@ import Dict exposing (Dict)
import Pages.Internal.ApplicationType exposing (ApplicationType)
import Pages.Internal.StaticHttpBody as Body
import Pages.StaticHttp.Request as HashRequest
import Pages.StaticHttpRequest exposing (RawRequest(..), WhatToDo)
import Pages.StaticHttpRequest exposing (RawRequest(..))
import RequestsAndPending exposing (RequestsAndPending)
import Set exposing (Set)
{-| A DataSource represents data that will be gathered at build time. Multiple `DataSource`s can be combined together using the `mapN` functions,
@ -256,17 +257,17 @@ map2 fn request1 request2 =
This is assuming that there are no duplicate URLs, so it can safely choose between either a raw or a reduced response.
It would not work correctly if it chose between two responses that were reduced with different `Json.Decode.Exploration.Decoder`s.
-}
combineReducedDicts : Dict String WhatToDo -> Dict String WhatToDo -> Dict String WhatToDo
combineReducedDicts : Set String -> Set String -> Set String
combineReducedDicts dict1 dict2 =
Dict.union dict1 dict2
Set.union dict1 dict2
lookup : ApplicationType -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error ( Dict String WhatToDo, value )
lookup : ApplicationType -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error ( Set String, value )
lookup =
lookupHelp Dict.empty
lookupHelp Set.empty
lookupHelp : Dict String WhatToDo -> ApplicationType -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error ( Dict String WhatToDo, value )
lookupHelp : Set String -> ApplicationType -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error ( Set String, value )
lookupHelp strippedSoFar appType requestInfo rawResponses =
case requestInfo of
RequestError error ->
@ -339,7 +340,7 @@ from the previous response to build up the URL, headers, etc. that you send to t
andThen : (a -> DataSource b) -> DataSource a -> DataSource b
andThen fn requestInfo =
-- TODO should this be non-empty Dict? Or should it be passed down some other way?
Request Dict.empty
Request Set.empty
( lookupUrls requestInfo
, \appType rawResponses ->
lookup
@ -400,7 +401,7 @@ andMap =
-}
succeed : a -> DataSource a
succeed value =
ApiRoute Dict.empty value
ApiRoute Set.empty value
{-| Stop the StaticHttp chain with the given error message. If you reach a `fail` in your request,

View File

@ -59,6 +59,7 @@ import Pages.Internal.StaticHttpBody as Body
import Pages.StaticHttp.Request as HashRequest
import Pages.StaticHttpRequest exposing (RawRequest(..))
import RequestsAndPending
import Set exposing (Set)
{-| Build an empty body for a DataSource.Http request. See [elm/http's `Http.emptyBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#emptyBody).
@ -207,7 +208,7 @@ request :
request request_ expect =
case expect of
ExpectJson decoder ->
Request Dict.empty
Request Set.empty
( [ request_ ]
, \_ rawResponseDict ->
rawResponseDict
@ -216,9 +217,7 @@ request request_ expect =
case maybeResponse of
Just rawResponse ->
Ok
( -- TODO check keepOrDiscard
Dict.singleton (request_ |> HashRequest.hash)
Pages.StaticHttpRequest.UseRawResponse
( Set.singleton (request_ |> HashRequest.hash)
, rawResponse
)
@ -247,9 +246,8 @@ request request_ expect =
(\finalRequest ->
( -- TODO check keepOrDiscard
strippedResponses
|> Dict.insert
|> Set.insert
(request_ |> HashRequest.hash)
Pages.StaticHttpRequest.UseRawResponse
, finalRequest
)
)
@ -258,7 +256,7 @@ request request_ expect =
)
ExpectString mapStringFn ->
Request Dict.empty
Request Set.empty
( [ request_ ]
, \_ rawResponseDict ->
rawResponseDict
@ -268,7 +266,7 @@ request request_ expect =
Just rawResponse ->
Ok
( -- TODO check keepOrDiscard
Dict.singleton (request_ |> HashRequest.hash) Pages.StaticHttpRequest.UseRawResponse
Set.singleton (request_ |> HashRequest.hash)
, rawResponse
)
@ -287,7 +285,7 @@ request request_ expect =
(\finalRequest ->
( -- TODO check keepOrDiscard
strippedResponses
|> Dict.insert (request_ |> HashRequest.hash) Pages.StaticHttpRequest.UseRawResponse
|> Set.insert (request_ |> HashRequest.hash)
, finalRequest
)
)
@ -296,7 +294,7 @@ request request_ expect =
)
toResult : Result Pages.StaticHttpRequest.Error ( Dict.Dict String Pages.StaticHttpRequest.WhatToDo, b ) -> RawRequest b
toResult : Result Pages.StaticHttpRequest.Error ( Set String, b ) -> RawRequest b
toResult result =
case result of
Err error ->

View File

@ -266,6 +266,43 @@ nextStep config ({ allRawResponses, errors } as model) maybeRoutes =
else
True
)
failedRequests : List BuildError
failedRequests =
staticResponses
|> Dict.toList
|> List.concatMap
(\( path, NotFetched request _ ) ->
let
staticRequestsStatus : StaticHttpRequest.Status ()
staticRequestsStatus =
StaticHttpRequest.cacheRequestResolution
ApplicationType.Cli
request
usableRawResponses
usableRawResponses : RequestsAndPending
usableRawResponses =
allRawResponses
maybePermanentError : Maybe StaticHttpRequest.Error
maybePermanentError =
case staticRequestsStatus of
StaticHttpRequest.HasPermanentError theError ->
Just theError
_ ->
Nothing
decoderErrors : List BuildError
decoderErrors =
maybePermanentError
|> Maybe.map (StaticHttpRequest.toBuildError path)
|> Maybe.map List.singleton
|> Maybe.withDefault []
in
decoderErrors
)
in
if pendingRequests then
let
@ -360,43 +397,6 @@ nextStep config ({ allRawResponses, errors } as model) maybeRoutes =
allErrors =
errors ++ failedRequests ++ generatedFileErrors
failedRequests : List BuildError
failedRequests =
staticResponses
|> Dict.toList
|> List.concatMap
(\( path, NotFetched request _ ) ->
let
staticRequestsStatus : StaticHttpRequest.Status ()
staticRequestsStatus =
StaticHttpRequest.cacheRequestResolution
ApplicationType.Cli
request
usableRawResponses
usableRawResponses : RequestsAndPending
usableRawResponses =
allRawResponses
maybePermanentError : Maybe StaticHttpRequest.Error
maybePermanentError =
case staticRequestsStatus of
StaticHttpRequest.HasPermanentError theError ->
Just theError
_ ->
Nothing
decoderErrors : List BuildError
decoderErrors =
maybePermanentError
|> Maybe.map (StaticHttpRequest.toBuildError path)
|> Maybe.map List.singleton
|> Maybe.withDefault []
in
decoderErrors
)
generatedFileErrors : List BuildError
generatedFileErrors =
generatedFiles

View File

@ -1,4 +1,4 @@
module Pages.StaticHttpRequest exposing (Error(..), RawRequest(..), Status(..), WhatToDo(..), cacheRequestResolution, resolve, resolveUrls, strippedResponsesEncode, toBuildError)
module Pages.StaticHttpRequest exposing (Error(..), RawRequest(..), Status(..), cacheRequestResolution, resolve, resolveUrls, strippedResponsesEncode, toBuildError)
import BuildError exposing (BuildError)
import Dict exposing (Dict)
@ -6,42 +6,32 @@ import List.Extra
import Pages.Internal.ApplicationType exposing (ApplicationType)
import Pages.StaticHttp.Request
import RequestsAndPending exposing (RequestsAndPending)
import Set exposing (Set)
import TerminalText as Terminal
type RawRequest value
= Request (Dict String WhatToDo) ( List Pages.StaticHttp.Request.Request, ApplicationType -> RequestsAndPending -> RawRequest value )
= Request (Set String) ( List Pages.StaticHttp.Request.Request, ApplicationType -> RequestsAndPending -> RawRequest value )
| RequestError Error
| ApiRoute (Dict String WhatToDo) value
| ApiRoute (Set String) value
type WhatToDo
= UseRawResponse
merge : String -> WhatToDo -> WhatToDo -> WhatToDo
merge _ _ _ =
UseRawResponse
strippedResponses : ApplicationType -> RawRequest value -> RequestsAndPending -> Dict String WhatToDo
strippedResponses : ApplicationType -> RawRequest value -> RequestsAndPending -> Set String
strippedResponses =
strippedResponsesHelp Dict.empty
strippedResponsesHelp Set.empty
strippedResponsesEncode : ApplicationType -> RawRequest value -> RequestsAndPending -> Result (List BuildError) (Dict String String)
strippedResponsesEncode appType rawRequest requestsAndPending =
strippedResponses appType rawRequest requestsAndPending
|> Dict.toList
|> Set.toList
|> List.map
(\( k, whatToDo ) ->
(case whatToDo of
UseRawResponse ->
Dict.get k requestsAndPending
|> Maybe.withDefault Nothing
|> Maybe.withDefault ""
|> Just
|> Ok
(\k ->
(Dict.get k requestsAndPending
|> Maybe.withDefault Nothing
|> Maybe.withDefault ""
|> Just
|> Ok
)
|> Result.map (Maybe.map (Tuple.pair k))
)
@ -75,7 +65,7 @@ combineMultipleErrors results =
results
strippedResponsesHelp : Dict String WhatToDo -> ApplicationType -> RawRequest value -> RequestsAndPending -> Dict String WhatToDo
strippedResponsesHelp : Set String -> ApplicationType -> RawRequest value -> RequestsAndPending -> Set String
strippedResponsesHelp usedSoFar appType request rawResponses =
case request of
RequestError _ ->
@ -85,26 +75,18 @@ strippedResponsesHelp usedSoFar appType request rawResponses =
case lookupFn appType rawResponses of
followupRequest ->
strippedResponsesHelp
(Dict.merge
(\key a -> Dict.insert key a)
(\key a b -> Dict.insert key (merge key a b))
(\key b -> Dict.insert key b)
(Set.union
usedSoFar
partiallyStrippedResponses
Dict.empty
)
appType
followupRequest
rawResponses
ApiRoute partiallyStrippedResponses _ ->
Dict.merge
(\key a -> Dict.insert key a)
(\key a b -> Dict.insert key (merge key a b))
(\key b -> Dict.insert key b)
Set.union
usedSoFar
partiallyStrippedResponses
Dict.empty
type Error