• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pureconfig / pureconfig / 16844894943

09 Aug 2025 03:28AM UTC coverage: 94.686% (-0.04%) from 94.726%
16844894943

Pull #1838

web-flow
Merge 12f0062cf into 38f73c635
Pull Request #1838: Rid of deprecated URL constructor

1 of 1 new or added line in 1 file covered. (100.0%)

95 existing lines in 26 files now uncovered.

2744 of 2898 relevant lines covered (94.69%)

2.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.0
/core/src/main/scala/pureconfig/package.scala
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
/** @author
5
  *   Mario Pastorelli
6
  */
7

8
import java.io.{OutputStream, OutputStreamWriter}
9
import java.nio.charset.StandardCharsets.UTF_8
10
import java.nio.file.{Files, Path}
11

12
import scala.reflect.ClassTag
13

14
import com.typesafe.config.{Config, ConfigRenderOptions}
15

16
import pureconfig.error._
17

18
package object pureconfig {
19

20
  /** Load a configuration of type `A` from the standard configuration files
21
    *
22
    * @return
23
    *   A `Success` with the configuration if it is possible to create an instance of type `A` from the configuration
24
    *   files, else a `Failure` with details on why it isn't possible
25
    */
26
  @deprecated("Use `ConfigSource.default.load[A]` instead", "0.12.0")
27
  def loadConfig[A](implicit reader: ConfigReader[A]): ConfigReader.Result[A] =
1✔
28
    ConfigSource.default.load[A]
2✔
29

30
  /** Load a configuration of type `A` from the standard configuration files
31
    *
32
    * @param namespace
33
    *   the base namespace from which the configuration should be load
34
    * @return
35
    *   A `Success` with the configuration if it is possible to create an instance of type `A` from the configuration
36
    *   files, else a `Failure` with details on why it isn't possible
37
    */
38
  @deprecated("Use `ConfigSource.default.at(namespace).load[A]` instead", "0.12.0")
39
  def loadConfig[A](namespace: String)(implicit reader: ConfigReader[A]): ConfigReader.Result[A] =
1✔
40
    ConfigSource.default.at(namespace).load[A]
2✔
41

42
  /** Load a configuration of type `A` from the given file. Note that standard configuration files are still loaded but
43
    * can be overridden from the given configuration file
44
    *
45
    * @return
46
    *   A `Success` with the configuration if it is possible to create an instance of type `A` from the configuration
47
    *   files, else a `Failure` with details on why it isn't possible
48
    */
49
  @deprecated("Use `ConfigSource.default(ConfigSource.file(path)).load[A]` instead", "0.12.0")
50
  def loadConfig[A](path: Path)(implicit reader: ConfigReader[A]): ConfigReader.Result[A] =
1✔
51
    ConfigSource.default(ConfigSource.file(path)).load[A]
2✔
52

53
  /** Load a configuration of type `A` from the given file. Note that standard configuration files are still loaded but
54
    * can be overridden from the given configuration file
55
    *
56
    * @param namespace
57
    *   the base namespace from which the configuration should be load
58
    * @return
59
    *   A `Success` with the configuration if it is possible to create an instance of type `A` from the configuration
60
    *   files, else a `Failure` with details on why it isn't possible
61
    */
62
  @deprecated("Use `ConfigSource.default(ConfigSource.file(path)).at(namespace).load[A]` instead", "0.12.0")
63
  def loadConfig[A](path: Path, namespace: String)(implicit reader: ConfigReader[A]): ConfigReader.Result[A] =
1✔
64
    ConfigSource.default(ConfigSource.file(path)).at(namespace).load[A]
2✔
65

66
  /** Load a configuration of type `A` from the given `Config` */
67
  @deprecated("Use `ConfigSource.fromConfig(conf).load[A]` instead", "0.12.0")
68
  def loadConfig[A](conf: Config)(implicit reader: ConfigReader[A]): ConfigReader.Result[A] =
1✔
69
    ConfigSource.fromConfig(conf).load[A]
2✔
70

71
  /** Load a configuration of type `A` from the given `Config` */
72
  @deprecated("Use `ConfigSource.fromConfig(conf).at(namespace).load[A]` instead", "0.12.0")
73
  def loadConfig[A](conf: Config, namespace: String)(implicit reader: ConfigReader[A]): ConfigReader.Result[A] =
1✔
74
    ConfigSource.fromConfig(conf).at(namespace).load[A]
2✔
75

76
  /** Load a configuration of type `A` from the given `Config`, falling back to the default configuration
77
    *
78
    * @param conf
79
    *   Typesafe configuration to load
80
    * @return
81
    *   A `Success` with the configuration if it is possible to create an instance of type `A` from the configuration
82
    *   files, else a `Failure` with details on why it isn't possible
83
    */
84
  @deprecated("Use `ConfigSource.fromConfig(conf).withFallback(ConfigSource.default).load[A]` instead", "0.12.0")
85
  def loadConfigWithFallback[A](conf: Config)(implicit reader: ConfigReader[A]): ConfigReader.Result[A] =
1✔
86
    ConfigSource.fromConfig(conf).withFallback(ConfigSource.default).load[A]
2✔
87

88
  /** Load a configuration of type `A` from the given `Config`, falling back to the default configuration
89
    *
90
    * @param conf
91
    *   Typesafe configuration to load
92
    * @param namespace
93
    *   the base namespace from which the configuration should be load
94
    * @return
95
    *   A `Success` with the configuration if it is possible to create an instance of type `A` from the configuration
96
    *   files, else a `Failure` with details on why it isn't possible
97
    */
98
  @deprecated(
99
    "Use `ConfigSource.fromConfig(conf).withFallback(ConfigSource.default).at(namespace).load[A]` instead",
100
    "0.12.0"
101
  )
UNCOV
102
  def loadConfigWithFallback[A](conf: Config, namespace: String)(implicit
103
      reader: ConfigReader[A]
104
  ): ConfigReader.Result[A] =
105
    ConfigSource.fromConfig(conf).withFallback(ConfigSource.default).at(namespace).load[A]
×
106

107
  /** Load a configuration of type `A` from the standard configuration files
108
    *
109
    * @return
110
    *   the configuration
111
    */
112
  @throws[ConfigReaderException[_]]
113
  @deprecated("Use `ConfigSource.default.loadOrThrow[A]` instead", "0.12.0")
114
  def loadConfigOrThrow[A: ClassTag](implicit reader: ConfigReader[A]): A = ConfigSource.default.loadOrThrow[A]
×
115

116
  /** Load a configuration of type `A` from the standard configuration files
117
    *
118
    * @param namespace
119
    *   the base namespace from which the configuration should be load
120
    * @return
121
    *   the configuration
122
    */
123
  @throws[ConfigReaderException[_]]
124
  @deprecated("Use `ConfigSource.default.at(namespace).loadOrThrow[A]` instead", "0.12.0")
UNCOV
125
  def loadConfigOrThrow[A: ClassTag](namespace: String)(implicit reader: ConfigReader[A]): A =
126
    ConfigSource.default.at(namespace).loadOrThrow[A]
×
127

128
  /** Load a configuration of type `A` from the given file. Note that standard configuration files are still loaded but
129
    * can be overridden from the given configuration file
130
    *
131
    * @return
132
    *   the configuration
133
    */
134
  @throws[ConfigReaderException[_]]
135
  @deprecated("Use `ConfigSource.default(ConfigSource.file(path)).loadOrThrow[A]` instead", "0.12.0")
UNCOV
136
  def loadConfigOrThrow[A: ClassTag](path: Path)(implicit reader: ConfigReader[A]): A =
137
    ConfigSource.default(ConfigSource.file(path)).loadOrThrow[A]
×
138

139
  /** Load a configuration of type `A` from the given file. Note that standard configuration files are still loaded but
140
    * can be overridden from the given configuration file
141
    *
142
    * @param namespace
143
    *   the base namespace from which the configuration should be load
144
    * @return
145
    *   the configuration
146
    */
147
  @throws[ConfigReaderException[_]]
148
  @deprecated("Use `ConfigSource.default(ConfigSource.file(path)).at(namespace).loadOrThrow[A]` instead", "0.12.0")
UNCOV
149
  def loadConfigOrThrow[A: ClassTag](path: Path, namespace: String)(implicit reader: ConfigReader[A]): A =
150
    ConfigSource.default(ConfigSource.file(path)).at(namespace).loadOrThrow[A]
×
151

152
  /** Load a configuration of type `A` from the given `Config`
153
    *
154
    * @param conf
155
    *   Typesafe configuration to load
156
    * @return
157
    *   the configuration
158
    */
159
  @throws[ConfigReaderException[_]]
160
  @deprecated("Use `ConfigSource.fromConfig(conf).loadOrThrow[A]` instead", "0.12.0")
UNCOV
161
  def loadConfigOrThrow[A: ClassTag](conf: Config)(implicit reader: ConfigReader[A]): A =
162
    ConfigSource.fromConfig(conf).loadOrThrow[A]
×
163

164
  /** Load a configuration of type `A` from the given `Config`
165
    *
166
    * @param conf
167
    *   Typesafe configuration to load
168
    * @param namespace
169
    *   the base namespace from which the configuration should be load
170
    * @return
171
    *   the configuration
172
    */
173
  @throws[ConfigReaderException[_]]
174
  @deprecated("Use `ConfigSource.fromConfig(conf).at(namespace).loadOrThrow[A]` instead", "0.12.0")
UNCOV
175
  def loadConfigOrThrow[A: ClassTag](conf: Config, namespace: String)(implicit reader: ConfigReader[A]): A =
176
    ConfigSource.fromConfig(conf).at(namespace).loadOrThrow[A]
×
177

178
  /** Load a configuration of type `A` from the given `Config`, falling back to the default configuration
179
    *
180
    * @param conf
181
    *   Typesafe configuration to load
182
    * @return
183
    *   the configuration
184
    */
185
  @throws[ConfigReaderException[_]]
186
  @deprecated("Use `ConfigSource.fromConfig(conf).withFallback(ConfigSource.default).loadOrThrow[A]` instead", "0.12.0")
UNCOV
187
  def loadConfigWithFallbackOrThrow[A: ClassTag](conf: Config)(implicit reader: ConfigReader[A]): A =
188
    ConfigSource.fromConfig(conf).withFallback(ConfigSource.default).loadOrThrow[A]
×
189

190
  /** Load a configuration of type `A` from the given `Config`, falling back to the default configuration
191
    *
192
    * @param conf
193
    *   Typesafe configuration to load
194
    * @param namespace
195
    *   the base namespace from which the configuration should be load
196
    * @return
197
    *   the configuration
198
    */
199
  @throws[ConfigReaderException[_]]
200
  @deprecated(
201
    "Use `ConfigSource.fromConfig(conf).withFallback(ConfigSource.default).at(namespace).loadOrThrow[A]` instead",
202
    "0.12.0"
203
  )
UNCOV
204
  def loadConfigWithFallbackOrThrow[A: ClassTag](conf: Config, namespace: String)(implicit reader: ConfigReader[A]): A =
205
    ConfigSource.fromConfig(conf).withFallback(ConfigSource.default).at(namespace).loadOrThrow[A]
×
206

207
  /** Save the given configuration into a property file
208
    *
209
    * @param conf
210
    *   The configuration to save
211
    * @param outputPath
212
    *   Where to write the configuration
213
    * @param overrideOutputPath
214
    *   Override the path if it already exists
215
    * @param options
216
    *   the config rendering options
217
    */
218
  @throws[IllegalArgumentException]
UNCOV
219
  def saveConfigAsPropertyFile[A](
220
      conf: A,
221
      outputPath: Path,
222
      overrideOutputPath: Boolean = false,
UNCOV
223
      options: ConfigRenderOptions = ConfigRenderOptions.defaults()
224
  )(implicit writer: ConfigWriter[A]): Unit = {
225

226
    if (!overrideOutputPath && Files.isRegularFile(outputPath)) {
×
227
      throw new IllegalArgumentException(s"Cannot save configuration in file '$outputPath' because it already exists")
×
UNCOV
228
    }
229
    if (Files isDirectory outputPath) {
×
230
      throw new IllegalArgumentException(
×
UNCOV
231
        s"Cannot save configuration in file '$outputPath' because it already exists and is a directory"
232
      )
UNCOV
233
    }
234

235
    saveConfigToStream(conf, Files.newOutputStream(outputPath), options)
×
236
  }
237

238
  /** Writes the configuration to the output stream and closes the stream
239
    *
240
    * @param conf
241
    *   The configuration to write
242
    * @param outputStream
243
    *   The stream in which the configuration should be written
244
    * @param options
245
    *   the config rendering options
246
    */
UNCOV
247
  def saveConfigToStream[A](
248
      conf: A,
249
      outputStream: OutputStream,
UNCOV
250
      options: ConfigRenderOptions = ConfigRenderOptions.defaults()
251
  )(implicit writer: ConfigWriter[A]): Unit = {
252

253
    // HOCON requires UTF-8:
254
    // https://github.com/lightbend/config/blob/master/HOCON.md#unchanged-from-json
255
    val printOutputStream = new OutputStreamWriter(outputStream, UTF_8)
×
256
    val rawConf = writer.to(conf)
×
257
    printOutputStream.write(rawConf.render(options))
×
258
    printOutputStream.close()
×
259
  }
260

261
  /** Loads `files` in order, allowing values in later files to backstop missing values from prior, and converts them
262
    * into a `Config`.
263
    *
264
    * This is a convenience method which enables having default configuration which backstops local configuration.
265
    *
266
    * The behavior of the method if an element of `files` references a file which doesn't exist or can't be read is
267
    * defined by the `failOnReadError` flag. With `failOnReadError = false`, such files will silently be ignored while
268
    * otherwise they would yield a failure (a `Left` value).
269
    *
270
    * @param files
271
    *   Files ordered in decreasing priority containing part or all of a `Config`
272
    * @param failOnReadError
273
    *   Where to return an error if any files fail to read
274
    * @param namespace
275
    *   the base namespace from which the configuration should be load
276
    */
277
  @deprecated("Construct a custom `ConfigSource` pipeline instead", "0.12.0")
278
  def loadConfigFromFiles[A](files: Traversable[Path], failOnReadError: Boolean = false, namespace: String = "")(
1✔
279
      implicit reader: ConfigReader[A]
280
  ): ConfigReader.Result[A] = {
281
    ConfigSource
1✔
282
      .default(
1✔
283
        files
284
          .map(ConfigSource.file)
1✔
285
          .map(cs => if (failOnReadError) cs else cs.optional)
1✔
286
          .foldLeft(ConfigSource.empty)(_.withFallback(_))
1✔
287
      )
288
      .at(namespace)
1✔
289
      .load[A]
1✔
290
  }
291

292
  /** @see
293
    *   [[loadConfigFromFiles]]
294
    * @return
295
    *   the configuration
296
    */
297
  @throws[ConfigReaderException[_]]
298
  @deprecated("Construct a custom `ConfigSource` pipeline instead", "0.12.0")
UNCOV
299
  def loadConfigFromFilesOrThrow[A: ClassTag](
300
      files: Traversable[Path]
301
  )(implicit reader: ConfigReader[A]): A = {
UNCOV
302
    ConfigSource
UNCOV
303
      .default(
304
        files
UNCOV
305
          .map(ConfigSource.file(_).optional)
UNCOV
306
          .foldLeft(ConfigSource.empty)(_.withFallback(_))
307
      )
308
      .loadOrThrow[A]
309
  }
310
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc