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.ApplicationType exposing (ApplicationType)
import Pages.Internal.StaticHttpBody as Body import Pages.Internal.StaticHttpBody as Body
import Pages.StaticHttp.Request as HashRequest import Pages.StaticHttp.Request as HashRequest
import Pages.StaticHttpRequest exposing (RawRequest(..), WhatToDo) import Pages.StaticHttpRequest exposing (RawRequest(..))
import RequestsAndPending exposing (RequestsAndPending) 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, {-| 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. 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. 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 = 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 = 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 = lookupHelp strippedSoFar appType requestInfo rawResponses =
case requestInfo of case requestInfo of
RequestError error -> 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 : (a -> DataSource b) -> DataSource a -> DataSource b
andThen fn requestInfo = andThen fn requestInfo =
-- TODO should this be non-empty Dict? Or should it be passed down some other way? -- TODO should this be non-empty Dict? Or should it be passed down some other way?
Request Dict.empty Request Set.empty
( lookupUrls requestInfo ( lookupUrls requestInfo
, \appType rawResponses -> , \appType rawResponses ->
lookup lookup
@ -400,7 +401,7 @@ andMap =
-} -}
succeed : a -> DataSource a succeed : a -> DataSource a
succeed value = 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, {-| 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.StaticHttp.Request as HashRequest
import Pages.StaticHttpRequest exposing (RawRequest(..)) import Pages.StaticHttpRequest exposing (RawRequest(..))
import RequestsAndPending 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). {-| 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 = request request_ expect =
case expect of case expect of
ExpectJson decoder -> ExpectJson decoder ->
Request Dict.empty Request Set.empty
( [ request_ ] ( [ request_ ]
, \_ rawResponseDict -> , \_ rawResponseDict ->
rawResponseDict rawResponseDict
@ -216,9 +217,7 @@ request request_ expect =
case maybeResponse of case maybeResponse of
Just rawResponse -> Just rawResponse ->
Ok Ok
( -- TODO check keepOrDiscard ( Set.singleton (request_ |> HashRequest.hash)
Dict.singleton (request_ |> HashRequest.hash)
Pages.StaticHttpRequest.UseRawResponse
, rawResponse , rawResponse
) )
@ -247,9 +246,8 @@ request request_ expect =
(\finalRequest -> (\finalRequest ->
( -- TODO check keepOrDiscard ( -- TODO check keepOrDiscard
strippedResponses strippedResponses
|> Dict.insert |> Set.insert
(request_ |> HashRequest.hash) (request_ |> HashRequest.hash)
Pages.StaticHttpRequest.UseRawResponse
, finalRequest , finalRequest
) )
) )
@ -258,7 +256,7 @@ request request_ expect =
) )
ExpectString mapStringFn -> ExpectString mapStringFn ->
Request Dict.empty Request Set.empty
( [ request_ ] ( [ request_ ]
, \_ rawResponseDict -> , \_ rawResponseDict ->
rawResponseDict rawResponseDict
@ -268,7 +266,7 @@ request request_ expect =
Just rawResponse -> Just rawResponse ->
Ok Ok
( -- TODO check keepOrDiscard ( -- TODO check keepOrDiscard
Dict.singleton (request_ |> HashRequest.hash) Pages.StaticHttpRequest.UseRawResponse Set.singleton (request_ |> HashRequest.hash)
, rawResponse , rawResponse
) )
@ -287,7 +285,7 @@ request request_ expect =
(\finalRequest -> (\finalRequest ->
( -- TODO check keepOrDiscard ( -- TODO check keepOrDiscard
strippedResponses strippedResponses
|> Dict.insert (request_ |> HashRequest.hash) Pages.StaticHttpRequest.UseRawResponse |> Set.insert (request_ |> HashRequest.hash)
, finalRequest , 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 = toResult result =
case result of case result of
Err error -> Err error ->

View File

@ -266,6 +266,43 @@ nextStep config ({ allRawResponses, errors } as model) maybeRoutes =
else else
True 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 in
if pendingRequests then if pendingRequests then
let let
@ -360,43 +397,6 @@ nextStep config ({ allRawResponses, errors } as model) maybeRoutes =
allErrors = allErrors =
errors ++ failedRequests ++ generatedFileErrors 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 : List BuildError
generatedFileErrors = generatedFileErrors =
generatedFiles 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 BuildError exposing (BuildError)
import Dict exposing (Dict) import Dict exposing (Dict)
@ -6,42 +6,32 @@ import List.Extra
import Pages.Internal.ApplicationType exposing (ApplicationType) import Pages.Internal.ApplicationType exposing (ApplicationType)
import Pages.StaticHttp.Request import Pages.StaticHttp.Request
import RequestsAndPending exposing (RequestsAndPending) import RequestsAndPending exposing (RequestsAndPending)
import Set exposing (Set)
import TerminalText as Terminal import TerminalText as Terminal
type RawRequest value 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 | RequestError Error
| ApiRoute (Dict String WhatToDo) value | ApiRoute (Set String) value
type WhatToDo strippedResponses : ApplicationType -> RawRequest value -> RequestsAndPending -> Set String
= UseRawResponse
merge : String -> WhatToDo -> WhatToDo -> WhatToDo
merge _ _ _ =
UseRawResponse
strippedResponses : ApplicationType -> RawRequest value -> RequestsAndPending -> Dict String WhatToDo
strippedResponses = strippedResponses =
strippedResponsesHelp Dict.empty strippedResponsesHelp Set.empty
strippedResponsesEncode : ApplicationType -> RawRequest value -> RequestsAndPending -> Result (List BuildError) (Dict String String) strippedResponsesEncode : ApplicationType -> RawRequest value -> RequestsAndPending -> Result (List BuildError) (Dict String String)
strippedResponsesEncode appType rawRequest requestsAndPending = strippedResponsesEncode appType rawRequest requestsAndPending =
strippedResponses appType rawRequest requestsAndPending strippedResponses appType rawRequest requestsAndPending
|> Dict.toList |> Set.toList
|> List.map |> List.map
(\( k, whatToDo ) -> (\k ->
(case whatToDo of (Dict.get k requestsAndPending
UseRawResponse -> |> Maybe.withDefault Nothing
Dict.get k requestsAndPending |> Maybe.withDefault ""
|> Maybe.withDefault Nothing |> Just
|> Maybe.withDefault "" |> Ok
|> Just
|> Ok
) )
|> Result.map (Maybe.map (Tuple.pair k)) |> Result.map (Maybe.map (Tuple.pair k))
) )
@ -75,7 +65,7 @@ combineMultipleErrors results =
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 = strippedResponsesHelp usedSoFar appType request rawResponses =
case request of case request of
RequestError _ -> RequestError _ ->
@ -85,26 +75,18 @@ strippedResponsesHelp usedSoFar appType request rawResponses =
case lookupFn appType rawResponses of case lookupFn appType rawResponses of
followupRequest -> followupRequest ->
strippedResponsesHelp strippedResponsesHelp
(Dict.merge (Set.union
(\key a -> Dict.insert key a)
(\key a b -> Dict.insert key (merge key a b))
(\key b -> Dict.insert key b)
usedSoFar usedSoFar
partiallyStrippedResponses partiallyStrippedResponses
Dict.empty
) )
appType appType
followupRequest followupRequest
rawResponses rawResponses
ApiRoute partiallyStrippedResponses _ -> ApiRoute partiallyStrippedResponses _ ->
Dict.merge Set.union
(\key a -> Dict.insert key a)
(\key a b -> Dict.insert key (merge key a b))
(\key b -> Dict.insert key b)
usedSoFar usedSoFar
partiallyStrippedResponses partiallyStrippedResponses
Dict.empty
type Error type Error