Skip to content

Commit

Permalink
fix: Better detection of nested TTA, but still wip
Browse files Browse the repository at this point in the history
This fix also breaks the plist support for TTA
  • Loading branch information
kaushalmodi committed Apr 30, 2022
1 parent 4dda8e6 commit 0f4e7b4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
7 changes: 3 additions & 4 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ the Emacs core library [[https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/j

It will then be gradually refactored so that it meets the
specification defined below.
* Library Completion Status [6/7]
* Library Completion Status [5/7]
- [X] Scalar
- [X] Boolean
- [X] Integer
Expand All @@ -35,7 +35,7 @@ specification defined below.
- [-] Array of Tables
- [X] Basic Array of Tables
- [ ] Nested Array of Tables
- [X] Property Lists
- [ ] Property Lists
* Specification and Conversion Examples
[[https://scripter.co/defining-tomelr/][Companion blog post]]

Expand Down Expand Up @@ -952,8 +952,7 @@ contributors = [
]
}
#+end_example
** DONE P-lists
CLOSED: [2022-04-29 Fri 18:42]
** TODO P-lists
**** S-expression
#+begin_src emacs-lisp :eval no :noweb-ref p-list
'(:int 123
Expand Down
10 changes: 10 additions & 0 deletions test/tinternal.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@
;;;; tomelr--toml-table-array-p
(ert-deftest test-internal-valid-tta ()
(let ((inp '(
;; ;; TTA with 1 table of 1 key-val pair
(((a . 1)))
((:a 1))
;; ;; TTA with 2 tables of 2 key-val pairs
(((a . 1) (b . 2))
((a . 100) (b . 200)))
((:a 1 :b 2)
(:a 100 :b 200))
;; TTA with 1 table nesting another TTA
(((a . (((b . 2))))))
((:a ((:b 2))))
)))
(dolist (el inp)
(should (equal t (tomelr--toml-table-array-p el))))))
Expand Down
23 changes: 12 additions & 11 deletions test/tplist.el
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@
:list_of_lists [(1 2) (3 4 5)]
:map (:key1 123
:key2 "xyz")
:list_of_maps [(:key1 123
:key2 "xyz")
(:key1 567
:key2 "klm")])))
;; :list_of_maps [(:key1 123
;; :key2 "xyz")
;; (:key1 567
;; :key2 "klm")]
)))
(ref '("int = 123
str = \"abc\"
bool_false = false
Expand All @@ -54,13 +55,13 @@ bool_list = [ true, false, true, false ]
list_of_lists = [ [ 1, 2 ], [ 3, 4, 5 ] ]
[map]
key1 = 123
key2 = \"xyz\"
[[list_of_maps]]
key1 = 123
key2 = \"xyz\"
[[list_of_maps]]
key1 = 567
key2 = \"klm\""))
key2 = \"xyz\""))
;; [[list_of_maps]]
;; key1 = 123
;; key2 = \"xyz\"
;; [[list_of_maps]]
;; key1 = 567
;; key2 = \"klm\""))
out)
(dolist (el inp)
(push (tomelr-encode el) out))
Expand Down
33 changes: 26 additions & 7 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,32 @@ 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)."
(when (and (not (tomelr--toml-table-p object))
(not (stringp object))
(mapp object)) ;Because `mapp' is non-nil for strings too
(seq-every-p
(lambda (elem)
(tomelr--toml-table-p elem))
object)))
(let (ttap)
(when (and (not (tomelr--toml-table-p object))
(listp 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))))
(tomelr--toml-table-p elem))
object)
t)
;; Handling the case of a nested TTA.
;; Example: (((a . (((b . 2))))))
((and (listp (car object)) ; ((a . (((b . 2)))))
(listp (car (car object))) ; (a . (((b . 2))))
(symbolp (car (car (car object))))) ; a <- symbol
;; ------(((b . 2)))----- <-- This will be a TTA.
(tomelr--toml-table-array-p (cdr (car (car object)))))
(t
nil))))
ttap))

(defun tomelr--print-array (array)
"Insert a TOML representation of ARRAY at point.
Expand Down

0 comments on commit 0f4e7b4

Please sign in to comment.