Skip to content

Commit

Permalink
feat: Support nested TOML tables
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 29, 2022
1 parent 65653d5 commit a1f434f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
15 changes: 11 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 [4/7]
* Library Completion Status [5/7]
- [X] Scalar
- [X] Boolean
- [X] Integer
Expand All @@ -29,9 +29,9 @@ specification defined below.
- [X] Nil
- [X] Arrays
- [X] Array of Arrays
- [-] Tables
- [X] Tables
- [X] Basic Tables
- [ ] Nested Tables
- [X] Nested Tables
- [ ] Array of Tables
- [ ] Basic Array of Tables
- [ ] Nested Array of Tables
Expand Down Expand Up @@ -436,7 +436,8 @@ CLOSED: [2022-04-29 Fri 13:41]
}
}
#+end_example
*** Nested TOML Tables
*** DONE Nested TOML Tables
CLOSED: [2022-04-29 Fri 14:30]
**** S-expression
#+begin_src emacs-lisp :eval no :noweb-ref nested-tables
'((table-1 . ((table-1a . ((key1 . "some string")
Expand All @@ -445,6 +446,12 @@ CLOSED: [2022-04-29 Fri 13:41]
(key2 . 98765))))))
#+end_src
**** TOML
#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml
(tomelr-encode
<<nested-tables>>)
#+end_src

#+RESULTS:
#+begin_src toml
[table-1]
[table-1.table-1a]
Expand Down
18 changes: 18 additions & 0 deletions test/ttable.el
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,23 @@
(push (tomelr-encode el) out))
(should (equal ref (nreverse out)))))

;;;; Nested tables
(ert-deftest test-nested-table ()
(let ((inp '(((table-1 . ((table-1a . ((key1 . "some string")
(key2 . 123)))
(table-1b . ((key1 . "foo")
(key2 . 98765))))))))
(ref '("[table-1]
[table-1.table-1a]
key1 = \"some string\"
key2 = 123
[table-1.table-1b]
key1 = \"foo\"
key2 = 98765"))
out)
(dolist (el inp)
(push (tomelr-encode el) out))
(should (equal ref (nreverse out)))))


(provide 'ttable)
30 changes: 24 additions & 6 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ ordered alphabetically.")
"Current indentation level during encoding.
Dictates repetitions of `tomelr-encoding-default-indentation'.")

(defvar tomelr--print-table-hierarchy ()
"Internal variable used to save the TOML table hierarchy.")

(defvar tomelr--print-keyval-separator " = "
"String used to separate key-value pairs during encoding.")

Expand Down Expand Up @@ -211,12 +214,27 @@ Return nil if OBJECT cannot be encoded as a TOML string."
;; (message "[tomelr--print-stringlike DBG] %S is keyword" object)
(tomelr--print-string (symbol-name object) 1))
((symbolp object)
;; (message "[tomelr--print-stringlike DBG] %S is symbol" object)
(cond
((equal type 'table)
(princ (format "[%s]" (symbol-name object))))
(t
(princ (symbol-name object)))))))
(let ((sym-name (symbol-name object)))
;; (message "[tomelr--print-stringlike DBG] %S is symbol, type = %S, depth = %d"
;; object type tomelr--print-indentation-depth)
(cond
((equal type 'table)
(if (null (nth tomelr--print-indentation-depth tomelr--print-table-hierarchy))
(progn
(push sym-name tomelr--print-table-hierarchy)
(setq tomelr--print-table-hierarchy (nreverse 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)
sym-name))
;; (message "[tomelr--print-stringlike DBG] table hier: %S"
;; tomelr--print-table-hierarchy)
(princ (format "[%s]" (string-join tomelr--print-table-hierarchy "."))))
(t
(princ sym-name)))))))

(defun tomelr--print-key (key &optional type)
"Insert a TOML key representation of KEY at point.
Expand Down

0 comments on commit a1f434f

Please sign in to comment.