zio-config icon indicating copy to clipboard operation
zio-config copied to clipboard

Document yaml source

Open afsalthaj opened this issue 4 years ago • 4 comments

We have support for yaml source. Let's update this in website.

afsalthaj avatar Jul 28 '20 14:07 afsalthaj

@afsalthaj I can pick it up, if you can give me few hints on where to start, as I'm new zio-config.

shankarshastri avatar Aug 20 '20 11:08 shankarshastri

@shankarshastri

Missed out about this story. Considering its been long running issue, would you still consider giving a hand? It's going to be a simple addition here:

https://zio.github.io/zio-config/docs/sources/sources_index

afsalthaj avatar May 16 '21 08:05 afsalthaj

@afsalthaj , I was going through this one: https://github.com/zio/zio-config/blob/master/yaml/shared/src/main/scala/zio/config/yaml/YamlConfigSource.scala Should I document using this as reference?

shankarshastri avatar May 16 '21 10:05 shankarshastri

import zio.config.ConfigDescriptor._
import zio.config._
import zio.config.yaml._

import java.io.File
import java.nio.file.Path
case class MyConfig(i: Int, b: Boolean, s: String)

val yamlString =
  """
      |top:
      |  child:
      |    - i: 1
      |    b: true
      |    s: "str"
      |""".stripMargin

val myConfig: ConfigDescriptor[MyConfig] =
  ConfigDescriptor.nested("top")(
    ConfigDescriptor.nested("child")(
      (int("i") |@| boolean("b") |@| string("s"))(
        MyConfig.apply,
        MyConfig.unapply
      )
    )
  )

// String Based Yaml Read
YamlConfigSource
  .fromYamlString(yamlString)
  .flatMap(source => read(myConfig from source))

// File Based Yaml Read
val filePath = "/path/to/file"
YamlConfigSource
  .fromYamlFile(new File(filePath))
  .flatMap(source => read(myConfig from source))

// Path Based Yaml Read
YamlConfigSource
  .fromYamlPath(Path.of(filePath))
  .flatMap(source => read(myConfig from source))

Will this example suffice?

shankarshastri avatar May 16 '21 13:05 shankarshastri