branch: externals/tomelr commit ed13b73e9b68ac2c51f3545ac337bbfeba063a42 Author: Kaushal Modi <kaushal.m...@gmail.com> Commit: Kaushal Modi <kaushal.m...@gmail.com>
feat: Support string keys --- README.org | 14 ++++++++++++- test/all-tests.el | 1 + test/tinternal.el | 8 ++++++-- test/tkey.el | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/ttable.el | 33 +++++++++++++++++++++++++++++++ tomelr.el | 10 +++++++++- 6 files changed, 121 insertions(+), 4 deletions(-) diff --git a/README.org b/README.org index 3bf0b13e59..8afb94fad9 100644 --- a/README.org +++ b/README.org @@ -590,7 +590,9 @@ CLOSED: [2022-04-29 Fri 14:30] '((table-1 . ((table-1a . ((key1 . "some string") (key2 . 123))) (table-1b . ((key1 . "foo") - (key2 . 98765)))))) + (key2 . 98765))))) + (menu . (("auto weight" . ((weight . 4033) + (identifier . "foo")))))) #+end_src **** TOML #+begin_src emacs-lisp :noweb yes :exports results :wrap src toml @@ -607,6 +609,10 @@ CLOSED: [2022-04-29 Fri 14:30] [table-1.table-1b] key1 = "foo" key2 = 98765 +[menu] + [menu."auto weight"] + weight = 4033 + identifier = "foo" #+end_src **** JSON Reference #+begin_src emacs-lisp :noweb yes :exports results @@ -626,6 +632,12 @@ CLOSED: [2022-04-29 Fri 14:30] "key1": "foo", "key2": 98765 } + }, + "menu": { + "auto weight": { + "weight": 4033, + "identifier": "foo" + } } } #+end_example diff --git a/test/all-tests.el b/test/all-tests.el index adab51f609..c25fd78e48 100644 --- a/test/all-tests.el +++ b/test/all-tests.el @@ -24,6 +24,7 @@ (require 'tjson-utils) (require 'tinternal) +(require 'tkey) (require 'tscalar) (require 'tnil) (require 'tarray) diff --git a/test/tinternal.el b/test/tinternal.el index 3aa23cb06f..2a51a0b6b2 100644 --- a/test/tinternal.el +++ b/test/tinternal.el @@ -30,8 +30,12 @@ ((a . 1) (b . 2)) ;; Nested TT ((a . 1) - (b . ((c . 3) - (d . 4)))) + (b . ((c . ((d . 3) + (e . 4)))))) + ;; Nested TT with string key + ((a . 1) + (b . (("some key" . ((d . 3) + (e . 4)))))) ;; Nested TTA ((a . 1) (b . (((c . 3)) diff --git a/test/tkey.el b/test/tkey.el new file mode 100644 index 0000000000..de3453d098 --- /dev/null +++ b/test/tkey.el @@ -0,0 +1,59 @@ +;; -*- lexical-binding: t; -*- + +;; Authors: Kaushal Modi <kaushal.m...@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 keys. + +;;; Code: +(require 'tomelr) + +;;;; Scalar - Boolean +(ert-deftest test-key-with-space () + (let ((inp '( + (("some key" . t)) + )) + (ref '( + "\"some key\" = true" + )) + out) + (dolist (el inp) + (push (tomelr-encode el) out)) + (should (equal ref (nreverse out))))) + +(ert-deftest test-table-name-with-space () + (let ((inp '( + ((menu . + (("auto weight" . + ((weight . 4033) + (identifier . "foo")))))) + )) + (ref '( + "[menu] + [menu.\"auto weight\"] + weight = 4033 + identifier = \"foo\"" + )) + out) + (dolist (el inp) + (push (tomelr-encode el) out)) + (should (equal ref (nreverse out))))) + + +(provide 'tkey) diff --git a/test/ttable.el b/test/ttable.el index c84fc0bf60..d1e70b19a5 100644 --- a/test/ttable.el +++ b/test/ttable.el @@ -81,5 +81,38 @@ (push (tomelr-encode el) out)) (should (equal ref (nreverse out))))) +(ert-deftest test-nested-table-string-keys () + (let ((inp '( + ((table-1 . (("some key" . ((key1 . "some string") + (key2 . 123)))))) + ((table-1 . ((table-1a . ((key1 . "some string") + (key2 . 123))) + (table-1b . ((key1 . "foo") + (key2 . 98765))))) + (menu . (("auto weight" . ((weight . 4033) + (identifier . "foo")))))) + )) + (ref '( + "[table-1] + [table-1.\"some key\"] + key1 = \"some string\" + key2 = 123" + "[table-1] + [table-1.table-1a] + key1 = \"some string\" + key2 = 123 + [table-1.table-1b] + key1 = \"foo\" + key2 = 98765 +[menu] + [menu.\"auto weight\"] + weight = 4033 + identifier = \"foo\"" + )) + out) + (dolist (el inp) + (push (tomelr-encode el) out)) + (should (equal ref (nreverse out))))) + (provide 'ttable) diff --git a/tomelr.el b/tomelr.el index ad7906e338..c9a0694ced 100644 --- a/tomelr.el +++ b/tomelr.el @@ -131,6 +131,7 @@ set to `keyword'. Return the same STRING passed as input." ;; (message "[tomelr--print-string DBG] string = `%s'" string) + (setq string (string-trim string "\"" "\"")) (let ((special-chars '((?b . ?\b) ;U+0008 (?f . ?\f) ;U+000C (?\\ . ?\\))) @@ -181,7 +182,14 @@ Possible value of TYPE are `table', `table-array' or nil. Return nil if OBJECT cannot be encoded as a TOML string." (let ((sym-name (cond ((stringp object) - object) + ;; https://toml.io/en/v1.0.0#keys + ;; Bare keys may only contain ASCII letters, ASCII digits, + ;; underscores, and dashes (A-Za-z0-9_-). + (if (string-match-p "\\`[A-Za-z0-9_-]+\\'" object) + object + ;; Wrap string in double-quotes if it + ;; doesn't contain only A-Za-z0-9_- chars. + (format "\"%s\"" object))) ;; Symbol beginning with `:', like `:some_key' ((keywordp object) (string-trim-left (symbol-name object) ":"))