Skip to content

Commit 168f839

Browse files
authored
keyword and symbol functions should treat strings as potentially namespaced (#1133)
Fixes #1131
1 parent 60e9e09 commit 168f839

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Fixed
99
* Fix a bug where tags in data readers were resolved as Vars within syntax quotes, rather than using standard data readers rules (#1129)
10+
* Fix a bug where `keyword` and `symbol` functions did not treat string arguments as potentially namespaced (#1131)
1011

1112
## [v0.3.2]
1213
### Added

src/basilisp/core.lpy

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,11 @@
507507
(defn symbol
508508
"Create a new symbol with ``name`` and optional namespace ``ns``.
509509

510-
``name`` may be keyword, symbol, string, or Var. If ``name`` is a keyword or symbol with a
511-
namespace, the namespace will be included in the resulting value. If ``name`` is a Var,
512-
the Var's namespace will always be the namespace of the resulting value.
510+
``name`` may be keyword, symbol, string, or Var. If ``name`` is a keyword or symbol
511+
with a namespace, the namespace will be included in the resulting value. If ``name``
512+
is a Var, the Var's namespace will always be the namespace of the resulting value. If
513+
``name`` is a string with at least one '/', the string will be split on the first '/'
514+
character with the first segment being used as ``ns`` and the second as ``name``.
513515

514516
If ``ns`` is not ``nil``, then both ``name`` and ``ns`` must be strings."
515517
([name]
@@ -522,7 +524,9 @@
522524
have the colon prefix added automatically, so it should not be provided.
523525

524526
``name`` may be keyword, symbol, or string. If ``name`` is a keyword or symbol with a
525-
namespace, the namespace will be included in the resulting value.
527+
namespace, the namespace will be included in the resulting value. If ``name`` is a
528+
string with at least one '/', the string will be split on the first '/' character
529+
with the first segment being used as ``ns`` and the second as ``name``.
526530

527531
If ``ns`` is not ``nil``, then both ``name`` and ``ns`` must be strings."
528532
([name]

src/basilisp/lang/runtime.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,9 @@ def _keyword_from_name_symbol(o: sym.Symbol) -> kw.Keyword:
10811081

10821082
@keyword_from_name.register(str)
10831083
def _keyword_from_name_str(o: str) -> kw.Keyword:
1084+
if "/" in o and o != "/":
1085+
ns, name = o.split("/", maxsplit=1)
1086+
return kw.keyword(name, ns=ns)
10841087
return kw.keyword(o)
10851088

10861089

@@ -1110,6 +1113,9 @@ def _symbol_from_name_symbol(o: sym.Symbol) -> sym.Symbol:
11101113

11111114
@symbol_from_name.register(str)
11121115
def _symbol_from_name_str(o: str) -> sym.Symbol:
1116+
if "/" in o and o != "/":
1117+
ns, name = o.split("/", maxsplit=1)
1118+
return sym.symbol(name, ns=ns)
11131119
return sym.symbol(o)
11141120

11151121

tests/basilisp/test_core_fns.lpy

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@
4141
(deftest keyword-test
4242
(testing "name only"
4343
(are [res name] (= res (keyword name))
44-
(keyword "") ""
45-
:name "name"
46-
:ns/name :ns/name
47-
:ns/name 'ns/name)
44+
(keyword "") ""
45+
(keyword "/") "/"
46+
(keyword "" "/") "//"
47+
:name "name"
48+
:ns/name "ns/name"
49+
(keyword "ns" "name/with-slash") "ns/name/with-slash"
50+
:ns/name :ns/name
51+
:ns/name 'ns/name)
4852

4953
(are [v] (thrown? python/TypeError (keyword v))
5054
nil
@@ -58,25 +62,29 @@
5862
:dotted.ns/name "dotted.ns" "name")
5963

6064
(are [ns name] (thrown? python/TypeError (keyword ns name))
61-
nil nil
62-
:kw :kw
65+
nil nil
66+
:kw :kw
6367
'sym 'sym)))
6468

6569
(deftest symbol-test
6670
(testing "name only"
6771
(are [res name] (= res (symbol name))
68-
(symbol "") ""
69-
'name "name"
70-
'ns/name :ns/name
71-
'ns/name 'ns/name
72-
'basilisp.core/map #'basilisp.core/map)
72+
(symbol "") ""
73+
(symbol "/") "/"
74+
(symbol "" "/") "//"
75+
'name "name"
76+
'ns/name "ns/name"
77+
(symbol "ns" "name/with-slash") "ns/name/with-slash"
78+
'ns/name :ns/name
79+
'ns/name 'ns/name
80+
'basilisp.core/map #'basilisp.core/map)
7381

7482
(are [v] (thrown? python/TypeError (symbol v))
7583
nil))
7684

7785
(testing "name and namespace"
7886
(are [res ns name] (= res (symbol ns name))
79-
(symbol "" "") "" ""
87+
(symbol "" "") "" ""
8088
'name nil "name"
8189
'ns/name "ns" "name"
8290
'dotted.ns/name "dotted.ns" "name")

0 commit comments

Comments
 (0)