Created
June 7, 2018 23:37
-
-
Save stephennancekivell/e20bcf8624b2f16f98526d38edd3dc05 to your computer and use it in GitHub Desktop.
scala diff recursive java Map[String,Any]
This file contains hidden or 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
val skip = Set.empty[String] | |
import java.util.{Map => JMap, List => JList} | |
def diff(a: JMap[String, Any], b: JMap[String, Any], prefix: String = ""): Unit = { | |
(a.keySet().asScala.toSet ++ b.keySet().asScala.toSet).filterNot(skip.contains).foreach { key => | |
val prefixKey = s"$prefix.$key" | |
(Option(a.get(key)), Option(b.get(key))) match { | |
case (Some(av), Some(bv)) if av.isInstanceOf[JMap[String, Any]] && bv.isInstanceOf[JMap[String, Any]] => | |
diff(av.asInstanceOf[JMap[String, Any]], bv.asInstanceOf[JMap[String, Any]], prefixKey) | |
case (Some(av), Some(bv)) if av.isInstanceOf[Map[String, Any]] && bv.isInstanceOf[Map[String, Any]] => | |
diff(av.asInstanceOf[Map[String, Any]].asJava, bv.asInstanceOf[Map[String, Any]].asJava, prefixKey) | |
case (Some(av), Some(bv)) if av.isInstanceOf[Seq[Any]] && bv.isInstanceOf[Seq[Any]] => | |
val aav = av.asInstanceOf[Seq[Any]] | |
val bbv = av.asInstanceOf[Seq[Any]] | |
aav.zip(bbv).zipWithIndex.foreach { | |
case ((x, y), idx) if x.isInstanceOf[JMap[String, Any]] && y.isInstanceOf[JMap[String, Any]] => | |
diff(x.asInstanceOf[JMap[String, Any]], y.asInstanceOf[JMap[String, Any]], prefixKey + s".$idx") | |
} | |
case (Some(av), Some(bv)) if av.isInstanceOf[JList[Any]] && bv.isInstanceOf[JList[Any]] => | |
val aav = av.asInstanceOf[JList[Any]] | |
val bbv = bv.asInstanceOf[JList[Any]] | |
aav.asScala.zip(bbv.asScala).zipWithIndex.foreach { | |
case ((x, y), idx) if x.isInstanceOf[JMap[String, Any]] && y.isInstanceOf[JMap[String, Any]] => | |
diff(x.asInstanceOf[JMap[String, Any]], y.asInstanceOf[JMap[String, Any]], prefixKey + s".$idx") | |
} | |
case (Some(av), Some(bv)) if av != bv => | |
println(s"different $prefixKey $av $bv ${av.getClass}") | |
case (Some(av), Some(bv)) if av == bv => | |
println(s"same $prefixKey $av $bv ${av.getClass} ${bv.getClass}") | |
case (Some(av), None) => | |
println(s"missing a $prefixKey $av") | |
case (None, Some(bv)) => | |
println(s"missing b $prefixKey $bv") | |
case _ => | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment