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

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

[elpa] externals/tomelr 171e5a7682 62/84: fix: List format array of plis


From: ELPA Syncer
Subject: [elpa] externals/tomelr 171e5a7682 62/84: fix: List format array of plists now detected as TOML Table Array
Date: Tue, 3 May 2022 09:58:14 -0400 (EDT)

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

    fix: List format array of plists now detected as TOML Table Array
    
    Also simplify tomelr--toml-table-array-p.
---
 test/tinternal.el | 53 ++++++++++++++++++++++++++++++++-------------
 tomelr.el         | 64 +++++++++++++++++++++++++++----------------------------
 2 files changed, 69 insertions(+), 48 deletions(-)

diff --git a/test/tinternal.el b/test/tinternal.el
index 554305b966..3aa23cb06f 100644
--- a/test/tinternal.el
+++ b/test/tinternal.el
@@ -24,8 +24,34 @@
 ;;; Code:
 (require 'tomelr)
 
+(ert-deftest test-internal-tomelr-alist-p-true ()
+  (let ((inp '(
+               ((a . 1))
+               ((a . 1) (b . 2))
+               ;; Nested TT
+               ((a . 1)
+                (b . ((c . 3)
+                      (d . 4))))
+               ;; Nested TTA
+               ((a . 1)
+                (b . (((c . 3))
+                      ((c . 300)))))
+               )))
+    (dolist (el inp)
+      (should (equal t (tomelr-alist-p el))))))
+
+(ert-deftest test-internal-tomelr-alist-p-false ()
+  (let ((inp '(
+               (a 1)
+               ((:a 1))                ;This is an array of TOML table
+               [(:a 1)]                ;This is an array of TOML table
+               (((a . 1)))             ;This is an array of TOML table
+               )))
+    (dolist (el inp)
+      (should (equal nil (tomelr-alist-p el))))))
+
 ;;;; tomelr--toml-table-p
