Skip to content

Commit 4296d5f

Browse files
committed
[54] Improve upon pretty printer for sources
1 parent 64396b1 commit 4296d5f

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

etc/src/System/Etc/Internal/Extra/Printer.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,16 @@ renderConfigValueJSON value = case value of
8989
)
9090
(HashMap.toList obj)
9191

92+
renderConfigValue :: (JSON.Value -> Doc) -> Value JSON.Value -> [Doc]
93+
renderConfigValue f value = case value of
94+
Plain (JSON.Array jsonArray) ->
95+
Vector.toList $ Vector.map (\jsonValue -> text "-" <+> f jsonValue) jsonArray
96+
Plain jsonValue -> return $ f jsonValue
97+
Sensitive{} -> return $ text "<<sensitive>>"
98+
9299
renderConfigSource :: (JSON.Value -> Doc) -> SomeConfigSource -> ([Doc], Doc)
93-
renderConfigSource = sourcePretty
100+
renderConfigSource f source =
101+
(renderConfigValue f (sourceValue source), sourcePrettyDoc source)
94102

95103
renderConfig_ :: MonadThrow m => ColorFn -> Config -> m Doc
96104
renderConfig_ ColorFn { blueColor } (Config configMap) =

etc/src/System/Etc/Internal/Types.hs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ((<>))
1414
import qualified RIO.HashMap as HashMap
1515
import qualified RIO.Set as Set
1616
import qualified RIO.Text as Text
17-
import qualified RIO.Vector as Vector
1817

1918
import Text.PrettyPrint.ANSI.Leijen (Doc, (<+>))
2019
import qualified Text.PrettyPrint.ANSI.Leijen as Doc
2120

21+
22+
2223
import Control.Exception (throw)
2324

2425
import Data.Bool (bool)
26+
import Data.Monoid ((<>))
2527
import qualified Data.Semigroup as Semigroup
2628
import Data.Typeable (cast, typeOf)
2729

@@ -73,10 +75,10 @@ data FileValueOrigin
7375
instance NFData FileValueOrigin
7476

7577
class 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

8183
data SomeConfigSource =
8284
forall source. ( Show source
@@ -101,25 +103,16 @@ data InvalidConfigSourceComparison
101103

102104
instance 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-
111106
instance 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

130123
instance Eq SomeConfigSource where
131-
(==) a b = compareValues a b == EQ
124+
(==) a b = compareSources a b == EQ
132125

133126
instance Ord SomeConfigSource where
134-
compare = compareValues
127+
compare = compareSources
135128

136129
data FileSource = FileSource
137130
{ fsConfigIndex :: !Int
@@ -141,15 +134,13 @@ data FileSource = FileSource
141134

142135
instance NFData FileSource
143136
instance 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

154145
fileSource :: Int -> Int -> FileValueOrigin -> Value JSON.Value -> SomeConfigSource
155146
fileSource precedenceOrder index origin val =
@@ -165,10 +156,8 @@ data EnvSource = EnvSource
165156
instance NFData EnvSource
166157
instance 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

173162
envSource :: Int -> Text -> Value JSON.Value -> SomeConfigSource
174163
envSource precedenceOrder varName val =
@@ -179,11 +168,8 @@ newtype DefaultSource =
179168
deriving (Generic, Typeable, Show, Eq, NFData)
180169

181170
instance 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

188174
defaultSource :: Value JSON.Value -> SomeConfigSource
189175
defaultSource = SomeConfigSource 0 . DefaultSource
@@ -197,10 +183,7 @@ newtype CliSource
197183

198184
instance 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

205188
cliSource :: Int -> Value JSON.Value -> SomeConfigSource
206189
cliSource precedenceOrder val = SomeConfigSource precedenceOrder $ CliSource val

0 commit comments

Comments
 (0)