[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tomelr ebe5959174 75/84: feat: Auto-coerce string to bo
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tomelr ebe5959174 75/84: feat: Auto-coerce string to boolean |
Date: |
Tue, 3 May 2022 09:58:15 -0400 (EDT) |
branch: externals/tomelr
commit ebe5959174812ffc3cf7d88040b854599b15a88a
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>
feat: Auto-coerce string to boolean
---
test/all-tests.el | 1 +
test/tcoerce.el | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
tomelr.el | 23 +++++++++++++++++++---
3 files changed, 80 insertions(+), 3 deletions(-)
diff --git a/test/all-tests.el b/test/all-tests.el
index c25fd78e48..03f614f844 100644
--- a/test/all-tests.el
+++ b/test/all-tests.el
@@ -31,3 +31,4 @@
(require 'ttable)
(require 'ttable-array)
(require 'tplist)
+(require 'tcoerce)
diff --git a/test/tcoerce.el b/test/tcoerce.el
new file mode 100644
index 0000000000..4e10dd7714
--- /dev/null
+++ b/test/tcoerce.el
@@ -0,0 +1,59 @@
+;; -*- 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 type coercing from strings.
+
+;;; Code:
+(require 'tomelr)
+
+;;;; Boolean Coercing
+(ert-deftest test-coerce-boolean-yes ()
+ (let ((tomelr-coerce-to-types '(boolean))
+ (inp '(
+ ((key1 . "true"))
+ ((key2 . "false"))
+ ))
+ (ref '(
+ "key1 = true"
+ "key2 = false"
+ ))
+ out)
+ (dolist (el inp)
+ (push (tomelr-encode el) out))
+ (should (equal ref (nreverse out)))))
+
+(ert-deftest test-coerce-boolean-no ()
+ (let ((tomelr-coerce-to-types '())
+ (inp '(
+ ((key1 . "true"))
+ ((key2 . "false"))
+ ))
+ (ref '(
+ "key1 = \"true\""
+ "key2 = \"false\""
+ ))
+ out)
+ (dolist (el inp)
+ (push (tomelr-encode el) out))
+ (should (equal ref (nreverse out)))))
+
+
+(provide 'tcoerce)
diff --git a/tomelr.el b/tomelr.el
index c9a0694ced..08f877d562 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -35,13 +35,22 @@
;;; Variables
-(defvar tomelr-false '(:false 'false "false")
+(defvar tomelr-false '(:false 'false)
"S-exp values to be interpreted as TOML `false'.")
(defvar tomelr-encoding-default-indentation " "
"String used for a single indentation level during encoding.
This value is repeated for each further nested element.")
+(defvar tomelr-coerce-to-types '(boolean)
+ "List of TOML types to which the TOML strings will be attempted to be
coerced.
+
+Valid symbols that can be present in this list: boolean, integer, float
+
+For example, if this list contains `boolean' and if a string
+value is exactly \"true\", it will coerce to TOML boolean
+`true'.")
+
;;;; Internal Variables
(defvar tomelr--print-indentation-prefix "\n"
"String used to start indentation during encoding.")
@@ -117,8 +126,16 @@ This macro sets up appropriate variable bindings for
(defun tomelr--print-boolean (object)
"Insert TOML boolean true or false at point if OBJECT is a boolean.
Return nil if OBJECT is not recognized as a TOML boolean."
- (prog1 (setq object (cond ((eq object t) "true")
- ((member object tomelr-false) "false")))
+ (prog1 (setq object (cond ((or
+ (eq object t)
+ (and (member 'boolean tomelr-coerce-to-types)
+ (equal object "true")))
+ "true")
+ ((or
+ (member object tomelr-false)
+ (and (member 'boolean tomelr-coerce-to-types)
+ (equal object "false")))
+ "false")))
(and object (insert object))))
;;;; Strings
- [elpa] externals/tomelr 6d2be83669 69/84: chore: package-lint fixes, (continued)
- [elpa] externals/tomelr 6d2be83669 69/84: chore: package-lint fixes, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 41ccea4ebe 64/84: refactor: Remove unnecessary tomelr-encode-keyword, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 406f4922a8 58/84: test: Add tests for json.el functions used in tomelr, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr b3b6a28d15 68/84: doc: Add more examples, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr f9d670e165 56/84: refactor: Clean up unused code, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 511240765b 71/84: chore: Ignore .elc files, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ed13b73e9b 74/84: feat: Support string keys, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr de661716af 72/84: style: Remove space from after and before [ and ] brackets, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 334b7cba54 76/84: refactor: Don't attempt to triple-quote TOML keys, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr c2d1328c44 80/84: fix: Boolean coercing when value is a symbol true or false, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ebe5959174 75/84: feat: Auto-coerce string to boolean,
ELPA Syncer <=
- [elpa] externals/tomelr a676192b43 81/84: fix: Integer coercing of a number strings with underscores, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 8da825067b 15/84: chore(readme): Update, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 65653d53ee 30/84: chore(doc): M-x checkdoc fixes, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr bcaea16ec0 39/84: chore(doc): Clean up old code block, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 45542fb234 67/84: chore: Bump version, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 7c004af90d 66/84: style: Remove unnecessary tomelr-encode-* functions, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 568de5efb2 83/84: chore: Bump version, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 0d4674f782 42/84: test: Test `tomelr--toml-table-p`, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 0eb4fa04ac 43/84: fix: Don't let array of TOML tables be recognized as TOML tables, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr a33dbd1286 50/84: fix: Detect nested TTA correctly when not present in first TT key, ELPA Syncer, 2022/05/03