on master help sign in
Drop lenses in favor of lightweight microlens

This is for #7

commit211713d5221128e056703986bd452456827dbea7 authorMichael Xavier <michael@michaelxavier.net> dateMon 14 Mar 2016, 21:35 (11 years ago) parentd48c70a
5 files changed+66−47
katip/katip.cabal+2−2 katip/src/Katip.hs+11−9 katip/src/Katip/Core.hs+34−27 katip/src/Katip/Scribes/Handle.hs+9−8 katip/test/Katip/Tests.hs+10−1
katip/katip.cabal+2−2
@@ -57,8 +57,6 @@ library
5757 , either >= 4 && < 4.5
5858 , exceptions >= 0.5 && < 0.9
5959 , hostname >=1.0 && <1.1
60− , lens >= 4.4 && <4.14
61− , lens-aeson < 1.1
6260 , old-locale >= 1.0 && < 1.1
6361 , string-conv >= 0.1 && < 0.2
6462 , template-haskell >= 2.8 && < 2.11
@@ -73,6 +71,8 @@ library
7371 , mtl >= 2.2 && < 2.3
7472 , transformers-base >= 0.4.1 && < 0.5
7573 , resourcet >= 1.1 && < 1.2
74+ , microlens >= 0.2.0.0 && < 0.5
75+ , microlens-th >= 0.1.0.0 && < 0.4
7676
7777 hs-source-dirs: src
7878 default-language: Haskell2010
katip/src/Katip.hs+11−9
@@ -32,11 +32,21 @@ module Katip
3232 , Environment (..)
3333 , Severity (..)
3434 , renderSeverity
35− , severityText
35+ , textToSeverity
3636 , Verbosity (..)
3737 , ToObject (..)
3838 , LogItem (..)
3939 , Item(..)
40+ , ThreadIdText(..)
41+ , PayloadSelection (..)
42+ , Scribe (..)
43+ , LogEnv (..)
44+ , KatipContext(..)
45+ , AnyLogContext
46+ , LogContexts, liftPayload
47+ , SimpleLogPayload, sl
48+
49+ -- ** @lens@-compatible Lenses
4050 , itemApp
4151 , itemEnv
4252 , itemSeverity
@@ -48,20 +58,12 @@ module Katip
4858 , itemTime
4959 , itemNamespace
5060 , itemLoc
51− , ThreadIdText(..)
52− , PayloadSelection (..)
53− , Scribe (..)
54− , LogEnv (..)
5561 , logEnvHost
5662 , logEnvPid
5763 , logEnvNs
5864 , logEnvEnv
5965 , logEnvTimer
6066 , logEnvScribes
61− , KatipContext(..)
62− , AnyLogContext
63− , LogContexts, liftPayload
64− , SimpleLogPayload, sl
6567
6668 -- * A Built-in Monad For Logging
6769 , KatipT (..)
katip/src/Katip/Core.hs+34−27
@@ -11,13 +11,14 @@
1111{-# LANGUAGE TemplateHaskell #-}
1212{-# LANGUAGE TypeFamilies #-}
1313{-# LANGUAGE UndecidableInstances #-}
14+-- | This module is not meant to be imported directly and may contain
15+-- internal mechanisms that will change without notice.
1416module Katip.Core where
1517
1618-------------------------------------------------------------------------------
1719import Control.Applicative as A
1820import Control.AutoUpdate
1921import Control.Concurrent
20−import Control.Lens
2122import Control.Monad.Base
2223import Control.Monad.Catch
2324import Control.Monad.IO.Class
@@ -47,11 +48,20 @@ import Data.Time
4748import GHC.Generics hiding (to)
4849import Language.Haskell.TH
4950import qualified Language.Haskell.TH.Syntax as TH
51+import Lens.Micro
52+import Lens.Micro.TH
5053import Network.HostName
5154import System.Posix
5255-------------------------------------------------------------------------------
5356
5457
58+readMay :: Read a => String -> Maybe a
59+readMay s = case [x | (x,t) <- reads s, ("","") <- lex t] of
60+ [x] -> Just x
61+ [] -> Nothing -- no parse
62+ _ -> Nothing -- Ambiguous parse
63+
64+
5565-------------------------------------------------------------------------------
5666-- | Represents a heirarchy of namespaces going from general to
5767-- specific. For instance: ["processname", "subsystem"]. Note that
@@ -116,27 +126,25 @@ renderSeverity s = case s of
116126
117127
118128-------------------------------------------------------------------------------
119−severityText :: Prism' Text Severity
120−severityText = prism renderSeverity toSev
121− where
122− toSev "Debug" = Right DebugS
123− toSev "Info" = Right InfoS
124− toSev "Notice" = Right NoticeS
125− toSev "Warning" = Right WarningS
126− toSev "Error" = Right ErrorS
127− toSev "Critical" = Right CriticalS
128− toSev "Alert" = Right AlertS
129− toSev "Emergency" = Right EmergencyS
130− toSev x = Left x
129+textToSeverity :: Text -> Maybe Severity
130+textToSeverity "Debug" = Just DebugS
131+textToSeverity "Info" = Just InfoS
132+textToSeverity "Notice" = Just NoticeS
133+textToSeverity "Warning" = Just WarningS
134+textToSeverity "Error" = Just ErrorS
135+textToSeverity "Critical" = Just CriticalS
136+textToSeverity "Alert" = Just AlertS
137+textToSeverity "Emergency" = Just EmergencyS
138+textToSeverity _ = Nothing
131139
132−instance ToJSON Severity where
133− toJSON s = A.String (s ^. re severityText)
134140
141+instance ToJSON Severity where
142+ toJSON s = A.String (renderSeverity s)
135143
136144instance FromJSON Severity where
137145 parseJSON = A.withText "Severity" parseSeverity
138146 where
139− parseSeverity t = case t ^? severityText of
147+ parseSeverity t = case textToSeverity t of
140148 Just x -> return x
141149 Nothing -> fail $ "Invalid Severity " ++ toS t
142150
@@ -289,13 +297,12 @@ instance FromJSON a => FromJSON (Item a) where
289297 <*> (fmap getLocJs <$> o A..: "loc")
290298
291299
292−processIDText :: Prism' Text ProcessID
293−processIDText = prism fromProcessID toProcessID
294− where
295− fromProcessID = toS . show
296− toProcessID t = case toS t ^? _Show of
297− Just i -> Right i
298− Nothing -> Left t
300+processIDToText :: ProcessID -> Text
301+processIDToText = toS . show
302+
303+
304+textToProcessID :: Text -> Maybe ProcessID
305+textToProcessID = readMay . toS
299306
300307
301308newtype ProcessIDJs = ProcessIDJs {
@@ -304,13 +311,13 @@ newtype ProcessIDJs = ProcessIDJs {
304311
305312
306313instance ToJSON ProcessIDJs where
307− toJSON (ProcessIDJs p) = A.String (p ^. re processIDText)
314+ toJSON (ProcessIDJs p) = A.String (processIDToText p)
308315
309316
310317instance FromJSON ProcessIDJs where
311318 parseJSON = A.withText "ProcessID" parseProcessID
312319 where
313− parseProcessID t = case t ^? processIDText of
320+ parseProcessID t = case textToProcessID t of
314321 Just p -> return $ ProcessIDJs p
315322 Nothing -> fail $ "Invalid ProcessIDJs " ++ toS t
316323
@@ -515,7 +522,7 @@ registerScribe
515522 -> Scribe
516523 -> LogEnv
517524 -> LogEnv
518−registerScribe nm h = logEnvScribes . at nm .~ Just h
525+registerScribe nm h = logEnvScribes %~ M.insert nm h
519526
520527
521528-------------------------------------------------------------------------------
@@ -526,7 +533,7 @@ unregisterScribe
526533 -- ^ Name of the scribe
527534 -> LogEnv
528535 -> LogEnv
529−unregisterScribe nm = logEnvScribes . at nm .~ Nothing
536+unregisterScribe nm = logEnvScribes %~ M.delete nm
530537
531538
532539-------------------------------------------------------------------------------
katip/src/Katip/Scribes/Handle.hs+9−8
@@ -3,15 +3,15 @@
33module Katip.Scribes.Handle where
44
55-------------------------------------------------------------------------------
6−import Control.Lens
76import Control.Monad
8−import Data.Aeson.Lens
7+import Data.Aeson
98import qualified Data.HashMap.Strict as HM
109import Data.Monoid
1110import Data.Text.Lazy.Builder
1211import Data.Text.Lazy.IO as T
1312import Data.Time
1413import qualified Data.Time.Locale.Compat as LC
14+import Lens.Micro
1515import System.IO
1616import System.IO.Unsafe (unsafePerformIO)
1717-------------------------------------------------------------------------------
@@ -29,15 +29,16 @@ getKeys :: LogItem s => Verbosity -> s -> [Builder]
2929getKeys verb a = payloadObject verb a ^..
3030 to HM.toList . traverse . to rendPair
3131 where
32− rendPair (k,v) = fromText k <> fromText ":" <> (v ^. _Primitive . to renderPrim)
32+ rendPair (k,v) = fromText k <> fromText ":" <> (renderPrim v)
3333
3434
3535-------------------------------------------------------------------------------
36−renderPrim :: Primitive -> Builder
37−renderPrim (StringPrim t) = fromText t
38−renderPrim (NumberPrim s) = fromString (show s)
39−renderPrim (BoolPrim b) = fromString (show b)
40−renderPrim NullPrim = fromText "null"
36+renderPrim :: Value -> Builder
37+renderPrim (String t) = fromText t
38+renderPrim (Number s) = fromString (show s)
39+renderPrim (Bool b) = fromString (show b)
40+renderPrim Null = fromText "null"
41+renderPrim _ = mempty
4142
4243
4344-------------------------------------------------------------------------------
katip/test/Katip/Tests.hs+10−1
@@ -1,4 +1,5 @@
11{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+{-# LANGUAGE OverloadedStrings #-}
23{-# LANGUAGE ScopedTypeVariables #-}
34{-# LANGUAGE StandaloneDeriving #-}
45{-# OPTIONS_GHC -fno-warn-orphans #-}
@@ -17,9 +18,11 @@ import Language.Haskell.TH
1718import System.Posix.Types
1819import Test.QuickCheck.Instances ()
1920import Test.Tasty
21+import Test.Tasty.HUnit
2022import Test.Tasty.QuickCheck
2123-------------------------------------------------------------------------------
2224import Katip
25+import Katip.Core
2326-------------------------------------------------------------------------------
2427
2528
@@ -27,7 +30,13 @@ tests :: TestTree
2730tests = testGroup "Katip"
2831 [
2932 testProperty "JSON cycle Item" $ \(i :: Item ()) ->
30− prop_json_cycle i
33+ prop_json_cycle i
34+ , testProperty "renderSeverity/textToSeverity cycle" $ \sev ->
35+ textToSeverity(renderSeverity sev) === Just sev
36+ , testProperty "processIDToText/textToProcessID cycle" $ \pid ->
37+ textToProcessID (processIDToText pid) === Just pid
38+ , testCase "processIDToText is just the number" $ do
39+ processIDToText 123 @?= "123"
3140 ]
3241
3342