Skip to content

Commit

Permalink
feat: Make a very basic nested array of TTA work
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 30, 2022
1 parent 0f4e7b4 commit a7b3a57
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
37 changes: 37 additions & 0 deletions test/ttable-array.el
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,42 @@ 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)))))

;;;; Nested array of tables
(ert-deftest test-nested-array-of-tables ()
(let ((inp '(
((fruits . (((varieties . (((name . "red delicious"))
((name . "granny smith"))))))))
;; ((fruits . (((name . "apple")
;; (physical . ((color . "red")
;; (shape . "round")))
;; (varieties . (((name . "red delicious"))
;; ((name . "granny smith")))))
;; ((name . "banana")
;; (varieties . (((name . "plantain"))))))))
))
(ref '("[[fruits]]
[[fruits.varieties]]
name = \"red delicious\"
[[fruits.varieties]]
name = \"granny smith\""
;; "[[fruits]]
;; name = \"apple\"
;; [fruits.physical]
;; color = \"red\"
;; shape = \"round\"
;; [[fruits.varieties]]
;; name = \"red delicious\"
;; [[fruits.varieties]]
;; name = \"granny smith\"
;; [[fruits]]
;; name = \"banana\"
;; [[fruits.varieties]]
;; name = \"plantain\""
))
out)
(dolist (el inp)
(push (tomelr-encode el) out))
(should (equal ref (nreverse out)))))


(provide 'ttable-array)
15 changes: 10 additions & 5 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ ordered alphabetically.")
Dictates repetitions of `tomelr-encoding-default-indentation'.")

(defvar tomelr--print-table-hierarchy ()
"Internal variable used to save the TOML Table hierarchy.")
"Internal variable used to save TOML Table hierarchies.
This variable is used for both TOML Tables and Arrays of TOML
Tables.")

(defvar tomelr--print-table-array-key ""
"Internal variable used to save the TOML Table Array name.")
Expand Down Expand Up @@ -219,8 +221,7 @@ Return nil if OBJECT cannot be encoded as a TOML string."
(string-trim-left (symbol-name object) ":"))
((symbolp object)
(symbol-name object)))))
(cond
((equal type 'table)
(when type
;; (message "[tomelr--print-stringlike DBG] %S is symbol, type = %S, depth = %d"
;; object type tomelr--print-indentation-depth)
(if (null (nth tomelr--print-indentation-depth tomelr--print-table-hierarchy))
Expand All @@ -236,9 +237,12 @@ Return nil if OBJECT cannot be encoded as a TOML string."
sym-name))
;; (message "[tomelr--print-stringlike DBG] table hier: %S"
;; tomelr--print-table-hierarchy)
)
(cond
((equal type 'table)
(princ (format "[%s]" (string-join tomelr--print-table-hierarchy "."))))
((equal type 'table-array)
(let ((tta-name (format "[[%s]]" sym-name)))
(let ((tta-name (format "[[%s]]" (string-join tomelr--print-table-hierarchy "."))))
(setq tomelr--print-table-array-key tta-name)
(princ tta-name)))
((stringp object)
Expand Down Expand Up @@ -417,7 +421,8 @@ See `tomelr-encode-array' that returns the same as a string."
(mapc (lambda (elt)
(if first
(setq first nil)
(insert (format "\n%s" tomelr--print-table-array-key)))
(tomelr--print-indentation)
(insert tomelr--print-table-array-key))
(tomelr--print elt))
array))))
(t
Expand Down

0 comments on commit a7b3a57

Please sign in to comment.