@@ -10,18 +10,20 @@ module System.Etc.Internal.Types
1010 , module System.Etc.Internal.Spec.Types
1111 ) where
1212
13- import RIO
13+ import RIO hiding ( (<>) )
1414import qualified RIO.HashMap as HashMap
1515import qualified RIO.Set as Set
1616import qualified RIO.Text as Text
17- import qualified RIO.Vector as Vector
1817
1918import Text.PrettyPrint.ANSI.Leijen (Doc , (<+>) )
2019import qualified Text.PrettyPrint.ANSI.Leijen as Doc
2120
21+
22+
2223import Control.Exception (throw )
2324
2425import Data.Bool (bool )
26+ import Data.Monoid ((<>) )
2527import qualified Data.Semigroup as Semigroup
2628import Data.Typeable (cast , typeOf )
2729
@@ -73,10 +75,10 @@ data FileValueOrigin
7375instance NFData FileValueOrigin
7476
7577class IConfigSource source where
76- sourceValue :: source -> Value JSON. Value
77- sourcePretty :: ( JSON. Value -> Doc ) -> source -> ([ Doc ], Doc )
78- compareValues :: source -> source -> Ordering
79- compareValues _ _ = EQ
78+ sourceValue :: source -> Value JSON. Value
79+ sourcePrettyDoc :: source -> Doc
80+ compareSources :: source -> source -> Ordering
81+ compareSources _ _ = EQ
8082
8183data SomeConfigSource =
8284 forall source . ( Show source
@@ -101,25 +103,16 @@ data InvalidConfigSourceComparison
101103
102104instance Exception InvalidConfigSourceComparison
103105
104- renderConfigValue :: (JSON. Value -> Doc ) -> Value JSON. Value -> [Doc ]
105- renderConfigValue f value = case value of
106- Plain (JSON. Array jsonArray) ->
107- Vector. toList $ Vector. map (\ jsonValue -> Doc. text " -" <+> f jsonValue) jsonArray
108- Plain jsonValue -> return $ f jsonValue
109- Sensitive {} -> return $ Doc. text " <<sensitive>>"
110-
111106instance IConfigSource SomeConfigSource where
112- sourceValue (SomeConfigSource _ inner) =
113- sourceValue inner
114- sourcePretty f (SomeConfigSource _ inner) =
115- sourcePretty f inner
116- compareValues x@ (SomeConfigSource ia a) y@ (SomeConfigSource ib b)
107+ sourcePrettyDoc (SomeConfigSource _ inner) = sourcePrettyDoc inner
108+ sourceValue (SomeConfigSource _ inner) = sourceValue inner
109+ compareSources x@ (SomeConfigSource ia a) y@ (SomeConfigSource ib b)
117110 | ia == ib =
118111 if fromValue (sourceValue a) == JSON. Null && fromValue (sourceValue b) == JSON. Null then
119112 EQ
120113 else if typeOf a == typeOf b then
121114 let b' = fromMaybe (throw (InvalidConfigSourceComparison x y)) (cast a)
122- in compareValues a b'
115+ in compareSources a b'
123116 else
124117 throw (InvalidConfigSourceComparison x y)
125118 | fromValue (sourceValue a) == JSON. Null = LT
@@ -128,10 +121,10 @@ instance IConfigSource SomeConfigSource where
128121 compare ia ib
129122
130123instance Eq SomeConfigSource where
131- (==) a b = compareValues a b == EQ
124+ (==) a b = compareSources a b == EQ
132125
133126instance Ord SomeConfigSource where
134- compare = compareValues
127+ compare = compareSources
135128
136129data FileSource = FileSource
137130 { fsConfigIndex :: ! Int
@@ -141,15 +134,13 @@ data FileSource = FileSource
141134
142135instance NFData FileSource
143136instance IConfigSource FileSource where
137+ compareSources = comparing fsConfigIndex
144138 sourceValue = fsValue
145- compareValues = comparing fsConfigIndex
146- sourcePretty f (FileSource _index origin value) =
147- let sourceDoc = case origin of
148- ConfigFileOrigin filepath -> Doc. text " File:" <+> Doc. text (Text. unpack filepath)
149- EnvFileOrigin envVar filepath ->
150- Doc. text " File:" <+> Doc. text (Text. unpack envVar) <> " =" <> Doc. text (Text. unpack filepath)
151- valueDoc = renderConfigValue f value
152- in (valueDoc, sourceDoc)
139+ sourcePrettyDoc (FileSource _index origin _value) =
140+ case origin of
141+ ConfigFileOrigin filepath -> Doc. text " File:" <+> Doc. text (Text. unpack filepath)
142+ EnvFileOrigin envVar filepath ->
143+ Doc. text " File:" <+> Doc. text (Text. unpack envVar) <> " =" <> Doc. text (Text. unpack filepath)
153144
154145fileSource :: Int -> Int -> FileValueOrigin -> Value JSON. Value -> SomeConfigSource
155146fileSource precedenceOrder index origin val =
@@ -165,10 +156,8 @@ data EnvSource = EnvSource
165156instance NFData EnvSource
166157instance IConfigSource EnvSource where
167158 sourceValue = esValue
168- sourcePretty f (EnvSource varname value) =
169- let sourceDoc = Doc. text " Env:" <+> Doc. text (Text. unpack varname)
170- valueDoc = renderConfigValue f value
171- in (valueDoc, sourceDoc)
159+ sourcePrettyDoc (EnvSource varname _value) =
160+ Doc. text " Env:" <+> Doc. text (Text. unpack varname)
172161
173162envSource :: Int -> Text -> Value JSON. Value -> SomeConfigSource
174163envSource precedenceOrder varName val =
@@ -179,11 +168,8 @@ newtype DefaultSource =
179168 deriving (Generic , Typeable , Show , Eq , NFData )
180169
181170instance IConfigSource DefaultSource where
182- sourceValue (DefaultSource val) = val
183- sourcePretty f (DefaultSource value) =
184- let sourceDoc = Doc. text " Default"
185- valueDoc = renderConfigValue f value
186- in (valueDoc, sourceDoc)
171+ sourceValue (DefaultSource value) = value
172+ sourcePrettyDoc (DefaultSource _value) = Doc. text " Default"
187173
188174defaultSource :: Value JSON. Value -> SomeConfigSource
189175defaultSource = SomeConfigSource 0 . DefaultSource
@@ -197,10 +183,7 @@ newtype CliSource
197183
198184instance IConfigSource CliSource where
199185 sourceValue (CliSource value) = value
200- sourcePretty f (CliSource value) =
201- let sourceDoc = Doc. text " Cli"
202- valueDoc = renderConfigValue f value
203- in (valueDoc, sourceDoc)
186+ sourcePrettyDoc (CliSource _value) = Doc. text " Cli"
204187
205188cliSource :: Int -> Value JSON. Value -> SomeConfigSource
206189cliSource precedenceOrder val = SomeConfigSource precedenceOrder $ CliSource val
0 commit comments