Skip to content

Commit

Permalink
Drop library, write custom diff function
Browse files Browse the repository at this point in the history
  • Loading branch information
rtyley committed Jan 30, 2024
1 parent 261f1d9 commit 7b8e396
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
34 changes: 20 additions & 14 deletions app/model/commands/UpdateAtomCommand.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model.commands

import java.util.Date
import ai.x.diff.DiffShow
import com.gu.atom.data.{AtomSerializer, VersionConflictError}
import com.gu.contentatom.thrift.{Atom, ContentAtomEvent, EventType, ChangeRecord => ThriftChangeRecord}
import com.gu.media.logging.Logging
Expand Down Expand Up @@ -124,22 +123,29 @@ case class UpdateAtomCommand(id: String, atom: MediaAtom, override val stores: D
}

object UpdateAtomCommand {
private val interestingFields = List("title", "category", "description", "duration", "source", "youtubeCategoryId", "license", "commentsEnabled", "channelId", "legallySensitive")
private val interestingFields = Seq[(String, MediaAtom => Option[Any])](
("title", a => Some(a.title)),
("category", a => Some(a.category)),
("description", _.description),
("duration", _.duration),
("source", _.source),
("youtubeCategoryId", _.youtubeCategoryId),
("license", _.license),
("commentsEnabled", _.composerCommentsEnabled),
("channelId", _.channelId),
("legallySensitive", _.legallySensitive)
)

// We don't use HTTP patch so diffing has to be done manually
def createDiffString(before: MediaAtom, after: MediaAtom): String = {
val fieldDiffs = DiffShow.diff[MediaAtom](before, after).string
.replaceAll("\\[*[0-9]+m", "") // Clean out the silly console coloring stuff
.split('\n')
.map(_.trim())
.filter(line => !line.contains("ERROR")) // More silly stuff from diffing library
.filter(line => interestingFields.exists(line.contains))
.mkString(", ")

if (fieldDiffs == "") { // There's a change, but in some field we're not interested in (or rather, unable to format nicely)
val changedFields = for {
(name, extractor) <- interestingFields
distinctValues = Seq(extractor(before), extractor(after)).distinct
if distinctValues.size > 1
} yield s"$name: ${distinctValues.map(_.getOrElse("[NONE]")).mkString(" -> ")}"

if (changedFields.isEmpty) { // There's a change, but in some field we're not interested in (or rather, unable to format nicely)
"Updated atom fields"
} else {
s"Updated atom fields ($fieldDiffs)"
}
} else s"Updated atom fields (${changedFields.mkString(", ")})"
}
}
3 changes: 0 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ val scanamoVersion = "1.0.0-M9"

val playJsonExtensionsVersion = "0.40.2"
val okHttpVersion = "2.4.0"
val diffVersion = "2.0.1"

val capiAwsVersion = "0.5"

val scalaTestVersion = "3.0.8"
Expand Down Expand Up @@ -135,7 +133,6 @@ lazy val app = (project in file("."))
libraryDependencies ++= Seq(
ehcache,
"com.fasterxml.jackson.core" % "jackson-databind" % jacksonDatabindVersion,
"ai.x" %% "diff" % diffVersion,
"com.amazonaws" % "aws-java-sdk-sts" % awsVersion,
"com.amazonaws" % "aws-java-sdk-ec2" % awsVersion,
"org.scalatestplus.play" %% "scalatestplus-play" % scalaTestPlusPlayVersion % "test",
Expand Down
20 changes: 11 additions & 9 deletions test/model/commands/UpdateAtomCommandTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model.commands

import com.gu.media.model.{Category, ContentChangeDetails, MediaAtom}
import model.commands.UpdateAtomCommand.createDiffString
import org.scalatest.{FunSuite, MustMatchers}

class UpdateAtomCommandTest extends FunSuite with MustMatchers {
Expand All @@ -11,7 +12,7 @@ class UpdateAtomCommandTest extends FunSuite with MustMatchers {
activeVersion = Some(1),
title = "title",
category = Category.News,
description = Some("description"),
description = Some("Example description"),
duration = Some(1),
source = Some("source"),
posterImage = None,
Expand All @@ -30,7 +31,6 @@ class UpdateAtomCommandTest extends FunSuite with MustMatchers {
keywords = List.empty,
license = None,
plutoData = None,
blockAds = false,
expiryDate = None,
legallySensitive = None,
sensitive = None,
Expand All @@ -40,16 +40,18 @@ class UpdateAtomCommandTest extends FunSuite with MustMatchers {
)

test("Diff output when nothing changes") {
val str = UpdateAtomCommand.createDiffString(mediaAtomFixture, mediaAtomFixture)
println(str)
str must be(
"""Updated atom fields (category = News$,, channelId = None,, description = Some( "description" ),, duration = Some( 1 ),, legallySensitive = None,, license = None,, source = Some( "source" ),, title = "title",, youtubeCategoryId = None,, youtubeTitle = "title")"""
)
createDiffString(mediaAtomFixture, mediaAtomFixture) must be("Updated atom fields")
}

test("Diff output when description changes") {
UpdateAtomCommand.createDiffString(mediaAtomFixture, mediaAtomFixture.copy(description = Some("New description"))) must be(
"Updated atom fields (MediaAtom( ..., description = Some( \u001B\"description\"\u001B -> \u001B\"New description\"\u001B ) ))"
createDiffString(mediaAtomFixture, mediaAtomFixture.copy(description = Some("New description"))) must be(
"Updated atom fields (description: Example description -> New description)"
)
}

test("Diff output when description is removed") {
createDiffString(mediaAtomFixture, mediaAtomFixture.copy(description = None)) must be(
"Updated atom fields (description: Example description -> [NONE])"
)
}
}

0 comments on commit 7b8e396

Please sign in to comment.