Skip to content

Commit 4f0c27f

Browse files
committed
Add joker.url/parse-query function.
1 parent 3adfc65 commit 4f0c27f

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

std/url.joke

+10
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@
3131
{:added "1.0"
3232
:go "queryUnescape(s)"}
3333
[^String s])
34+
35+
(defn parse-query
36+
"Parses the URL-encoded query string and returns a map listing the vectors of values specified for each key.
37+
Always returns a non-nil map containing all the valid query parameters found.
38+
Query is expected to be a list of key=value settings separated by ampersands. A setting without
39+
an equals sign is interpreted as a key set to an empty value. Settings containing a non-URL-encoded
40+
semicolon are considered invalid. "
41+
{:added "1.3.6"
42+
:go "parseQuery(s)"}
43+
[^String s])

std/url/a_url.go

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ import (
77
"net/url"
88
)
99

10+
var __parse_query__P ProcFn = __parse_query_
11+
var parse_query_ Proc = Proc{Fn: __parse_query__P, Name: "parse_query_", Package: "std/url"}
12+
13+
func __parse_query_(_args []Object) Object {
14+
_c := len(_args)
15+
switch {
16+
case _c == 1:
17+
s := ExtractString(_args, 0)
18+
_res := parseQuery(s)
19+
return _res
20+
21+
default:
22+
PanicArity(_c)
23+
}
24+
return NIL
25+
}
26+
1027
var __path_escape__P ProcFn = __path_escape_
1128
var path_escape_ Proc = Proc{Fn: __path_escape__P, Name: "path_escape_", Package: "std/url"}
1229

std/url/a_url_slow_init.go

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ func InternsOrThunks() {
1414
}
1515
urlNamespace.ResetMeta(MakeMeta(nil, `Parses URLs and implements query escaping.`, "1.0"))
1616

17+
urlNamespace.InternVar("parse-query", parse_query_,
18+
MakeMeta(
19+
NewListFrom(NewVectorFrom(MakeSymbol("s"))),
20+
`Parses the URL-encoded query string and returns a map listing the vectors of values specified for each key.
21+
Always returns a non-nil map containing all the valid query parameters found.
22+
Query is expected to be a list of key=value settings separated by ampersands. A setting without
23+
an equals sign is interpreted as a key set to an empty value. Settings containing a non-URL-encoded
24+
semicolon are considered invalid. `, "1.3.6"))
25+
1726
urlNamespace.InternVar("path-escape", path_escape_,
1827
MakeMeta(
1928
NewListFrom(NewVectorFrom(MakeSymbol("s"))),

std/url/url_native.go

+9
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ func queryUnescape(s string) string {
2121
}
2222
return res
2323
}
24+
25+
func parseQuery(s string) Object {
26+
values, _ := url.ParseQuery(s)
27+
res := EmptyArrayMap()
28+
for k, v := range values {
29+
res.Add(MakeString(k), MakeStringVector(v))
30+
}
31+
return res
32+
}

tests/eval/url-test.joke

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns joker.test-joker.url
2+
(:require [joker.test :refer [deftest is]]
3+
[joker.url :as url]))
4+
5+
(deftest parse-query
6+
(is (= {} (url/parse-query "")))
7+
(is (= {"q" ["1"]} (url/parse-query "q=1")))
8+
(is (= {"q" [""]} (url/parse-query "q")))
9+
(is (= {"foo" ["1"] "bar" ["2" "3"]} (url/parse-query "foo=1&bar=2&bar=3"))))

0 commit comments

Comments
 (0)