Skip to content

Commit

Permalink
Merge 274b3de into c175340
Browse files Browse the repository at this point in the history
  • Loading branch information
ruippeixotog committed Jun 9, 2017
2 parents c175340 + 274b3de commit 588869f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
### 0.7.3 (unreleased)

- New features
- `ConfigFieldMapping` now has a `withOverrides` method that allows users to easily define exceptional cases to an
existing mapping;
- Bug fixes
- A breaking change introduced in v0.7.1 where `loadConfigFromFiles` stopped allowing missing files was reverted.

Expand Down
19 changes: 17 additions & 2 deletions core/src/main/scala/pureconfig/ConfigFieldMapping.scala
@@ -1,10 +1,25 @@
package pureconfig

/**
* A mapping between case class fields and their respective keys in the config.
*/
trait ConfigFieldMapping extends (String => String) {

def apply(fieldName: String): String

/**
* Returns a `ConfigFieldMapping` that uses this mapping with some overrides.
*
* @param overrides the overrides for this mapping as pairs (field, configKey)
* @return a `ConfigFieldMapping` that maps fields using `overrides` if the field is present there and otherwise
* uses this mapping.
*/
def withOverrides(overrides: (String, String)*) =
ConfigFieldMapping(overrides.toMap.withDefault(apply))
}

object ConfigFieldMapping {

/**
* Creates a ConfigFieldMapping from the provided function, mapping names in
* the object that will receive config values to names in the configuration
Expand Down Expand Up @@ -32,9 +47,9 @@ object ConfigFieldMapping {
*/
def apply(typeFieldConvention: NamingConvention, configFieldConvention: NamingConvention): ConfigFieldMapping = {
if (typeFieldConvention == configFieldConvention) {
apply(identity(_))
apply(identity)
} else {
apply(typeFieldConvention.toTokens _ andThen configFieldConvention.fromTokens _)
apply(typeFieldConvention.toTokens _ andThen configFieldConvention.fromTokens)
}
}
}
28 changes: 28 additions & 0 deletions core/src/test/scala/pureconfig/ConfigFieldMappingSuite.scala
@@ -0,0 +1,28 @@
package pureconfig

import org.scalatest.{ FlatSpec, Matchers }

class ConfigFieldMappingSuite extends FlatSpec with Matchers {

behavior of "ConfigFieldMapping"

it should "allow defining a mapping using a function" in {
val mapping = ConfigFieldMapping(_.replace("Field", "ConfigKey"))
mapping("theBeautifulField") === "theBeautifulConfigKey"
mapping("theUglyFld") === "theUglyFld"
}

it should "allow defining a mapping between two naming conventions" in {
val mapping = ConfigFieldMapping(CamelCase, SnakeCase)
mapping("theBeautifulField") === "the_beautiful_field"
mapping("theUglyFld") === "the_ugly_fld"
}

it should "allow defining mappings with some overrides" in {
val mapping = ConfigFieldMapping(CamelCase, SnakeCase).withOverrides(
"theUglyFld" -> "the_ugly_field")

mapping("theBeautifulField") === "the_beautiful_field"
mapping("theUglyFld") === "the_ugly_field"
}
}

0 comments on commit 588869f

Please sign in to comment.