[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/tomelr a25d952a17 77/84: feat: Auto-coerce string to in
From: |
ELPA Syncer |
Subject: |
[elpa] externals/tomelr a25d952a17 77/84: feat: Auto-coerce string to integers |
Date: |
Tue, 3 May 2022 09:58:15 -0400 (EDT) |
branch: externals/tomelr
commit a25d952a17d344ac3d7396ae78a34e21b9ada14e
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>
feat: Auto-coerce string to integers
---
test/tcoerce.el | 38 ++++++++++++++++++++++++++++++++++++++
tomelr.el | 22 +++++++++++++++++++---
2 files changed, 57 insertions(+), 3 deletions(-)
diff --git a/test/tcoerce.el b/test/tcoerce.el
index 4e10dd7714..975f8625b8 100644
--- a/test/tcoerce.el
+++ b/test/tcoerce.el
@@ -55,5 +55,43 @@
(push (tomelr-encode el) out))
(should (equal ref (nreverse out)))))
+;;;; Integer Coercing
+(ert-deftest test-coerce-integer-yes ()
+ (let ((tomelr-coerce-to-types '(integer))
+ (inp '(
+ ((key . "-123"))
+ ((key . "0"))
+ ((key . "123"))
+ ;; Number too large~
+ ;; (fixnump (string-to-number "10040216507682529280")) ;=> nil
+ ;; So this number won't be coerced.
+ ((key . "10040216507682529280"))
+ ))
+ (ref '(
+ "key = -123"
+ "key = 0"
+ "key = 123"
+ "key = \"10040216507682529280\""
+ ))
+ out)
+ (dolist (el inp)
+ (push (tomelr-encode el) out))
+ (should (equal ref (nreverse out)))))
+
+(ert-deftest test-coerce-integer-no ()
+ (let ((tomelr-coerce-to-types '())
+ (inp '(
+ ((key . "123"))
+ ((key . "10040216507682529280"))
+ ))
+ (ref '(
+ "key = \"123\""
+ "key = \"10040216507682529280\""
+ ))
+ 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 858da82d89..3a93c750c8 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -42,7 +42,7 @@
"String used for a single indentation level during encoding.
This value is repeated for each further nested element.")
-(defvar tomelr-coerce-to-types '(boolean)
+(defvar tomelr-coerce-to-types '(boolean integer)
"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
@@ -240,8 +240,24 @@ Return nil if OBJECT cannot be encoded as a TOML string."
((equal type 'table-array-key)
(princ (format "[[%s]]" (string-join tomelr--print-table-hierarchy
"."))))
((stringp object)
- ;; (message "[tomelr--print-stringlike DBG] %S is string" object)
- (tomelr--print-string sym-name type))
+ (cond
+ ;; If it an integer that can be stored in the system as a
+ ;; fixnum. For example, if `object' is "10040216507682529280"
+ ;; that needs more than 64 bits to be stored as a signed
+ ;; integer, it will be automatically stored as a float. So
+ ;; (integerp (string-to-number object)) will return nil [or
+ ;; `fixnump' instead of `integerp' in Emacs 27 or newer]
+ ;; https://github.com/toml-lang/toml#integer
+ ;; Integer examples: 7, +7, -7, 7_000
+ ((and (member 'integer tomelr-coerce-to-types)
+ (string-match-p "\\`[+-]?[[:digit:]_]+\\'" object)
+ (if (functionp #'fixnump) ;`fixnump' and `bignump' get introduced
in Emacs 27.x
+ (fixnump (string-to-number object))
+ (integerp (string-to-number object)))) ;On older Emacsen,
`integerp' behaved the same as the new `fixnump'
+ (princ object))
+ (t
+ ;; (message "[tomelr--print-stringlike DBG] %S is string" object)
+ (tomelr--print-string sym-name type))))
((keywordp object)
;; (message "[tomelr--print-stringlike DBG] %S is keyword" object)
(tomelr--print-string sym-name 'keyword))
- [elpa] externals/tomelr 4dda8e6be3 46/84: chore: Style edits in a test, (continued)
- [elpa] externals/tomelr 4dda8e6be3 46/84: chore: Style edits in a test, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 5959b90ffa 44/84: fix: Don't let TOML tables be recognized as TOML tables arrays, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr b64eb07e99 49/84: fix: Detect TT with sub-tables correctly, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 4386d99a85 65/84: refactor(minor): Use `tomelr--toml-table-p`, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 26f1fc2f3c 55/84: doc: Update the medley example, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr e2b313ca3b 52/84: feat: Implement everything planned in the initial spec, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 4434ccc64b 70/84: chore: Bump version, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 10a1994aed 51/84: feat: Support (lightly tested) nested TOML Table Arrays, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr df0e73334f 59/84: Revert "doc: Update the medley example", ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ec381fd723 79/84: fix: Auto-stringify and auto-quote symbol values, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr a25d952a17 77/84: feat: Auto-coerce string to integers,
ELPA Syncer <=
- [elpa] externals/tomelr b2ba4c46b5 82/84: refactor: Minor code reorg, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr d86fd721ce 63/84: fix: Compatibility for emacs 26.3, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr a1fa47379e 84/84: chore: Add CHANGELOG generated using git cliff tool, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ae983711be 78/84: fix: Auto-stringify symbols like 1.10.1, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 61397410af 01/84: Initial empty commit, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 846676a172 06/84: feat: Add plist example, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 8bc506af5a 02/84: Add s-exp->toml examples and spec, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 7d8d41f15b 19/84: feat: Encode to multi-line TOML string automatically, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr 0ba5f2ff69 24/84: chore(readme): Move mixed type array example to the 'medley' example, ELPA Syncer, 2022/05/03
- [elpa] externals/tomelr ca9245038a 35/84: fix: Attempt to make tomelr--toml-table-p more robust, ELPA Syncer, 2022/05/03