You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by ar...@apache.org on 2020/09/02 18:55:21 UTC

[incubator-nlpcraft] branch master updated: Fix for NLPCRAFT-106.

This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/master by this push:
     new 6236a13  Fix for NLPCRAFT-106.
6236a13 is described below

commit 6236a1313ea721a18a05ab20b6597b2921c08a66
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Wed Sep 2 11:55:10 2020 -0700

    Fix for NLPCRAFT-106.
---
 nlpcraft/pom.xml                                   |  4 ----
 .../nlpcraft/common/ascii/NCAsciiTable.scala       | 22 ++++++++++++++++++++--
 .../nlpcraft/examples/sql/db/SqlAccess.scala       | 13 ++++++++-----
 .../nlpcraft/examples/sql/NCSqlExampleSpec.scala   | 17 ++++++++++-------
 4 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/nlpcraft/pom.xml b/nlpcraft/pom.xml
index eb14116..2e087bd 100644
--- a/nlpcraft/pom.xml
+++ b/nlpcraft/pom.xml
@@ -224,10 +224,6 @@
             <groupId>com.github.vertical-blank</groupId>
             <artifactId>sql-formatter</artifactId>
         </dependency>
-        <dependency>
-            <groupId>com.jakewharton.fliptables</groupId>
-            <artifactId>fliptables</artifactId>
-        </dependency>
 
         <!-- Test dependencies. -->
         <dependency>
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
index fdc79a2..a905616 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
@@ -672,13 +672,31 @@ object NCAsciiTable {
 
     /**
      * Creates new ASCII text table with all defaults.
+     *
+     * @return Newly created ASCII table.
      */
     def apply() = new NCAsciiTable
 
     /**
      * Creates new ASCII table with given header cells.
      *
-     * @param cells Header.
+     * @param hdrs Header.
+     * @return Newly created ASCII table.
+     */
+    def apply(hdrs: Any*): NCAsciiTable = new NCAsciiTable #= (hdrs: _*)
+
+    /**
+     * Creates new ASCII table with given headers and data.
+     *
+     * @param hdrs Headers.
+     * @param data Table data (sequence of rows).
+     * @return Newly created ASCII table.
      */
-    def apply(cells: Any*): NCAsciiTable = new NCAsciiTable #= (cells: _*)
+    def of(hdrs: Seq[Any], data: Seq[Seq[Any]]): NCAsciiTable = {
+        val tbl = new NCAsciiTable #= (hdrs: _*)
+
+        data.foreach(tbl += (_: _*))
+
+        tbl
+    }
 }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlAccess.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlAccess.scala
index c6c5864..b3dbb45 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlAccess.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlAccess.scala
@@ -20,8 +20,8 @@ package org.apache.nlpcraft.examples.sql.db
 import java.sql.{Connection, PreparedStatement, SQLException}
 
 import com.github.vertical_blank.sqlformatter.SqlFormatter
-import com.jakewharton.fliptables.FlipTable
 import com.typesafe.scalalogging.LazyLogging
+import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.h2.jdbc.JdbcSQLException
 import org.h2.jdbcx.JdbcDataSource
 import resource.managed
@@ -79,7 +79,7 @@ object SqlAccess extends LazyLogging {
                     val cnt = md.getColumnCount
 
                     val cols = (1 to cnt).map(md.getColumnName)
-                    var rows = List.empty[Seq[String]]
+                    var rows = Seq.empty[Seq[String]]
 
                     while (rs.next)
                         rows :+= (1 to cnt).map(i ⇒ {
@@ -99,12 +99,15 @@ object SqlAccess extends LazyLogging {
 
                         logger.info(s"Execution result, first $LOG_ROWS lines...")
 
-                        var data = rows.take(LOG_ROWS).toArray.map(_.toArray)
+                        var data = rows.take(LOG_ROWS).map(_.toSeq)
 
                         if (rows.nonEmpty && rows.size > LOG_ROWS)
-                            data = data ++ Array(cols.indices.map(_ ⇒ "...").toArray)
+                            data ++= Seq(cols.indices.map(_ ⇒ "..."))
 
-                        logger.info(s"\n${FlipTable.of(cols.toArray, data)}")
+                        logger.info(s"\n${NCAsciiTable.of(
+                            cols,
+                            data
+                        )}")
                     }
 
                     SqlResult(cols, rows)
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/sql/NCSqlExampleSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/sql/NCSqlExampleSpec.scala
index 4ed1f2b..9615994 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/sql/NCSqlExampleSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/sql/NCSqlExampleSpec.scala
@@ -23,7 +23,7 @@ import com.github.difflib.text.DiffRowGenerator
 import com.github.vertical_blank.sqlformatter.SqlFormatter
 import com.google.gson.Gson
 import com.google.gson.reflect.TypeToken
-import com.jakewharton.fliptables.FlipTable
+import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.apache.nlpcraft.examples.sql.db.SqlServer
 import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
 import org.apache.nlpcraft.model.tools.test.{NCTestClient, NCTestClientBuilder}
@@ -83,6 +83,11 @@ class NCSqlExampleSpec {
 
     private def toPretty(s: String): util.List[String] = SqlFormatter.format(s).split("\n").toSeq.asJava
 
+    /**
+     *
+     * @param multiLineOut
+     * @param cases
+     */
     private def check(multiLineOut: Boolean, cases: Case*): Unit = {
         val errs = collection.mutable.LinkedHashMap.empty[String, String]
 
@@ -108,13 +113,11 @@ class NCSqlExampleSpec {
                                 if (multiLineOut) {
                                     val rows = DIFF.generateDiffRows(toPretty(expSqlNorm), toPretty(resSqlNorm)).asScala
 
-                                    val table =
-                                        FlipTable.of(
-                                            Array("Expected", "Real"),
-                                            rows.map(p ⇒ Array(p.getOldLine, p.getNewLine)).toArray
-                                        )
+                                    val tbl = NCAsciiTable("Expected", "Real")
 
-                                    errs += txt → s"Unexpected SQL:\n$table"
+                                    rows.foreach(r => tbl += (r.getOldLine, r.getNewLine))
+
+                                    errs += txt → s"Unexpected SQL:\n$tbl"
                                 }
                                 else {
                                     val rows = DIFF.generateDiffRows(Seq(expSqlNorm).asJava, Seq(resSqlNorm).asJava).asScala