Skip to content

Commit

Permalink
feat: Support basic TOML Table Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 29, 2022
1 parent ca92450 commit ad8366d
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 11 deletions.
35 changes: 32 additions & 3 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ specification defined below.
- [X] Tables
- [X] Basic Tables
- [X] Nested Tables
- [ ] Array of Tables
- [ ] Basic Array of Tables
- [-] Array of Tables
- [X] Basic Array of Tables
- [ ] Nested Array of Tables
- [ ] Property Lists
* Specification and Conversion Examples
Expand Down Expand Up @@ -483,7 +483,8 @@ CLOSED: [2022-04-29 Fri 14:30]
}
#+end_example
** TOML Array of Tables: Lists of Maps
*** Basic Array of Tables
*** DONE Basic Array of Tables
CLOSED: [2022-04-29 Fri 18:14]
**** S-expression
#+begin_src emacs-lisp :eval no :noweb-ref table-arrays
'((products . (((name . "Hammer")
Expand All @@ -500,6 +501,34 @@ CLOSED: [2022-04-29 Fri 14:30]
(note . "A note `mono`.")))))
#+end_src
**** TOML
#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml
(tomelr-encode
<<table-arrays>>)
#+end_src

#+RESULTS:
#+begin_src toml
[[products]]
name = "Hammer"
sku = 738594937
[[products]]
[[products]]
name = "Nail"
sku = 284758393
color = "gray"
[[org_logbook]]
timestamp = 2022-04-08T14:53:00-04:00
note = """
This note addition prompt shows up on typing the `C-c C-z` binding.
See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#Drawers)."""
[[org_logbook]]
timestamp = 2018-09-06T11:45:00-04:00
note = "Another note **bold** _italics_."
[[org_logbook]]
timestamp = 2018-09-06T11:37:00-04:00
note = "A note `mono`."
#+end_src

#+begin_src toml
[[products]]
name = "Hammer"
Expand Down
1 change: 1 addition & 0 deletions test/all-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
(require 'tnil)
(require 'tarray)
(require 'ttable)
(require 'ttable-array)
(require 'tplist)
66 changes: 66 additions & 0 deletions test/ttable-array.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
;; -*- lexical-binding: t; -*-

;; Authors: Kaushal Modi <kaushal.modi@gmail.com>

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Test conversion to TOML tables.

;;; Code:
(require 'tomelr)

;;;; Basic array of tables
(ert-deftest test-array-of-tables ()
(let ((inp '(((products . (((name . "Hammer")
(sku . 738594937))
()
((name . "Nail")
(sku . 284758393)
(color . "gray"))))
(org_logbook . (((timestamp . 2022-04-08T14:53:00-04:00)
(note . "This note addition prompt shows up on typing the `C-c C-z` binding.\nSee [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#Drawers)."))
((timestamp . 2018-09-06T11:45:00-04:00)
(note . "Another note **bold** _italics_."))
((timestamp . 2018-09-06T11:37:00-04:00)
(note . "A note `mono`.")))))))
(ref '("[[products]]
name = \"Hammer\"
sku = 738594937
[[products]]
[[products]]
name = \"Nail\"
sku = 284758393
color = \"gray\"
[[org_logbook]]
timestamp = 2022-04-08T14:53:00-04:00
note = \"\"\"
This note addition prompt shows up on typing the `C-c C-z` binding.
See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#Drawers).\"\"\"
[[org_logbook]]
timestamp = 2018-09-06T11:45:00-04:00
note = \"Another note **bold** _italics_.\"
[[org_logbook]]
timestamp = 2018-09-06T11:37:00-04:00
note = \"A note `mono`.\""))
out)
(dolist (el inp)
(push (tomelr-encode el) out))
(should (equal ref (nreverse out)))))


(provide 'ttable-array)
50 changes: 42 additions & 8 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ 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 the TOML Table hierarchy.")

(defvar tomelr--print-table-array-key ""
"Internal variable used to save the TOML Table Array name.")

(defvar tomelr--print-keyval-separator " = "
"String used to separate key-value pairs during encoding.")
Expand Down Expand Up @@ -236,6 +239,10 @@ Return nil if OBJECT cannot be encoded as a TOML string."
;; (message "[tomelr--print-stringlike DBG] table hier: %S"
;; tomelr--print-table-hierarchy)
(princ (format "[%s]" (string-join tomelr--print-table-hierarchy "."))))
((equal type 'table-array)
(let ((tta-name (format "[[%s]]" sym-name)))
(setq tomelr--print-table-array-key tta-name)
(princ tta-name)))
(t
(princ sym-name)))))))

Expand Down Expand Up @@ -290,6 +297,7 @@ Definition of a TOML Table (TT):
"Insert TOML representation of KEY - VAL pair at point."
(let ((type (cond
((tomelr--toml-table-p val) 'table)
((tomelr--toml-table-array-p val) 'table-array)
(t nil))))
;; (message "[tomelr--print-pair DBG] key = %S, val = %S, type = %S"
;; key val type)
Expand Down Expand Up @@ -356,21 +364,47 @@ See `tomelr-encode-alist' that returns the same as a string."
((signal 'tomelr-error (list list)))))

;;;; Arrays
(defun tomelr--toml-table-array-p (object)
"Return non-nil if OBJECT can represent a TOML Table Array.
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 (listp object)
(seq-every-p
(lambda (elem)
(tomelr--toml-table-p elem))
object)))

(defun tomelr--print-array (array)
"Insert a TOML representation of ARRAY at point.
See `tomelr-encode-array' that returns the same as a string."
(insert "[ ")
(unless (= 0 (length array))
(tomelr--with-indentation
;; (message "[tomelr--print-array DBG] array = %S, TTA = %S"
;; array (tomelr--toml-table-array-p array))
(cond
((tomelr--toml-table-array-p array)
(unless (= 0 (length array))
(let ((first t))
(mapc (lambda (elt)
(if first
(setq first nil)
(insert ", "))
(insert (format "\n%s" tomelr--print-table-array-key)))
(tomelr--print elt))
array)))
(insert " "))
(insert "]"))
array))))
(t
(insert "[ ")
(unless (= 0 (length array))
(tomelr--with-indentation
(let ((first t))
(mapc (lambda (elt)
(if first
(setq first nil)
(insert ", "))
(tomelr--print elt))
array)))
(insert " "))
(insert "]"))))

(defun tomelr-encode-array (array)
"Return a TOML representation of ARRAY.
Expand Down

0 comments on commit ad8366d

Please sign in to comment.