• 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

95.45
/core/src/main/scala/pureconfig/ConfigConvert.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
package pureconfig
8

9
import scala.reflect.ClassTag
10
import scala.util.Try
11

12
import pureconfig.error.FailureReason
13

14
/** Trait for objects capable of reading and writing objects of a given type from and to `ConfigValues`.
15
  */
16
trait ConfigConvert[A] extends ConfigReader[A] with ConfigWriter[A] { outer =>
17

18
  /** Transforms the values read and written by this `ConfigConvert` using two functions.
19
    *
20
    * @param f
21
    *   the function applied to values after they are read; a thrown exception will be caught and converted to a
22
    *   pureconfig FailureReason
23
    * @param g
24
    *   the function applied to values before they are written
25
    * @tparam B
26
    *   the type of the returned `ConfigConvert`
27
    * @return
28
    *   a `ConfigConvert` that reads and writes values of type `B` by applying `f` and `g` on read and write,
29
    *   respectively.
30
    */
31
  def xmap[B](f: A => B, g: B => A): ConfigConvert[B] =
1✔
32
    ConfigConvert(map(f), contramap(g))
7✔
33

34
  /** Transforms the values read and written by this `ConfigConvert` using two functions where the reader may specify
35
    * custom failure reason.
36
    *
37
    * @param f
38
    *   the function applied to values after they are read
39
    * @param g
40
    *   the function applied to values before they are written
41
    * @tparam B
42
    *   the type of the returned `ConfigConvert`
43
    * @return
44
    *   a `ConfigConvert` that reads and writes values of type `B` by applying `f` and `g` on read and write,
45
    *   respectively.
46
    */
47
  def xemap[B](f: A => Either[FailureReason, B], g: B => A): ConfigConvert[B] =
1✔
48
    ConfigConvert(emap(f), contramap(g))
2✔
49
}
50

51
/** Provides methods to create [[ConfigConvert]] instances.
52
  */
53
object ConfigConvert extends ConvertHelpers {
54

55
  def apply[A](implicit conv: ConfigConvert[A]): ConfigConvert[A] = conv
1✔
56

57
  def apply[A](reader: ConfigReader[A], writer: ConfigWriter[A]): ConfigConvert[A] =
1✔
58
    new ConfigConvert[A] {
16✔
59
      def from(cur: ConfigCursor) = reader.from(cur)
13✔
60
      def to(a: A) = writer.to(a)
8✔
61
    }
62

63
  implicit def fromReaderAndWriter[A](implicit
1✔
64
      reader: ConfigReader[A],
65
      writer: ConfigWriter[A]
66
  ): ConfigConvert[A] =
67
    ConfigConvert(reader, writer)
11✔
68

69
  def viaString[A](fromF: String => Either[FailureReason, A], toF: A => String): ConfigConvert[A] =
1✔
70
    ConfigConvert(ConfigReader.fromString(fromF), ConfigWriter.toString(toF))
14✔
71

UNCOV
72
  def viaStringTry[A: ClassTag](fromF: String => Try[A], toF: A => String): ConfigConvert[A] = {
73
    viaString[A](tryF(fromF), toF)
×
74
  }
75

76
  def viaStringOpt[A: ClassTag](fromF: String => Option[A], toF: A => String): ConfigConvert[A] = {
1✔
77
    viaString[A](optF(fromF), toF)
2✔
78
  }
79

80
  def viaNonEmptyString[A](fromF: String => Either[FailureReason, A], toF: A => String)(implicit
1✔
81
      ct: ClassTag[A]
82
  ): ConfigConvert[A] = {
83
    viaString[A](string => ensureNonEmpty(ct)(string).flatMap(s => fromF(s)), toF)
6✔
84
  }
85

86
  def viaNonEmptyStringTry[A: ClassTag](fromF: String => Try[A], toF: A => String): ConfigConvert[A] = {
1✔
87
    viaNonEmptyString[A](tryF(fromF), toF)
4✔
88
  }
89

90
  def viaNonEmptyStringOpt[A: ClassTag](fromF: String => Option[A], toF: A => String): ConfigConvert[A] = {
1✔
91
    viaNonEmptyString[A](optF(fromF), toF)
2✔
92
  }
93
}
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