emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/tomelr 0f7a6cf7f4 2/5: fix: Case of table arrays in nes


From: ELPA Syncer
Subject: [elpa] externals/tomelr 0f7a6cf7f4 2/5: fix: Case of table arrays in nested sub tables
Date: Wed, 11 May 2022 15:57:52 -0400 (EDT)

branch: externals/tomelr
commit 0f7a6cf7f40717b3fd7735f3ee78978e2d031bdb
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>

    fix: Case of table arrays in nested sub tables
---
 test/ttable-array.el | 41 +++++++++++++++++++++++++++++++++++++++++
 tomelr.el            | 28 +++++++++++++---------------
 2 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/test/ttable-array.el b/test/ttable-array.el
index a76192ede9..caefae6b8c 100644
--- a/test/ttable-array.el
+++ b/test/ttable-array.el
@@ -127,5 +127,46 @@ See 
[org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D
       (push (tomelr-encode el) out))
     (should (equal ref (nreverse out)))))
 
+;;;; TOML Table Array in a deeply nested sub-table
+(ert-deftest test-tta-in-subtable ()
+  (let ((inp '(
+               ((logbook . ((toplevel . ((notes . (((note . "abc")
+                                                    (val . 123))
+                                                   ((note . "def")
+                                                    (val . 456))
+                                                   ((note . "ghi")
+                                                    (val . 789))
+                                                   ))))
+                            (sub1 . ((notes . (((note . "subabc")
+                                                (val . 99123))
+                                               ((note . "subdef")
+                                                (val . 99456))
+                                               )))))))
+               ))
+        (ref '(
+               "[logbook]
+  [logbook.toplevel]
+    [[logbook.toplevel.notes]]
+      note = \"abc\"
+      val = 123
+    [[logbook.toplevel.notes]]
+      note = \"def\"
+      val = 456
+    [[logbook.toplevel.notes]]
+      note = \"ghi\"
+      val = 789
+  [logbook.sub1]
+    [[logbook.sub1.notes]]
+      note = \"subabc\"
+      val = 99123
+    [[logbook.sub1.notes]]
+      note = \"subdef\"
+      val = 99456"
+               ))
+        out)
+    (dolist (el inp)
+      (push (tomelr-encode el) out))
+    (should (equal ref (nreverse out)))))
+
 
 (provide 'ttable-array)
diff --git a/tomelr.el b/tomelr.el
index 0588c290f1..7217fa23e4 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -281,28 +281,26 @@ Return nil if OBJECT cannot be encoded as a TOML string."
     ;; (message "[tomelr--print-stringlike DBG] str = %S" str)
     (when (member key-type '(table-key table-array-key))
       ;; (message "[tomelr--print-stringlike DBG] %S is symbol, type = %S, 
depth = %d"
-      ;;          object type tomelr--print-indentation-depth)
+      ;;          object key-type tomelr--print-indentation-depth)
       (if (null (nth tomelr--print-indentation-depth 
tomelr--print-table-hierarchy))
           (progn
-            (push str tomelr--print-table-hierarchy)
-            (setq tomelr--print-table-hierarchy (nreverse 
tomelr--print-table-hierarchy)))
+            (push str tomelr--print-table-hierarchy))
+
         ;; Throw away table keys collected at higher depths, if
         ;; any, from earlier runs of this function.
         (setq tomelr--print-table-hierarchy
-              (seq-take tomelr--print-table-hierarchy
-                        (1+ tomelr--print-indentation-depth)))
-        (setf (nth tomelr--print-indentation-depth 
tomelr--print-table-hierarchy)
-              str))
-      ;; (message "[tomelr--print-stringlike DBG] table hier: %S"
-      ;;          tomelr--print-table-hierarchy)
+              (reverse (seq-take (reverse tomelr--print-table-hierarchy)
+                                 (1+ tomelr--print-indentation-depth))))
+        (setf (nth 0 tomelr--print-table-hierarchy) str))
+      ;; (message "[tomelr--print-stringlike DBG] table hier: %S" 
tomelr--print-table-hierarchy)
       )
     (cond
      ;; TT keys
      ((equal key-type 'table-key)
-      (princ (format "[%s]" (string-join tomelr--print-table-hierarchy "."))))
+      (princ (format "[%s]" (string-join (reverse 
tomelr--print-table-hierarchy) "."))))
      ;; TTA keys
      ((equal key-type 'table-array-key)
-      (princ (format "[[%s]]" (string-join tomelr--print-table-hierarchy 
"."))))
+      (princ (format "[[%s]]" (string-join (reverse 
tomelr--print-table-hierarchy) "."))))
      ;; Normal keys (Alist and Plist keys)
      ((equal key-type 'normal-key)
       (princ str))
@@ -428,12 +426,11 @@ Definition of a TOML Table Array (TTA):
   ;; Throw away table keys collected at higher depths, if
   ;; any, from earlier runs of this function.
   (setq tomelr--print-table-hierarchy
-        (seq-take tomelr--print-table-hierarchy
-                  (1+ tomelr--print-indentation-depth)))
-
+        (reverse (seq-take (reverse tomelr--print-table-hierarchy)
+                           (1+ tomelr--print-indentation-depth))))
   (tomelr--print-indentation)
   (insert
-   (format "[[%s]]" (string-join tomelr--print-table-hierarchy "."))))
+   (format "[[%s]]" (string-join (reverse tomelr--print-table-hierarchy) 
"."))))
 
 (defun tomelr--print-array (array)
   "Insert a TOML representation of ARRAY at point."
@@ -480,6 +477,7 @@ See `tomelr-encode' that returns the same as a string."
   "Return a TOML representation of OBJECT as a string.
 If an error is detected during encoding, an error based on
 `tomelr-error' is signaled."
+  (setq tomelr--print-table-hierarchy ())
   (string-trim
    (tomelr--with-output-to-string (tomelr--print object))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]