From 26262d3bb4e2df438bd655c9fd4e4b3b6881be94 Mon Sep 17 00:00:00 2001 From: pingu Date: Wed, 16 Oct 2024 18:56:49 +0200 Subject: [PATCH 1/2] Linsen fixes once again --- src/Model/Linsen.hs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Model/Linsen.hs b/src/Model/Linsen.hs index 9f06a6d..1080530 100644 --- a/src/Model/Linsen.hs +++ b/src/Model/Linsen.hs @@ -11,6 +11,7 @@ where import Control.Monad ( (>=>) , (<=<) , zipWithM + , filterM , ap ) import Control.Monad.Catch ( MonadThrow ) import Control.Monad.IO.Class ( MonadIO ) @@ -21,9 +22,11 @@ import Data.Aeson.Types ( Parser , parseEither ) import Data.Bifunctor ( first ) import qualified Data.ByteString.Lazy.Char8 as BL8 +import Data.Char ( isSpace ) import Data.Functor ( (<&>) ) import Data.List.Extra ( (!?) ) import Data.Text.Lazy ( Text + , all , replace , strip ) import Data.Thyme ( parseTime @@ -36,6 +39,7 @@ import Network.HTTP.Req import Model.Types ( NoMenu(..) , Menu(..) , Restaurant ( Restaurant ) ) +import Prelude hiding ( all ) import Util ( menusToEitherNoLunch ) swedishTimeLocale :: TimeLocale @@ -72,9 +76,9 @@ swedishTimeLocale = TimeLocale } pattern MeatDish, FishDish, VegDish :: Integer -pattern MeatDish = 3 -pattern FishDish = 8 -pattern VegDish = 13 +pattern MeatDish = 2 +pattern FishDish = 6 +pattern VegDish = 10 fetch :: (MonadHttp m, MonadIO m, MonadThrow m) @@ -107,6 +111,13 @@ parse day = >=> (.: "richText") >=> (.: "root") >=> (.: "children") + >=> filterM (withObject "filter whitespace" + $ (.: "children") + >=> \case + [] -> fail "no text" + (v:_) -> pure v + >=> (.: "text") + >=> (pure . not . all isSpace)) >=> (\v' -> (case v' !? 1 of Nothing -> fail "failed to index into food" From 934727bd1f8e9c177ef7dba3ba0b66ac3c84dad2 Mon Sep 17 00:00:00 2001 From: pingu Date: Wed, 16 Oct 2024 19:04:48 +0200 Subject: [PATCH 2/2] Haha, tests --- src/Model/Linsen.hs | 2 +- test/Main.hs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Model/Linsen.hs b/src/Model/Linsen.hs index 1080530..e0797f6 100644 --- a/src/Model/Linsen.hs +++ b/src/Model/Linsen.hs @@ -114,7 +114,7 @@ parse day = >=> filterM (withObject "filter whitespace" $ (.: "children") >=> \case - [] -> fail "no text" + [] -> fail "Empty list" (v:_) -> pure v >=> (.: "text") >=> (pure . not . all isSpace)) diff --git a/test/Main.hs b/test/Main.hs index 9cfdf7d..c95b7da 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -105,7 +105,7 @@ main = hspec $ do "parses a blob of JSON without error, that has no lunch" (do s2 <- BL.readFile "test/linsen2.json" -- Test that has no lunch - testFun (Left (NMParseError "Error in $: Unable to parse day" undefined)) + testFun (Left (NMParseError "Error in $: Empty list" undefined)) (L.parse (fromGregorian 2024 06 06) (fromJust $ decode s2)) @@ -116,7 +116,7 @@ main = hspec $ do "parses a blob of JSON without error, that has the wrong week" (do s3 <- BL.readFile "test/linsen3.json" - testFun (Left (NMParseError "Error in $: Unable to parse day" undefined)) + testFun (Left (NMParseError "Error in $: Empty list" undefined)) (L.parse (fromGregorian 2024 06 26) (fromJust $ decode s3))