branch: externals/yaml commit 10af746dad82ed5efc5d1b3ae39b149c68f93fcb Author: Zachary Romero <zacrom...@posteo.net> Commit: Zachary Romero <zacrom...@posteo.net>
Add option to convert string keys to symbols --- README.md | 5 ++++- yaml-tests.el | 13 ++++++++++++- yaml.el | 12 ++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 91c635d32b..11026f1559 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,10 @@ The following keyword args are accepted: objects data in. It takes the following symbols: - `hash-table` (default) - `alist` -n - `plist` + - `plist` +- `:object-key-type` specifies how map keys should be handled. It takes the following symbols: + - `string` (default) + - `symbol` keys of maps will be converted to symbols. Not that this matches the behavior of the JSON parser. - `:sequence-type` specifies the Lisp data structure to store the parsed sequences in. It takes the following symbols: - `array` (default) diff --git a/yaml-tests.el b/yaml-tests.el index 3b0c3fd8dd..67e0805b90 100644 --- a/yaml-tests.el +++ b/yaml-tests.el @@ -330,7 +330,18 @@ key-2: |2- ---" :object-type 'alist) '(("key-1" . " ---\n ---") - ("key-2" . " ---\n ---"))))) + ("key-2" . " ---\n ---")))) + (should (equal (yaml-parse-string " +key-1: |-2 + --- + --- +key-2: |2- + --- + ---" + :object-key-type 'symbol + :object-type 'alist) + '((key-1 . " ---\n ---") + (key-2 . " ---\n ---"))))) (ert-deftest yaml-parsing-completes () diff --git a/yaml.el b/yaml.el index 1184cadf67..aa9c812f75 100644 --- a/yaml.el +++ b/yaml.el @@ -74,6 +74,7 @@ This flag is intended for development purposes.") "Stack of parsing states.") (defvar yaml--parsing-object-type nil) +(defvar yaml--parsing-object-key-type nil) (defvar yaml--parsing-sequence-type nil) (defvar yaml--parsing-null-object nil) (defvar yaml--parsing-false-object nil) @@ -412,6 +413,9 @@ This flag is intended for development purposes.") (progn (let ((key (pop yaml--cache)) (table (car yaml--object-stack))) + (when (and (eql 'symbol yaml--parsing-object-key-type) + (stringp key)) + (setq key (intern key))) (puthash key value table)) (pop yaml--state-stack))) ((equal top-state :trail-comments) @@ -947,6 +951,7 @@ value. It defaults to the symbol :false." (setq yaml--anchor-mappings (make-hash-table :test 'equal)) (setq yaml--resolve-aliases nil) (let ((object-type (plist-get args :object-type)) + (object-key-type (plist-get args :object-key-type)) (sequence-type (plist-get args :sequence-type)) (null-object (plist-get args :null-object)) (false-object (plist-get args :false-object))) @@ -959,6 +964,13 @@ value. It defaults to the symbol :false." ((equal 'plist object-type) (setq yaml--parsing-object-type 'plist)) (t (error "Invalid object-type. object-type must be hash-table, alist, or plist"))) + (cond + ((or (not object-key-type) + (equal 'string object-key-type)) + (setq yaml--parsing-object-key-type 'string)) + ((equal 'symbol object-key-type) + (setq yaml--parsing-object-key-type 'symbol)) + (t (error "Invalid object-key-type. object-key-type must be string, or symbol"))) (cond ((or (not sequence-type) (equal sequence-type 'array))