Skip to content

Commit

Permalink
queryFromURI should be stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Apr 18, 2020
1 parent a7c63ab commit 9eb34bd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/Neuron/CLI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Neuron.CLI.Types
Expand Down Expand Up @@ -116,4 +117,8 @@ commandParser defaultNotesDir = do
ribServe <- Rib.Cli.serveOption
pure RibConfig {..}
uriReader =
eitherReader $ bimap displayException Z.queryFromURI . URI.mkURI . toText
eitherReader $ \(toText -> s) -> case URI.mkURI s of
Right uri ->
first toString $ Z.queryFromURI uri
Left e ->
Left $ displayException e
6 changes: 3 additions & 3 deletions src/Neuron/Zettelkasten/Link.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data ZLink
ZLink_QueryZettels Connection LinkTheme [Query]
deriving (Eq, Show)

mkZLink :: MarkdownLink -> Maybe ZLink
mkZLink :: HasCallStack => MarkdownLink -> Maybe ZLink
mkZLink MarkdownLink {markdownLinkUri = uri, markdownLinkText = linkText} =
-- NOTE: We should probably drop the 'cf' variants in favour of specifying
-- the connection type as a query param or something.
Expand All @@ -42,9 +42,9 @@ mkZLink MarkdownLink {markdownLinkUri = uri, markdownLinkText = linkText} =
let zid = parseZettelID linkText
in Just $ ZLink_ConnectZettel OrdinaryConnection zid
Just "zquery" ->
Just $ ZLink_QueryZettels Folgezettel (linkThemeFromURI uri) (queryFromURI uri)
Just $ ZLink_QueryZettels Folgezettel (linkThemeFromURI uri) (either error id $ queryFromURI uri)
Just "zcfquery" ->
Just $ ZLink_QueryZettels OrdinaryConnection (linkThemeFromURI uri) (queryFromURI uri)
Just $ ZLink_QueryZettels OrdinaryConnection (linkThemeFromURI uri) (either error id $ queryFromURI uri)
_ -> do
let uriS = URI.render uri
guard $ uriS == linkText
Expand Down
18 changes: 11 additions & 7 deletions src/Neuron/Zettelkasten/Query.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | Queries to the Zettel store
module Neuron.Zettelkasten.Query where

import Control.Monad.Except
import qualified Data.Map.Strict as Map
import Lucid
import Neuron.Zettelkasten.Store
Expand Down Expand Up @@ -43,14 +44,17 @@ instance ToHtml [Query] where

type QueryResults = [Zettel]

queryFromURI :: URI.URI -> [Query]
queryFromURI :: MonadError Text m => URI.URI -> m [Query]
queryFromURI uri =
flip mapMaybe (URI.uriQuery uri) $ \case
URI.QueryParam (URI.unRText -> key) (URI.unRText -> val) ->
case key of
"tag" -> Just $ Query_ZettelsByTag (TagPattern $ toString val)
case fmap URI.unRText (URI.uriScheme uri) of
Just proto | proto `elem` ["zquery", "zcfquery"] ->
pure $ flip mapMaybe (URI.uriQuery uri) $ \case
URI.QueryParam (URI.unRText -> key) (URI.unRText -> val) ->
case key of
"tag" -> Just $ Query_ZettelsByTag (TagPattern $ toString val)
_ -> Nothing
_ -> Nothing
_ -> Nothing
_ -> throwError "Bad URI (expected: zquery: or zcfquery:)"

matchQuery :: Zettel -> Query -> Bool
matchQuery Zettel {..} = \case
Expand Down
4 changes: 2 additions & 2 deletions test/Neuron/Zettelkasten/LinkSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ zLinkCases =
),
( "zquery: link",
(Right (".", "zquery://search?tag=science")),
Just $ ZLink_QueryZettels Folgezettel LinkTheme_Default [ByTag $ TagPattern "science"]
Just $ ZLink_QueryZettels Folgezettel LinkTheme_Default [Query_ZettelsByTag $ TagPattern "science"]
),
( "zcfquery: link, with link theme",
(Right (".", "zcfquery://search?tag=science&linkTheme=withDate")),
Just $ ZLink_QueryZettels OrdinaryConnection LinkTheme_WithDate [ByTag $ TagPattern "science"]
Just $ ZLink_QueryZettels OrdinaryConnection LinkTheme_WithDate [Query_ZettelsByTag $ TagPattern "science"]
),
( "normal link",
(Left "https://www.google.com"),
Expand Down
11 changes: 6 additions & 5 deletions test/Neuron/Zettelkasten/QuerySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ spec =
it "Parse all zettels URI" $
parseQueryString "zquery://search" `shouldBe` Right []
it "Parse single tag" $
parseQueryString "zquery://search?tag=foo" `shouldBe` Right [ByTag $ TagPattern "foo"]
parseQueryString "zquery://search?tag=foo" `shouldBe` Right [Query_ZettelsByTag $ TagPattern "foo"]
it "Parse hierarchical tag" $ do
parseQueryString "zquery://search?tag=foo/bar" `shouldBe` Right [ByTag $ TagPattern "foo/bar"]
parseQueryString "zquery://search?tag=foo/bar" `shouldBe` Right [Query_ZettelsByTag $ TagPattern "foo/bar"]
it "Parse tag pattern" $ do
parseQueryString "zquery://search?tag=foo/**/bar/*/baz" `shouldBe` Right [ByTag $ TagPattern "foo/**/bar/*/baz"]
parseQueryString "zquery://search?tag=foo/**/bar/*/baz" `shouldBe` Right [Query_ZettelsByTag $ TagPattern "foo/**/bar/*/baz"]
it "Parse multiple tags" $
parseQueryString "zquery://search?tag=foo&tag=bar"
`shouldBe` Right [ByTag $ TagPattern "foo", ByTag $ TagPattern "bar"]
`shouldBe` Right [Query_ZettelsByTag $ TagPattern "foo", Query_ZettelsByTag $ TagPattern "bar"]
where
parseQueryString :: Text -> Either Text [Query]
parseQueryString =
bimap displayException queryFromURI . mkURI
either (Left . toText . displayException) queryFromURI . mkURI

0 comments on commit 9eb34bd

Please sign in to comment.