Skip to content

Commit

Permalink
Extract Freckle.App.Env.keyValue
Browse files Browse the repository at this point in the history
Allow injecting the separating character too.
  • Loading branch information
pbrisbin committed Oct 25, 2023
1 parent 40e9a54 commit d1ea1b3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions library/Freckle/App/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Freckle.App.Env
, eitherReader
, time
, keyValues
, keyValue
, splitOnParse
, timeout
) where
Expand Down Expand Up @@ -137,10 +138,13 @@ time fmt =
-- >>> var keyValues "TAGS" mempty `parsePure` [("TAGS", "foo:bar,:bat")]
-- Left [("TAGS",UnreadError "Value bat has no key: \":bat\"")]
keyValues :: Reader Error [(Text, Text)]
keyValues = splitOnParse ',' $ eitherReader keyValue
keyValues = splitOnParse ',' $ keyValue ':'

keyValue :: Char -> Reader Error (Text, Text)
keyValue c =
eitherReader $ go . second (T.drop 1) . T.breakOn (T.singleton c) . pack
where
keyValue :: String -> Either String (Text, Text)
keyValue s = case second (T.drop 1) $ T.breakOn ":" $ pack s of
go = \case
(k, v) | T.null v -> Left $ "Key " <> unpack k <> " has no value"
(k, v) | T.null k -> Left $ "Value " <> unpack v <> " has no key"
(k, v) -> Right (k, v)
Expand Down

0 comments on commit d1ea1b3

Please sign in to comment.