New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a withOverrides method to ConfigFieldMapping #259
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0413667
Add a withExceptions method to ConfigFieldMapping
ruippeixotog 36dd621
Add withExceptions to CHANGELOG
ruippeixotog 213551c
Simplify withExceptions implementation
ruippeixotog 0afd04b
Merge branch 'master' into field-mapping-exceptions
ruippeixotog 274b3de
Rename withExceptions to withOverrides
ruippeixotog d57a9c5
Merge branch 'master' into field-mapping-exceptions
ruippeixotog File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,27 @@ | ||
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 exceptions. | ||
* | ||
* @param exceptions the exceptions for this mapping as pairs (field, configKey) | ||
* @return a `ConfigFieldMapping` that maps fields using `exceptions` if the field is present there and otherwise | ||
* uses this mapping. | ||
*/ | ||
def withExceptions(exceptions: (String, String)*) = { | ||
val map = exceptions.toMap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A slightly terser take:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nicely done, I didn't think of using |
||
ConfigFieldMapping { field => map.getOrElse(field, this.apply(field)) } | ||
} | ||
} | ||
|
||
object ConfigFieldMapping { | ||
|
||
/** | ||
* Creates a ConfigFieldMapping from the provided function, mapping names in | ||
* the object that will receive config values to names in the configuration | ||
|
@@ -32,9 +49,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
28
core/src/test/scala/pureconfig/ConfigFieldMappingSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 exceptions" in { | ||
val mapping = ConfigFieldMapping(CamelCase, SnakeCase).withExceptions( | ||
"theUglyFld" -> "the_ugly_field") | ||
|
||
mapping("theBeautifulField") === "the_beautiful_field" | ||
mapping("theUglyFld") === "the_ugly_field" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ruippeixotog It might be worth adding a comment explaining that any override will be applied globally throughout the configuration, e.g. if a
("foo", "bar")
exception is provided it will affect all fields namedfoo
regardless of which object they're in and will affect allbar
config keys regardless of the path leading up to the.bar
part.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the distinction between
ConfigFieldMapping
and aProductHint
provides that information. AConfigFieldMapping
has no type parameter and has no effect as an implicit. On the other hand, aProductHint
has a type parameter restricting the hint to certain types (possibly one or all). Different product hints may be configured with different mappings, so "any override will be applied globally throughout the configuration" isn't entirely accurate. Do you still think it would be better to improve the documentation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may seem nitpicky, but I wonder about the naming.
Exceptions
makes me think this will throw Exceptions. Maybe something like name overrides?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ruippeixotog I agree that I was wrong when I said "globally throughout the configuration." I didn't realize the scope that the
ConfigFieldMapping
was applied within was aProductHint
. (I probably should, but I didn't.)Probably nothing needs to be added. I'm skeptical I could inform without confusing.
Sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leifwickland I understand the confusion, in older pureconfig versions
ConfigFieldMapping
was the object to provide implicitly to configure product derivation :) SinceProductHint
s were added,ConfigFieldMapping
was made only one of the multiple things you can configure via aProductHint
.@derekmorr yeah, I wonder about that too. I don't think that the current name is perfect, but I'm not sure if
withOverrides
isn't less explicit/correct in the context of a mapping... I ended up usingwithExceptions
. @leifwickland, @melrief, @jcazevedo, what do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ruippeixotog I'd probably go with
withOverrides
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer
withExceptions
. They're effectively exceptions to the general mapping rule. I think any confusion users might have that this will throw exceptions can be dismissed by looking at the method's documentation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@melrief, what do you think about this? We need a fifth opinion here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer
overrides
. While I agree on the meaning of exception in this case, I also think the name can be confusing. I like override because it can be seen as "override the mapping behavior for the given fields"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All right, renamed to
withOverrides
in 274b3de! :)