Skip to content

Commit

Permalink
Merge pull request #405 from pureconfig/custom-render-options
Browse files Browse the repository at this point in the history
Allow customizing the config rendering options
  • Loading branch information
ruippeixotog committed Aug 17, 2018
2 parents 6c7f8a9 + 01a0cae commit 122ea56
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
19 changes: 15 additions & 4 deletions core/src/main/scala/pureconfig/package.scala
Expand Up @@ -236,31 +236,42 @@ package object pureconfig {
* @param conf The configuration to save
* @param outputPath Where to write the configuration
* @param overrideOutputPath Override the path if it already exists
* @param options the config rendering options
*/
@throws[IllegalArgumentException]
def saveConfigAsPropertyFile[Config](conf: Config, outputPath: Path, overrideOutputPath: Boolean = false)(implicit writer: Derivation[ConfigWriter[Config]]): Unit = {
def saveConfigAsPropertyFile[Config](
conf: Config,
outputPath: Path,
overrideOutputPath: Boolean = false,
options: ConfigRenderOptions = ConfigRenderOptions.defaults())(implicit writer: Derivation[ConfigWriter[Config]]): Unit = {

if (!overrideOutputPath && Files.isRegularFile(outputPath)) {
throw new IllegalArgumentException(s"Cannot save configuration in file '$outputPath' because it already exists")
}
if (Files isDirectory outputPath) {
throw new IllegalArgumentException(s"Cannot save configuration in file '$outputPath' because it already exists and is a directory")
}

saveConfigToStream(conf, Files.newOutputStream(outputPath))
saveConfigToStream(conf, Files.newOutputStream(outputPath), options)
}

/**
* Writes the configuration to the output stream and closes the stream
*
* @param conf The configuration to write
* @param outputStream The stream in which the configuration should be written
* @param options the config rendering options
*/
def saveConfigToStream[Config](conf: Config, outputStream: OutputStream)(implicit writer: Derivation[ConfigWriter[Config]]): Unit = {
def saveConfigToStream[Config](
conf: Config,
outputStream: OutputStream,
options: ConfigRenderOptions = ConfigRenderOptions.defaults())(implicit writer: Derivation[ConfigWriter[Config]]): Unit = {

// HOCON requires UTF-8:
// https://github.com/lightbend/config/blob/master/HOCON.md#unchanged-from-json
val printOutputStream = new OutputStreamWriter(outputStream, UTF_8)
val rawConf = writer.value.to(conf)
printOutputStream.write(rawConf.render())
printOutputStream.write(rawConf.render(options))
printOutputStream.close()
}

Expand Down
Expand Up @@ -9,8 +9,7 @@ import scala.reflect.ClassTag
import cats.data.NonEmptyList
import cats.effect.Sync
import cats.implicits._

import com.typesafe.config.{ Config => TypesafeConfig }
import com.typesafe.config.{ ConfigRenderOptions, Config => TypesafeConfig }
import pureconfig.error.{ ConfigReaderException, ConfigReaderFailures }
import pureconfig.{ ConfigReader, ConfigWriter, Derivation }

Expand Down Expand Up @@ -99,21 +98,30 @@ package object catseffect {
* @param conf The configuration to save
* @param outputPath Where to write the configuration
* @param overrideOutputPath Override the path if it already exists
* @param options the config rendering options
* @return The return action will save out the supplied configuration upon invocation
*/
def saveConfigAsPropertyFileF[F[_], A](conf: A, outputPath: Path, overrideOutputPath: Boolean = false)(implicit F: Sync[F], writer: Derivation[ConfigWriter[A]]): F[Unit] = F.delay {
pureconfig.saveConfigAsPropertyFile(conf, outputPath, overrideOutputPath)
def saveConfigAsPropertyFileF[F[_], A](
conf: A,
outputPath: Path,
overrideOutputPath: Boolean = false,
options: ConfigRenderOptions = ConfigRenderOptions.defaults())(implicit F: Sync[F], writer: Derivation[ConfigWriter[A]]): F[Unit] = F.delay {
pureconfig.saveConfigAsPropertyFile(conf, outputPath, overrideOutputPath, options)
}

/**
* Writes the configuration to the output stream and closes the stream
*
* @param conf The configuration to write
* @param outputStream The stream in which the configuration should be written
* @param options the config rendering options
* @return The return action will save out the supplied configuration upon invocation
*/
def saveConfigToStreamF[F[_], A](conf: A, outputStream: OutputStream)(implicit F: Sync[F], writer: Derivation[ConfigWriter[A]]): F[Unit] = F.delay {
pureconfig.saveConfigToStream(conf, outputStream)
def saveConfigToStreamF[F[_], A](
conf: A,
outputStream: OutputStream,
options: ConfigRenderOptions = ConfigRenderOptions.defaults())(implicit F: Sync[F], writer: Derivation[ConfigWriter[A]]): F[Unit] = F.delay {
pureconfig.saveConfigToStream(conf, outputStream, options)
}

/**
Expand Down
11 changes: 7 additions & 4 deletions modules/fs2/src/main/scala/pureconfig/module/fs2/package.scala
Expand Up @@ -9,8 +9,7 @@ import scala.reflect.ClassTag
import _root_.fs2.{ Stream, async, io, text }
import cats.effect.{ Effect, Sync }
import cats.implicits._
import com.typesafe.config.ConfigFactory

import com.typesafe.config.{ ConfigFactory, ConfigRenderOptions }
import pureconfig.{ ConfigReader, ConfigWriter, Derivation }
import pureconfig.error.ConfigReaderException

Expand Down Expand Up @@ -55,10 +54,14 @@ package object fs2 {
* Writes the configuration to a fs2 byte stream
*
* @param config The configuration to write
* @param options the config rendering options
* @return the configuration as a stream of utf-8 bytes
*/
def saveConfigToStream[F[_], A](config: A)(implicit writer: Derivation[ConfigWriter[A]]): Stream[F, Byte] = {
val asString = writer.value.to(config).render()
def saveConfigToStream[F[_], A](
config: A,
options: ConfigRenderOptions = ConfigRenderOptions.defaults())(implicit writer: Derivation[ConfigWriter[A]]): Stream[F, Byte] = {

val asString = writer.value.to(config).render(options)
Stream.emit(asString).through(text.utf8Encode)
}

Expand Down

0 comments on commit 122ea56

Please sign in to comment.