Skip to content

Commit

Permalink
[#66] Prepare command and JSON deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclavsvejcar committed Apr 9, 2021
1 parent 99b0c2a commit 11ab0af
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Headroom/Command/Readers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Headroom.Command.Readers
( licenseReader
, licenseTypeReader
, regexReader
, templateRefReader
, parseLicense
)
where
Expand All @@ -30,6 +31,10 @@ import Headroom.Data.Regex ( Regex(..)
, compile
)
import Headroom.FileType.Types ( FileType(..) )
import Headroom.Meta ( TemplateType )
import Headroom.Template.TemplateRef ( TemplateRef(..)
, mkTemplateRef
)
import Options.Applicative
import RIO
import qualified RIO.Text as T
Expand Down Expand Up @@ -69,6 +74,14 @@ regexReader =
in eitherReader parse


-- | Reader for 'TemplateRef'.
templateRefReader :: ReadM TemplateRef
templateRefReader =
let parse input = mapLeft displayException
(mkTemplateRef @TemplateType . T.pack $ input)
in eitherReader parse


-- | Parses 'LicenseType' and 'FileType' from the input string,
-- formatted as @licenseType:fileType@.
--
Expand Down
4 changes: 3 additions & 1 deletion src/Headroom/Command/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ chooseAction info header = do
justify = T.justifyLeft 30 ' '


-- | Loads templates using given template references.
-- | Loads templates using given template references. If multiple sources define
-- template for the same 'FileType', then the preferred one (based on ordering
-- of 'TemplateRef' is selected).
loadTemplateRefs :: forall a env
. ( Template a
, Has (Network (RIO env)) env
Expand Down
10 changes: 10 additions & 0 deletions src/Headroom/Template/TemplateRef.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ module Headroom.Template.TemplateRef
)
where

import Data.Aeson ( FromJSON(..)
, Value(String)
)
import Data.String.Interpolate ( iii )
import Headroom.Data.EnumExtra ( textToEnum )
import Headroom.Data.Regex ( match
, re
)
import Headroom.FileType.Types ( FileType(..) )
import Headroom.Meta ( TemplateType )
import Headroom.Template ( Template(..) )
import Headroom.Types ( fromHeadroomError
, toHeadroomError
Expand All @@ -63,6 +67,12 @@ data TemplateRef
deriving (Eq, Ord, Show)


instance FromJSON TemplateRef where
parseJSON = \case
String s -> maybe (error $ T.unpack s) pure (mkTemplateRef @TemplateType s)
other -> error $ "Invalid value for template reference: " <> show other


------------------------------ PUBLIC FUNCTIONS ------------------------------

-- | Creates a 'TemplateRef' from given text. If the raw text appears to be
Expand Down
8 changes: 8 additions & 0 deletions test/Headroom/Template/TemplateRefSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Headroom.Template.TemplateRefSpec
where


import qualified Data.Aeson as Aeson
import Headroom.Template.Mustache ( Mustache )
import Headroom.Template.TemplateRef
import RIO
Expand Down Expand Up @@ -57,6 +58,13 @@ spec = do
renderRef sample `shouldBe` expected


describe "FromJSON instance for TemplateRef" $ do
it "deserializes TemplateRef from JSON value" $ do
let sample = "\"http://foo/haskell.mustache\""
expected = UriTemplateRef [uri|http://foo/haskell.mustache|]
Aeson.decode sample `shouldBe` Just expected


describe "Ord instance for TemplateRef" $ do
it "should properly order records" $ do
let sample =
Expand Down

0 comments on commit 11ab0af

Please sign in to comment.