branch: externals/tomelr commit 69217d47a65cb987d7d1ce32d3db5566a169ceca Author: Kaushal Modi <kaushal.m...@gmail.com> Commit: Kaushal Modi <kaushal.m...@gmail.com>
feat: Skip converting keys whose values are nil --- README.org | 71 +++++++++++++++++++++++------------------- test/all-tests.el | 1 + test/{all-tests.el => tnil.el} | 32 ++++++++++++++++--- tomelr.el | 11 ++++--- 4 files changed, 75 insertions(+), 40 deletions(-) diff --git a/README.org b/README.org index 5f1db7d305..c21b8a72e9 100644 --- a/README.org +++ b/README.org @@ -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 [1/7] +* Library Completion Status [2/7] - [X] Scalar - [X] Boolean - [X] Integer @@ -26,7 +26,7 @@ specification defined below. - [X] String - [X] Date - [X] Date + Time with Offset -- [ ] Nil +- [X] Nil - [ ] Arrays - [ ] Array of Arrays - [ ] Tables @@ -241,6 +241,43 @@ odt3 = 1979-05-27T00:32:00.999999-07:00 : "odt2": "1979-05-27T00:32:00-07:00", : "odt3": "1979-05-27T00:32:00.999999-07:00" : } +** DONE Nil +CLOSED: [2022-04-29 Fri 00:11] +**** S-expression +#+begin_src emacs-lisp :eval no :noweb-ref nil-value +'((key1 . 123) + (key2 . nil) + (key3 . "abc") + (key4 . :false) + (key5 . t)) +#+end_src +**** TOML +#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml +(tomelr-encode + <<nil-value>>) +#+end_src + +#+RESULTS: +#+begin_src toml +key1 = 123 +key3 = "abc" +key4 = false +key5 = true +#+end_src +**** JSON Reference +#+begin_src emacs-lisp :noweb yes :exports results +(json-encode-pretty + <<nil-value>>) +#+end_src + +#+RESULTS: +: { +: "key1": 123, +: "key2": null, +: "key3": "abc", +: "key4": false, +: "key5": true +: } ** TOML Arrays: Lists https://toml.io/en/v1.0.0#array *** Lists @@ -827,36 +864,6 @@ booleans = [true, false] ] } #+end_example -** Nil -**** S-expression -#+begin_src emacs-lisp :eval no :noweb-ref nil-value -'((key1 . 123) - (key2 . nil) - (key3 . "abc") - (key4 . :false) - (key5 . t)) -#+end_src -**** TOML -#+begin_src toml -key1 = 123 -key3 = "abc" -key4 = false -key5 = true -#+end_src -**** JSON Reference -#+begin_src emacs-lisp :noweb yes :exports results -(json-encode-pretty - <<nil-value>>) -#+end_src - -#+RESULTS: -: { -: "key1": 123, -: "key2": null, -: "key3": "abc", -: "key4": false, -: "key5": true -: } ** P-lists **** S-expression #+begin_src emacs-lisp :eval no :noweb-ref p-list diff --git a/test/all-tests.el b/test/all-tests.el index 209d39f3be..5b1013864c 100644 --- a/test/all-tests.el +++ b/test/all-tests.el @@ -22,3 +22,4 @@ (setq load-prefer-newer t) (require 'tscalar) +(require 'tnil) diff --git a/test/all-tests.el b/test/tnil.el similarity index 54% copy from test/all-tests.el copy to test/tnil.el index 209d39f3be..a5b8801e1e 100644 --- a/test/all-tests.el +++ b/test/tnil.el @@ -1,4 +1,4 @@ -;;; all-tests.el --- Tests for tomelr.el -*- lexical-binding: t; -*- +;; -*- lexical-binding: t; -*- ;; Authors: Kaushal Modi <kaushal.m...@gmail.com> @@ -17,8 +17,32 @@ ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see <https://www.gnu.org/licenses/>. -;;; Code: +;;; Commentary: -(setq load-prefer-newer t) +;; Test removal of keys with nil value. -(require 'tscalar) +;;; Code: +(require 'tomelr) + +;;;; Key with nil value +(ert-deftest test-nil () + (let ((inp '(((nil_key . nil)) + ((bool1 . t) + (int . +99) + (nil_key1 . nil) + (bool2 . :false) + (nil_key2 . nil) + (bool3 . "false")) + )) + (ref '("" + "bool1 = true +int = 99 +bool2 = false +bool3 = false")) + out) + (dolist (el inp) + (push (tomelr-encode el) out)) + (should (equal ref (nreverse out))))) + + +(provide 'tnil) diff --git a/tomelr.el b/tomelr.el index 341417f53c..cd7ff6b901 100644 --- a/tomelr.el +++ b/tomelr.el @@ -225,10 +225,12 @@ Signal `tomelr-key-format' if it cannot be encoded as a string." ;;;; Objects (defun tomelr--print-pair (key val) "Insert TOML representation of KEY-VAL pair at point." - (tomelr--print-indentation) ;Newline before each key in a key-value pair - (tomelr--print-key key) - (insert tomelr--print-keyval-separator) - (tomelr--print val)) + ;; (message "[tomelr--print-pair DBG] key = %S, val = %S" key val) + (when val ;Don't print the key if val is nil + (tomelr--print-indentation) ;Newline before each key in a key-value pair + (tomelr--print-key key) + (insert tomelr--print-keyval-separator) + (tomelr--print val))) (defun tomelr--print-map (map) "Insert TOML object representation of MAP at point. @@ -311,6 +313,7 @@ ARRAY can also be a list." ((hash-table-p object) (tomelr--print-unordered-map object)) ((signal 'tomelr-error (list object))))) + ;;; User API (defun tomelr-encode (object)