-(ert-deftest test-internal-valid-toml-tables ()
+(ert-deftest test-internal-toml-table-true ()
   (let ((inp '(
                ((a . 1))
                (:a 1)
@@ -46,9 +72,7 @@
 (ert-deftest test-internal-toml-table-false ()
   (let ((inp '(
                (a 1)
-               ;; FIXME: TTA with plist and list notation doesn't get 
recognized as one
-               ;; ((:a 1))                ;This is an array of TOML table
-               ;;
+               ((:a 1))                ;This is an array of TOML table
                [(:a 1)]                ;This is an array of TOML table
                (((a . 1)))             ;This is an array of TOML table
                )))
@@ -80,17 +104,16 @@
     (dolist (el inp)
       (should (equal t (tomelr--toml-table-array-p el))))))
 
-;; FIXME: TTA with list notation + plist doesn't work
-;; (ert-deftest test-internal-tta-plist-list-notation-true ()
-;;   (let ((inp '(
-;;                ;; TTA with 1 table of 1 key-val pair
-;;                ((:a  1))
-;;                ;; TTA with 1 table nesting another TTA
-;;                ((:a 1 :b 2)
-;;                 (:a 100 :b 200))
-;;                )))
-;;     (dolist (el inp)
-;;       (should (equal t (tomelr--toml-table-array-p el))))))
+(ert-deftest test-internal-tta-plist-list-notation-true ()
+  (let ((inp '(
+               ;; TTA with 1 table of 1 key-val pair
+               ((:a  1))
+               ;; TTA with 1 table nesting another TTA
+               ((:a 1 :b 2)
+                (:a 100 :b 200))
+               )))
+    (dolist (el inp)
+      (should (equal t (tomelr--toml-table-array-p el))))))
 
 (ert-deftest test-internal-tta-false ()
   (let ((inp '(
diff --git a/tomelr.el b/tomelr.el
index 3f944395e1..6d9d9ffee0 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -240,9 +240,32 @@ Signal `tomelr-key-format' if it cannot be encoded as a 
string."
       (signal 'tomelr-key-format (list key))))
 
 ;;;; Objects
+;; `tomelr-alist-p' is a slightly modified version of `json-alist-p'.
+;; It fixes this scenario: (json-alist-p '((:a 1))) return t, which is wrong.
+;; '((:a 1)) is an array of plist format maps, and not an alist.
+;; (tomelr-alist-p '((:a 1))) returns nil as expected.
+(defun tomelr-alist-p (list)
+  "Non-nil if and only if LIST is an alist with simple keys."
+  (declare (pure t) (side-effect-free error-free))
+  (while (and (consp (car-safe list))
+              (not (json-plist-p (car-safe list)))
+              (atom (caar list)))
+    ;; (message "[tomelr-alist-p DBG] INSIDE list = %S, car = %S, caar = %S, 
atom of caar = %S"
+    ;;          list (car-safe list) (caar list) (atom (caar list)))
+    (setq list (cdr list)))
+  ;; (message "[tomelr-alist-p DBG] out 2 list = %S, is alist? %S" list (null 
list))
+  (null list))
+
 (defun tomelr--toml-table-p (object)
-  "Return non-nil if OBJECT can represent a TOML Table."
-  (or (json-alist-p object)
+  "Return non-nil if OBJECT can represent a TOML Table.
+
+Recognize both alist and plist format maps as TOML Tables.
+
+Examples:
+
+- Alist format: \\='((a . 1) (b . \"foo\"))
+- Plist format: \\='(:a 1 :b \"foo\")"
+  (or (tomelr-alist-p object)
       (json-plist-p object)))
 
 (defun tomelr--print-pair (key val)
@@ -273,7 +296,7 @@ This works for any MAP satisfying `mapp'."
 ;;;; Lists (including alists and plists)
 (defun tomelr--print-list (list)
   "Insert a TOML representation of LIST at point."
-  (cond ((or (json-alist-p list)
+  (cond ((or (tomelr-alist-p list)
              (json-plist-p list))
          (tomelr--print-map list))
         ((listp list)
@@ -288,36 +311,11 @@ Definition of a TOML Table Array (TTA):
 
 - OBJECT is TTA if it is of type ((TT1) (TT2) ..) where each element is a
   TOML Table (TT)."
-  (let (ttap)
-    (when (and (not (tomelr--toml-table-p object))
-               (or (listp object)
-                   (vectorp object)))
-      ;; (message "[tomelr--toml-table-array-p DBG] object = %S, type = %S, 
len = %d"
-      ;;          object (type-of object) (safe-length object))
-      (setq ttap (cond
-                  ((seq-every-p
-                    (lambda (elem)
-                      ;; (message "  [tomelr--toml-table-array-p DBG] elem = 
%S, type = %S, len = %d"
-                      ;;          elem (type-of elem) (safe-length elem))
-                      ;; (when (listp elem)
-                      ;;   (message "  [tomelr--toml-table-array-p DBG] 
sub-elem 0 = %S, type = %S, len = %d"
-                      ;;            (car elem) (type-of (car elem)) 
(safe-length (car elem))))
-                      (or
-                       (tomelr--toml-table-p elem)
-                       ;; Handling the case of a nested TTA.
-                       ;; Example:                        (((a . (((b . 2))))))
-                       (and (listp elem)                 ; ((a . (((b . 2)))))
-                            (listp (car elem))           ;  (a . (((b . 2))))
-                            (symbolp (car (car elem)))   ;   a  <- symbol
-                            ;;                           --(((b . 2)))-  <-- 
This will be a TTA.
-                            (tomelr--toml-table-array-p (cdr (car elem))))))
-                    object)
-                   t)
-                  (t
-                   nil))))
-    ;; (message "[tomelr--toml-table-array-p DBG] ttap = %S" ttap)
-    ;; (message "=====")
-    ttap))
+  (when (or (proper-list-p object)
+            (vectorp object))
+    (seq-every-p
+     (lambda (elem) (tomelr--toml-table-p elem))
+     object)))
 
 (defun tomelr--print-tta-key ()
   "Print TOML Table Array key."



reply via email to

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