on master help sign in
Some doc improvements
68a8443Michael Snoyman
ChangeLog.md+4
@@ -1,3 +1,7 @@
1+## 0.8.16.1
2+
3+* Slight doc improvement
4+
1## 0.8.16
5## 0.8.16
2
6
3Add env variable parsing. `loadYamlSettings` can read config values from the environment with Yaml that specifies an env var.
7Add env variable parsing. `loadYamlSettings` can read config values from the environment with Yaml that specifies an env var.
Data/Yaml/Config.hs+60−6
@@ -9,16 +9,19 @@
9--
9--
10-- On a historical note, this code was taken directly from the yesod web framework's configuration module.
10-- On a historical note, this code was taken directly from the yesod web framework's configuration module.
11module Data.Yaml.Config
11module Data.Yaml.Config
12− ( applyCurrentEnv
12+ ( -- * High-level
13− , getCurrentEnv
14− , applyEnvValue
15− , loadYamlSettings
13+ loadYamlSettings
14+ -- ** EnvUsage
16 , EnvUsage
15 , EnvUsage
17 , ignoreEnv
16 , ignoreEnv
18 , useEnv
17 , useEnv
19 , requireEnv
18 , requireEnv
20 , useCustomEnv
19 , useCustomEnv
21 , requireCustomEnv
20 , requireCustomEnv
21+ -- * Lower level
22+ , applyCurrentEnv
23+ , getCurrentEnv
24+ , applyEnvValue
22 ) where
25 ) where
23
26
24
27
@@ -50,6 +53,14 @@ mergeValues :: Value -> Value -> Value
50mergeValues (Object x) (Object y) = Object $ H.unionWith mergeValues x y
53mergeValues (Object x) (Object y) = Object $ H.unionWith mergeValues x y
51mergeValues x _ = x
54mergeValues x _ = x
52
55
56+-- | Override environment variable placeholders in the given @Value@ with
57+-- values from the environment.
58+--
59+-- If the first argument is @True@, then all placeholders _must_ be provided by
60+-- the actual environment. Otherwise, default values from the @Value@ will be
61+-- used.
62+--
63+-- @since 0.8.16
53applyEnvValue :: Bool -- ^ require an environment variable to be present?
64applyEnvValue :: Bool -- ^ require an environment variable to be present?
54 -> H.HashMap Text Text -> Value -> Value
65 -> H.HashMap Text Text -> Value -> Value
55applyEnvValue requireEnv' env =
66applyEnvValue requireEnv' env =
@@ -82,26 +93,62 @@ applyEnvValue requireEnv' env =
82
93
83 parseValue val = fromMaybe (String val) $ Y.decode $ encodeUtf8 val
94 parseValue val = fromMaybe (String val) $ Y.decode $ encodeUtf8 val
84
95
96+-- | Get the actual environment as a @HashMap@ from @Text@ to @Text@.
97+--
98+-- @since 0.8.16
85getCurrentEnv :: IO (H.HashMap Text Text)
99getCurrentEnv :: IO (H.HashMap Text Text)
86getCurrentEnv = fmap (H.fromList . map (pack *** pack)) getEnvironment
100getCurrentEnv = fmap (H.fromList . map (pack *** pack)) getEnvironment
87
101
102+-- | A convenience wrapper around 'applyEnvValue' and 'getCurrentEnv'
103+--
104+-- @since 0.8.16
88applyCurrentEnv :: Bool -- ^ require an environment variable to be present?
105applyCurrentEnv :: Bool -- ^ require an environment variable to be present?
89 -> Value -> IO Value
106 -> Value -> IO Value
90applyCurrentEnv requireEnv' orig = flip (applyEnvValue requireEnv') orig <$> getCurrentEnv
107applyCurrentEnv requireEnv' orig = flip (applyEnvValue requireEnv') orig <$> getCurrentEnv
91
108
109+-- | Defines how we want to use the environment variables when loading a config
110+-- file. Use the smart constructors provided by this module.
111+--
112+-- @since 0.8.16
92data EnvUsage = IgnoreEnv
113data EnvUsage = IgnoreEnv
93 | UseEnv
114 | UseEnv
94 | RequireEnv
115 | RequireEnv
95 | UseCustomEnv (H.HashMap Text Text)
116 | UseCustomEnv (H.HashMap Text Text)
96 | RequireCustomEnv (H.HashMap Text Text)
117 | RequireCustomEnv (H.HashMap Text Text)
97
118
119+-- | Do not use any environment variables, instead relying on defaults values
120+-- in the config file.
121+--
122+-- @since 0.8.16
98−ignoreEnv, useEnv, requireEnv :: EnvUsage
123+ignoreEnv :: EnvUsage
99ignoreEnv = IgnoreEnv
124ignoreEnv = IgnoreEnv
125+
126+-- | Use environment variables when available, otherwise use defaults.
127+--
128+-- @since 0.8.16
129+useEnv :: EnvUsage
100useEnv = UseEnv
130useEnv = UseEnv
131+
132+-- | Do not use default values from the config file, but instead take all
133+-- overrides from the environment. If a value is missing, loading the file will
134+-- throw an exception.
135+--
136+-- @since 0.8.16
137+requireEnv :: EnvUsage
101requireEnv = RequireEnv
138requireEnv = RequireEnv
102
139
140+-- | Same as 'useEnv', but instead of the actual environment, use the provided
141+-- @HashMap@ as the environment.
142+--
143+-- @since 0.8.16
103−useCustomEnv, requireCustomEnv :: H.HashMap Text Text -> EnvUsage
144+useCustomEnv :: H.HashMap Text Text -> EnvUsage
104useCustomEnv = UseCustomEnv
145useCustomEnv = UseCustomEnv
146+
147+-- | Same as 'requireEnv', but instead of the actual environment, use the
148+-- provided @HashMap@ as the environment.
149+--
150+-- @since 0.8.16
151+requireCustomEnv :: H.HashMap Text Text -> EnvUsage
105requireCustomEnv = RequireCustomEnv
152requireCustomEnv = RequireCustomEnv
106
153
107-- | Load the settings from the following three sources:
154-- | Load the settings from the following three sources:
@@ -111,6 +158,13 @@ requireCustomEnv = RequireCustomEnv
111-- * Run time environment variables
158-- * Run time environment variables
112--
159--
113-- * The default compile time config file
160-- * The default compile time config file
161+--
162+-- For example, to load up settings from @config/foo.yaml@ and allow overriding
163+-- from the actual environment, you can use:
164+--
165+-- > loadYamlSettings ["config/foo.yaml"] [] useEnv
166+--
167+-- @since 0.8.16
114loadYamlSettings
168loadYamlSettings
115 :: FromJSON settings
169 :: FromJSON settings
116 => [FilePath] -- ^ run time config files to use, earlier files have precedence
170 => [FilePath] -- ^ run time config files to use, earlier files have precedence
yaml.cabal+1−1
@@ -1,5 +1,5 @@
1name: yaml
1name: yaml
2−version: 0.8.16
2+version: 0.8.16.1
3license: BSD3
3license: BSD3
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov