Skip to content

Commit

Permalink
feat: Skip converting keys whose values are nil
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 29, 2022
1 parent 91800b2 commit 69217d4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 36 deletions.
71 changes: 39 additions & 32 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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
- [X] Float
- [X] String
- [X] Date
- [X] Date + Time with Offset
- [ ] Nil
- [X] Nil
- [ ] Arrays
- [ ] Array of Arrays
- [ ] Tables
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/all-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
(setq load-prefer-newer t)

(require 'tscalar)
(require 'tnil)
48 changes: 48 additions & 0 deletions test/tnil.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
;; -*- 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 removal of keys with nil value.

;;; 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)
11 changes: 7 additions & 4 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 69217d4

Please sign in to comment.