trait ConfigDescriptorFunctions extends AnyRef
- Alphabetic
- By Inheritance
- ConfigDescriptorFunctions
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
- class PartiallyAppliedEnumeration[D] extends AnyRef
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
collectAll[A](head: ⇒ ConfigDescriptor[A], tail: ConfigDescriptor[A]*): ConfigDescriptor[List[A]]
collectAllis an alias tosequence.collectAllis an alias tosequence. In Functional Programming terms, it is a Traverse implementation for ConfigDescriptor. In other words, it allows us to convert aListofConfigDescriptor[A]toConfigDescriptor[List[A]].Example:
final case class Variables(variable1: Int, variable2: Option[Int]) object CollectAllExample extends App with EitherImpureOps { val listOfConfig: List[ConfigDescriptor[Variables]] = List("GROUP1", "GROUP2", "GROUP3", "GROUP4") .map( group => (int(s"${group}_VARIABLE1") zip int(s"${group}_VARIABLE2").optional).to[Variables] ) val configOfList: ConfigDescriptor[List[Variables]] = collectAll(listOfConfig.head, listOfConfig.tail: _*) val map = Map( "GROUP1_VARIABLE1" -> "1", "GROUP1_VARIABLE2" -> "2", "GROUP2_VARIABLE1" -> "3", "GROUP2_VARIABLE2" -> "4", "GROUP3_VARIABLE1" -> "5", "GROUP3_VARIABLE2" -> "6", "GROUP4_VARIABLE1" -> "7" ) // loadOrThrow here is only for the purpose of example val result: List[Variables] = read(configOfList from ConfigSource.fromMap(map, "constant")).loadOrThrow val written: PropertyTree[String, String] = write(configOfList, result).loadOrThrow assert( result == List(Variables(1, Some(2)), Variables(3, Some(4)), Variables(5, Some(6)), Variables(7, None)) )
-
def
enumeration[D]: PartiallyAppliedEnumeration[D]
enumeration allows user to up-cast all the subtypes to its super type defined by
D.enumeration allows user to up-cast all the subtypes to its super type defined by
D. This is mainly useful in definingcoproducts(sealed trait)Example:
sealed trait D case class A(a: String) extends D case class B(b: Int) extends D case class C(c: Double) extends D val config: ConfigDescriptor[D] = enumeration[D]( string("a").to[A], int("b").to[B], double("c").to[C] )
Currently enumeration supports to a maximum of 9 terms. If you have more terms, use
orElseto combine the terms.enumeration[D](a, b, c, d, e, f, g, h) orElse enumeration[D](i, j, k)
NOTE:
Use zio-config-magnolia for better compile time safety when it comes to
sealed trait, as it has strong compile time behaviour and makes sure all subtypes are being handled. On the other hand,enumerationdoesn't complain at compile time if you forgot to pass the config descriptor of any of the subtype.Example:
import zio.config.magnolia._ val config = descriptor[D]
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
head[A](path: ConfigDescriptorModule.K)(desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[A]
headdescribes getting the head of a possible list valueheaddescribes getting the head of a possible list valueExample:
final case class Config(userName: String, port: Option[Int]) object Config { val source = ConfigSource.fromMap(Map("USERNAME" -> "af,sa", "PORT" -> "1"), valueDelimiter = Some(',')) val databaseConfig: ConfigDescriptor[Config] = (head("USERNAME")(string) zip int("PORT").optional).to[Config] } read(Config.databaseConfig from Config.source) // returns Config("af", 1)
-
def
head[A](desc: ConfigDescriptor[A]): ConfigDescriptor[A]
headdescribes getting the head of a possible list valueheaddescribes getting the head of a possible list valueExample:
final case class Config(userName: String, port: Option[Int]) object Config { val source = ConfigSource.fromMap(Map("USERNAME" -> "af,sa", "PORT" -> "1"), valueDelimiter = Some(',')) val databaseConfig: ConfigDescriptor[Config] = (head(string("USERNAME")) zip int("PORT").optional).to[Config] } read(Config.databaseConfig from Config.source) // returns Config("af", 1)
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
list[A](path: ConfigDescriptorModule.K)(desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[List[A]]
list("xyz")(confgDescriptor)represents just a list variant of configDescriptor within the keyxyz.list("xyz")(confgDescriptor)represents just a list variant of configDescriptor within the keyxyz. Note that,nested("xyz")(list(configDescriptor))is same aslist("xyz")(configDescriptor).For example:
list("key")(string)implies value ofkeyis of the typeList[String]Here is a more detailed example.
We know
val config = string("USERNAME") from sourcerepresents a program that says, there exists a key called "USERNAME" (in some ConfigSource called source) with a value that is of the typeString.list("xyz")(config)would then imply, there exists a list ofUSERNAME -> valuepair within the key "xyz".val json = s""" | xyz : [ | { | "USERNAME" : "value1" | }, | | { | "USERNAME" : "value2" | } | ] |""".stripMargin val config = string("USERNAME") // Within the key "xyz", we have a list of key-value pair, where key is always "USERNAME" // NOTE: In HOCON, there is always a need of key (in this case, xyz) at parent level. val listConfig = list("xyz")(config) val userNames: ZIO[Any, ReadError[String], List[String]] = read(listConfig from ypesafeConfigSource.fromHoconString(json))
returns
List(value1, value2) -
def
list[A](desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[List[A]]
list(confgDescriptor)represents just a list variant of configuration extraction.list(confgDescriptor)represents just a list variant of configuration extraction.For example, we know
val config = string("USERNAME") from sourcerepresents a program that says, there exists a key called "USERNAME" (in some ConfigSource called source) with a value that is of the typeString.list(config)would then imply, there exists a list ofUSERNAME -> valuepair.Given below is a complete example:
val json = s""" | xyz : [ | { | "USERNAME" : "value1" | }, | | { | "USERNAME" : "value2" | } | ] |""".stripMargin val config = string("USERNAME") // Within the key "xyz", we have a list of key-value pair, where key is always "USERNAME" // NOTE: In HOCON, there is always a need of key (in this case, xyz) at parent level. val listConfig = nested("xyz")(list(config)) val userNames: ZIO[Any, ReadError[String], List[String]] = read(listConfig from TypesafeConfigSource.fromHoconString(json))
returns
List(value1, value2)NOTE:
nested("xyz")(list(string("USERNAME"))is same aslist("xyz")(string("USERNAME")) -
def
listOrSingleton[A](path: ConfigDescriptorModule.K)(desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[List[A]]
listOrSingletonis a flexible version oflist.listOrSingletonis a flexible version oflist. This means, even if the value is not of the typeListit considers the value a singleton and returnsList(singleValue)We
list("xyz")(confgDescriptor)represents just a list variant of configDescriptor within the keyxyz. That islist("key")(string)implies value ofkeyis of the typeList[String]However if the value of
keywas not a list, but instead a simple string, and if we are usinglistOrSingletonit will be considered as aList.Here is a more detailed example.
val json = s""" | USERNAME : { | "USERNAME" : "abc" | } |""".stripMargin val config = string("USERNAME") val usernames: ZIO[Any, ReadError[String], List[String]] = read(listOrSingleton("configs")(config) from TypesafeConfigSource.fromHoconString(json))
returns
List(value1) -
def
map[A](path: ConfigDescriptorModule.K)(desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[Map[ConfigDescriptorModule.K, A]]
map("xyz")(confgDescriptor)represents retrieving a map (of key value pairs) that exists within the key "xyz"map("xyz")(confgDescriptor)represents retrieving a map (of key value pairs) that exists within the key "xyz"Let's explain this in detail with an example: int("URL") implies there exists a value of the type string under the key "URL" On similar lines, map("URL")(int) implies there exists a value of the type
Mapunder the keyURLand the type of the value of each key in the map is of the type Int.Sidee note: Obviously, for complex types such as Map, you can also rely on zio-config-magnolia that allows you to retrieve any value of the type Map[String, A] for all type A, that has an instance of
Description(refer zio-config-magnolia api docs)val config = map("xyz")(int) val source: ConfigSource = TypesafeConfigSource.fromHoconString( "xyz" : { "key1" : "1" "key2" : "2" "key3" : "3" } ) // Forming a TypesafeConfigSource from string returned an Either (being able to capture errors) because // the HOCON string can be an invalid string. val result = read(config from source) // Right(Map("key1" -> 1, "key2" -> 2, "key3" -> 3))
We explained
mapusing TypesafeConfigSource. However, for zio-config source doesn't really matter. For example, lets try to fetch a map from a flattened scala Map.val source = ConfigSource.fromMap( Map( "xyz_key1" -> "1", "xyz_key2" -> "2", "xyz_key3" -> "3" ), keyDelimiter = Some('_') ) val config = read(config from source) // Right( Map("key1" -> 1, "key2" -> 2, "key3" -> 3))
-
def
map[A](desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[Map[ConfigDescriptorModule.K, A]]
Retrieve a
Mapgiven an existingConfigDescriptor.Retrieve a
Mapgiven an existingConfigDescriptor.map(configDescriptor)is similar tomap(path)(configDescriptor)except that there is nopathassociated with it. For the same reason, you would need the second version given below:def map[A](path: K)(desc: => ConfigDescriptor[A])Before we try to understand the semantics of
map(configDescriptor), let's understand the semantics ofmap(path)(configDescriptor); a function with the same name given below, but it takes a path as well.map("xyz")(confgDescriptor)represents retrieving a map (of key value pairs) that exists within the key "xyz"Let's explain this in detail with an example: int("URL") implies there exists a value of the type string under the key "URL" On similar lines, map("URL")(int) implies there exists a value of the type
Mapunder the keyURLand the type of the value of each key in the map is of the type Int.Sidee note: Obviously, for complex types such as Map, you can also rely on zio-config-magnolia that allows you to retrieve any value of the type Map[String, A] for all type A, that has an instance of
Description(refer zio-config-magnolia api docs)val config = map("xyz")(int) val source: ConfigSource = TypesafeConfigSource.fromHoconString( "xyz" : { "key1" : "1" "key2" : "2" "key3" : "3" } ) // Forming a TypesafeConfigSource from string returned an Either (being able to capture errors) because // the HOCON string can be an invalid string. val result = sourceOrFailed.flatMap(source => read(config from source)) // Map("key1" -> 1, "key2" -> 2, "key3" -> 3)
We explained
mapusing TypesafeConfigSource. However, for zio-config source doesn't really matter. For example, lets try to fetch a map from a flattened scala Map.val source = ConfigSource.fromMap( Map( "xyz_key1" -> "1", "xyz_key2" -> "2", "xyz_key3" -> "3" ), keyDelimiter = Some('_') ) val config = read(config from source) // Map("key1" -> 1, "key2" -> 2, "key3" -> 3)
Now what does it mean if we say
val config = map(int("id"))instead ofval config = map("id")(int)The difference is
map("id")(int)implies there exists a map within the keyid, whose values of are of the typeIntOn the other handmap(int("id"))implies there exists a map hose value is of the type {"id" : "Int"}Example:
val mapConfig = map(int("id")) // This means there exists a Map whose value is of the type {"String" : "Int"}. val sourceOrFailure: ConfigSource = TypesafeConfigSource.fromHoconString( s""" "abc" : { "key1" : { "id" : "2" }, "key2" : { "id" : "3" } } """" ) val result = read(nested("abc")(map(int("id"))) from source) // Map("key1" -> 1, "key2" -> 2)
This is really useful when the config source consist of a map but you need to fetch the value of the keys in the map from an nested key within itself. In this example it is "id".
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
nested[A](path: ConfigDescriptorModule.K)(desc: ⇒ ConfigDescriptor[A], keyType: Option[KeyType] = None): ConfigDescriptor[A]
nested allows us to retrieve a config from a path
K, whereKis typicallyString.nested allows us to retrieve a config from a path
K, whereKis typicallyString.Example :
val config = nested("key")(string) val mapSource = ConfigSource.fromMap( "key" : "value" ) val result = read(config from mapSource) // "value"
Note that
string("key")is same as that ofnested("key")(string) -
def
nonEmptyChunk[A](path: String)(desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[NonEmptyChunk[A]]
Similar to
listhowever the size of the values shouldn't be zero -
def
nonEmptyChunk[A](desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[NonEmptyChunk[A]]
Similar to
listhowever the size of the values shouldn't be zero -
def
nonEmptyChunkOrSingleton[A](path: String)(desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[NonEmptyChunk[A]]
Similar to
listhowever the size of the values shouldn't be zero.Similar to
listhowever the size of the values shouldn't be zero. Also it can accept a singleton value as a NonEmptyChunk -
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
set[A](path: ConfigDescriptorModule.K)(desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[Set[A]]
set("xyz")(confgDescriptor)represents just a set variant of configDescriptor within the keyxyz.set("xyz")(confgDescriptor)represents just a set variant of configDescriptor within the keyxyz. Note that,nested("xyz")(set(configDescriptor))is same asset("xyz")(configDescriptor).For example:
set("key")(string)implies value ofkeyis of the typeSet[String]Here is a more detailed example.
list("xyz")(string)would then imply, there exists a set of type String under "xyz"val json = s""" | xyz : ["a", "b"] |""".stripMargin val source: ConfigSource = TypesafeConfigSource.fromHoconString(json) read(set("xyz")(string) from source)
returns
List(value1, value2) -
def
set[A](desc: ⇒ ConfigDescriptor[A]): ConfigDescriptor[Set[A]]
set("xyz")(confgDescriptor)represents just a set variant of configDescriptor within the keyxyz.set("xyz")(confgDescriptor)represents just a set variant of configDescriptor within the keyxyz. Note that,nested("xyz")(set(configDescriptor))is same asset("xyz")(configDescriptor).For example:
set("key")(string)implies value ofkeyis of the typeSet[String]Here is a more detailed example.
list("xyz")(string)would then imply, there exists a set of type String under "xyz"val json = s""" | xyz : ["a", "b"] |""".stripMargin val source: ConfigSource = TypesafeConfigSource.fromHoconString(json) read(set("xyz")(string) from source)
returns
Right(List(value1, value2)) -
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()