Code Monkey home page Code Monkey logo

tablediff's Introduction

Build Status

TableDiff

A Scala based (but usable from Java and other JVM languages) utility for finding a diff of 2 tables, taking account of their structure.

Available on maven central

There are lots of options in the library functions, so for simple usage, you probably want to create little util functions. e.g. to compare 2 lists of cases classes.

import org.suecarter.tablediff._
def listsDiffString(leftList: Iterable[Product], rightList: Iterable[Product]): String = {
  def listReport(list: Iterable[Product]) =
    ReportContent(
      columnHeaders = Seq(list.headOption.getOrElse(Nil).productElementNames.toSeq), // Scala 2.13+ for this
      mainData = list.map(x => x.productIterator.toSeq).toSeq,
    )
  StringTableDiff.diffReportToString(
    TableDiff.produceReportDiff(listReport(leftList), listReport(rightList)),
  )
}

case class A(b: Int, c: String)
listsDiffString(Seq(A(1,"x"), A(2,"y"), A(4, "c")), Seq(A(1,"x"), A(2,"z"), A(3, "c"))) 

+---------------------+
|b         |c         |
+---------------------+
|1         |x         |
|2         |[-y-]{+z+}|
|[-4-]{+3+}|c         |
+---------------------+

For usage example in java. (See this in the class org.suecarter.javatablediffexample.JavaTableDiffTest.java, the testDemo test)

Compare left table
+----+--------------+
|    |col1|col2|col3|
+----+--------------+
|row1|m1,1|m2,1|m3,1|
|row2|m1,2|m2,2|m3,2|
+----+--------------+

to right table
+----+---------------------+
|    |col1|col2|col2.5|col3|
+----+---------------------+
|row1|m1,x|m2,1|m2.5,1|m3,1|
|row2|m1,2|m2,2|m2.5,2|m3,2|
+----+---------------------+

Produces these diffs, showing us that the cell in row1,col1 has
changed from m1,1 to m1,x and a whole new column (col2.5) has 
been added.
+----+----------------------------------+
|    |col1         |col2|{+col2.5+}|col3|
+----+----------------------------------+
|row1|m1,[-1-]{+x+}|m2,1|{+m2.5,1+}|m3,1|
|row2|m1,2         |m2,2|{+m2.5,2+}|m3,2|
+----+----------------------------------+

There was HTML rendering code that was removed in V1.1. It was old and not used by me any more. Let me know if anyone still needs it.

tablediff's People

Contributors

dependabot[bot] avatar fommil avatar smootoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tablediff's Issues

Chunking is broken.

Chunking will obscure diff results on chunk boundaries if row count does not match.

Steps to reproduce:

  1. Create two tables lager then chunk size different in one row (row should be present only in one table, but not in other).
  2. Run diff.

Result: Diff will indicate deleted row correctly, but will also report identical row deletion and addition on each chunk boundary after the deleted row.

Excepted result: chunking should not affect diff results.

Just to not bother with enomorous chunks:

  1. Change default chunk size to 2.
  2. Run
    val lhs = ReportContent(
      Array(Array("No"),
            Array("0"),
            Array("1"),
            Array("2")), 1,1)
    val rhs = ReportContent(
      Array(Array("No"),
        Array("1"),
        Array("2")), 1,1)
    var diff = TableDiff.produceReportDiff(lhs, rhs)
    println(StringTableDiff.diffReportToString(diff))

Result will be:

+----------+
|No        |
+----------+
|[-0-]{+2+}|
|1         |
|[-2-]     |
+----------+
  1. Change default chunk back to 1000, run the same program. Result will be:
+-----+
|No   |
+-----+
|[-0-]|
|1    |
|2    |
+-----+

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.