Skip to content

Commit

Permalink
renaming: enableIncompletePatches -> ignoreRedundantPatcherFields
Browse files Browse the repository at this point in the history
  • Loading branch information
krzemin committed Jan 18, 2020
1 parent 6419a19 commit 29170fa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sealed abstract class PatcherCfg

object PatcherCfg {
final class Empty extends PatcherCfg
final class EnableIncompletePatches[C <: PatcherCfg] extends PatcherCfg
final class IgnoreRedundantPatcherFields[C <: PatcherCfg] extends PatcherCfg
}

trait PatcherConfiguration {
Expand All @@ -16,20 +16,20 @@ trait PatcherConfiguration {
import c.universe._

case class PatcherConfig(
enableIncompletePatches: Boolean = false
ignoreRedundantPatcherFields: Boolean = false
)

def capturePatcherConfig(cfgTpe: Type, config: PatcherConfig = PatcherConfig()): PatcherConfig = {

import PatcherCfg._

val emptyT = typeOf[Empty]
val enableIncompletePatches = typeOf[EnableIncompletePatches[_]].typeConstructor
val ignoreRedundantPatcherFields = typeOf[IgnoreRedundantPatcherFields[_]].typeConstructor

if (cfgTpe =:= emptyT) {
config
} else if (cfgTpe.typeConstructor =:= enableIncompletePatches) {
capturePatcherConfig(cfgTpe.typeArgs.head, config.copy(enableIncompletePatches = true))
} else if (cfgTpe.typeConstructor =:= ignoreRedundantPatcherFields) {
capturePatcherConfig(cfgTpe.typeArgs.head, config.copy(ignoreRedundantPatcherFields = true))
} else {
// $COVERAGE-OFF$
c.abort(c.enclosingPosition, "Bad internal patcher config type shape!")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.scalaland.chimney.internal.dsl

import io.scalaland.chimney.internal.PatcherCfg
import io.scalaland.chimney.internal.PatcherCfg.EnableIncompletePatches
import io.scalaland.chimney.internal.PatcherCfg.IgnoreRedundantPatcherFields
import io.scalaland.chimney.internal.macros.ChimneyBlackboxMacros

import scala.language.experimental.macros

class PatcherInto[T, P, C <: PatcherCfg](val obj: T, val objPatch: P) {

def enableIncompletePatches: PatcherInto[T, P, EnableIncompletePatches[C]] =
new PatcherInto[T, P, EnableIncompletePatches[C]](obj, objPatch)
def ignoreRedundantPatcherFields: PatcherInto[T, P, IgnoreRedundantPatcherFields[C]] =
new PatcherInto[T, P, IgnoreRedundantPatcherFields[C]](obj, objPatch)

def patch: T = macro ChimneyBlackboxMacros.patchImpl[T, P, C]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ trait PatcherMacros extends PatcherConfiguration {
transformOptionalValue(fnPatch, pParam, tParam, fnObj)
)
case None =>
if (config.enableIncompletePatches) {
if (config.ignoreRedundantPatcherFields) {
None
} else {
Some(
Expand Down
13 changes: 8 additions & 5 deletions chimney/src/test/scala/io/scalaland/chimney/PatcherSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@ object PatcherSpec extends TestSuite {
User(10, Email("[email protected]"), Phone(123123123L))
}

"patch with incomplete patches" - {
"patch with redundant fields" - {

import TestDomain._

case class IncompletePatch(phone: Phone, address: String)
case class PatchWithRedundantField(phone: Phone, address: String)
// note address doesn't exist in User

val patch = IncompletePatch(Phone(4321L), "Unknown")
val patch = PatchWithRedundantField(Phone(4321L), "Unknown")

compileError("exampleUser.patchUsing(patch)")
.check("", "Field named 'address' not found in target patching type io.scalaland.chimney.TestDomain.User!")
.check(
"",
"Field named 'address' not found in target patching type io.scalaland.chimney.TestDomain.User!"
)

exampleUser
.using(patch)
.enableIncompletePatches
.ignoreRedundantPatcherFields
.patch ==>
exampleUser.copy(phone = patch.phone)
}
Expand Down

0 comments on commit 29170fa

Please sign in to comment.