|
| 1 | +package difflicious |
| 2 | +import difflicious.DiffResult.MismatchTypeResult |
| 3 | +import difflicious.differ.RecordDiffer |
| 4 | +import difflicious.utils.TypeName |
| 5 | +import difflicious.utils.TypeName.SomeTypeName |
| 6 | +import difflicious.DiffResult |
| 7 | +import difflicious.internal.EitherGetSyntax._ |
| 8 | + |
| 9 | +import scala.collection.immutable.ListMap |
| 10 | +import magnolia1._ |
| 11 | + |
| 12 | +import scala.collection.mutable |
| 13 | + |
| 14 | +trait DifferGen extends Derivation[Differ]: |
| 15 | + override def join[T](ctx: CaseClass[Differ, T]): Differ[T] = |
| 16 | + new RecordDiffer[T]( |
| 17 | + ctx.params.map { p => |
| 18 | + val getter = p.deref |
| 19 | + p.label -> Tuple2(getter.asInstanceOf[(T => Any)], p.typeclass.asInstanceOf[Differ[Any]]) |
| 20 | + }.to(ListMap), |
| 21 | + isIgnored = false, |
| 22 | + typeName = toDiffliciousTypeName(ctx.typeInfo) |
| 23 | + ) |
| 24 | + |
| 25 | + override def split[T](ctx: SealedTrait[Differ, T]): Differ[T] = |
| 26 | + new SealedTraitDiffer(ctx, isIgnored = false) |
| 27 | + |
| 28 | + final class SealedTraitDiffer[T](ctx: SealedTrait[Differ, T], isIgnored: Boolean) extends Differ[T]: |
| 29 | + override type R = DiffResult |
| 30 | + |
| 31 | + override def diff(inputs: DiffInput[T]): DiffResult = inputs match |
| 32 | + case DiffInput.ObtainedOnly(obtained) => |
| 33 | + ctx.choose(obtained)(sub => sub.typeclass.diff(DiffInput.ObtainedOnly(sub.cast(obtained)))) |
| 34 | + case DiffInput.ExpectedOnly(expected) => |
| 35 | + ctx.choose(expected)(sub => sub.typeclass.diff(DiffInput.ExpectedOnly(sub.cast(expected)))) |
| 36 | + case DiffInput.Both(obtained, expected) => |
| 37 | + ctx.choose(obtained) { obtainedSubtype => |
| 38 | + ctx.choose(expected) { expectedSubtype => |
| 39 | + if obtainedSubtype.typeInfo.short == expectedSubtype.typeInfo.short then |
| 40 | + obtainedSubtype.typeclass.asInstanceOf[Differ[T]].diff(obtainedSubtype.value, expectedSubtype.value) |
| 41 | + else MismatchTypeResult( |
| 42 | + obtained = obtainedSubtype.typeclass.diff(DiffInput.ObtainedOnly(obtainedSubtype.cast(obtained))), |
| 43 | + obtainedTypeName = toDiffliciousTypeName(obtainedSubtype.typeInfo), |
| 44 | + expected = expectedSubtype.typeclass.diff(DiffInput.ExpectedOnly(expectedSubtype.cast(expected))), |
| 45 | + expectedTypeName = toDiffliciousTypeName(expectedSubtype.typeInfo), |
| 46 | + pairType = PairType.Both, |
| 47 | + isIgnored = isIgnored, |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + override def configureIgnored(newIgnored: Boolean): Differ[T] = |
| 54 | + val newSubtypes = mutable.ArrayBuffer.empty[SealedTrait.Subtype[Differ, T, Any]] |
| 55 | + ctx.subtypes.map { sub => |
| 56 | + newSubtypes += SealedTrait.Subtype[Differ, T, Any]( |
| 57 | + typeInfo = sub.typeInfo, |
| 58 | + annotations = sub.annotations, |
| 59 | + typeAnnotations = sub.typeAnnotations, |
| 60 | + isObject = sub.isObject, |
| 61 | + index = sub.index, |
| 62 | + callByNeed = |
| 63 | + CallByNeed(sub.typeclass.configureRaw(ConfigurePath.current, ConfigureOp.SetIgnored(newIgnored)).unsafeGet.asInstanceOf[Differ[Any]]), |
| 64 | + isType = sub.cast.isDefinedAt, |
| 65 | + asType = sub.cast.apply, |
| 66 | + ) |
| 67 | + } |
| 68 | + val newSealedTrait = new SealedTrait( |
| 69 | + typeInfo = ctx.typeInfo, |
| 70 | + subtypes = IArray(newSubtypes.toArray: _*), |
| 71 | + annotations = ctx.annotations, |
| 72 | + typeAnnotations = ctx.typeAnnotations, |
| 73 | + ) |
| 74 | + new SealedTraitDiffer[T](newSealedTrait, isIgnored = newIgnored) |
| 75 | + |
| 76 | + protected def configurePath( |
| 77 | + step: String, |
| 78 | + nextPath: ConfigurePath, |
| 79 | + op: ConfigureOp |
| 80 | + ): Either[ConfigureError, Differ[T]] = |
| 81 | + ctx.subtypes.zipWithIndex.find{ (sub, _) => sub.typeInfo.short == step} match { |
| 82 | + case Some((sub, idx)) => |
| 83 | + sub.typeclass |
| 84 | + .configureRaw(nextPath, op) |
| 85 | + .map { newDiffer => |
| 86 | + val newSubtype = SealedTrait.Subtype[Differ, T, Any]( |
| 87 | + typeInfo = sub.typeInfo, |
| 88 | + annotations = sub.annotations, |
| 89 | + typeAnnotations = sub.typeAnnotations, |
| 90 | + isObject = sub.isObject, |
| 91 | + index = sub.index, |
| 92 | + callByNeed = CallByNeed(newDiffer.asInstanceOf[Differ[Any]]), |
| 93 | + isType = sub.cast.isDefinedAt, |
| 94 | + asType = sub.cast.apply, |
| 95 | + ) |
| 96 | + val newSubtypes = ctx.subtypes.updated(idx, newSubtype) |
| 97 | + val newSealedTrait = new SealedTrait( |
| 98 | + typeInfo = ctx.typeInfo, |
| 99 | + subtypes = newSubtypes, |
| 100 | + annotations = ctx.annotations, |
| 101 | + typeAnnotations = ctx.typeAnnotations, |
| 102 | + ) |
| 103 | + new SealedTraitDiffer[T](newSealedTrait, isIgnored) |
| 104 | + } |
| 105 | + case None => |
| 106 | + Left(ConfigureError.UnrecognizedSubType(nextPath, ctx.subtypes.map(_.typeInfo.short).toVector)) |
| 107 | + } |
| 108 | + |
| 109 | + protected def configurePairBy(path: ConfigurePath, op: ConfigureOp.PairBy[_]): Either[ConfigureError, Differ[T]] = |
| 110 | + Left(ConfigureError.InvalidConfigureOp(path, op, "SealedTraitDiffer")) |
| 111 | + |
| 112 | + end SealedTraitDiffer |
| 113 | + |
| 114 | + private def toDiffliciousTypeName(typeInfo: TypeInfo): SomeTypeName = { |
| 115 | + TypeName( |
| 116 | + long = s"${typeInfo.owner}.${typeInfo.short}", |
| 117 | + short = typeInfo.short, |
| 118 | + typeArguments = typeInfo.typeParams.map(toDiffliciousTypeName).toList |
| 119 | + ) |
| 120 | + } |
0 commit comments