Skip to content

Commit 63610c9

Browse files
committed
Address review feedback
1 parent 8eaff8d commit 63610c9

File tree

2 files changed

+60
-60
lines changed

2 files changed

+60
-60
lines changed

compiler/test/stdlib/uri.test.gr

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,23 +281,23 @@ testResolve("a://a.com?a#a", "", "a://a.com?a")
281281

282282
// make
283283

284-
assert Uri.make(scheme=Some("+"), percentEncodeComponents=false) ==
284+
assert Uri.make(scheme=Some("+"), encodeComponents=false) ==
285285
Err(Uri.InvalidSchemeError)
286286
assert Uri.make(
287287
userinfo=Some("%"),
288288
host=Some("a"),
289-
percentEncodeComponents=false
289+
encodeComponents=false
290290
) ==
291291
Err(Uri.InvalidUserinfoError)
292-
assert Uri.make(host=Some("#"), percentEncodeComponents=false) ==
292+
assert Uri.make(host=Some("#"), encodeComponents=false) ==
293293
Err(Uri.InvalidHostError)
294-
assert Uri.make(port=Some(-1), host=Some("a"), percentEncodeComponents=false) ==
294+
assert Uri.make(port=Some(-1), host=Some("a"), encodeComponents=false) ==
295295
Err(Uri.InvalidPortError)
296-
assert Uri.make(path="%2", percentEncodeComponents=false) ==
296+
assert Uri.make(path="%2", encodeComponents=false) ==
297297
Err(Uri.InvalidPathError)
298-
assert Uri.make(query=Some("#"), percentEncodeComponents=false) ==
298+
assert Uri.make(query=Some("#"), encodeComponents=false) ==
299299
Err(Uri.InvalidQueryError)
300-
assert Uri.make(fragment=Some("%"), percentEncodeComponents=false) ==
300+
assert Uri.make(fragment=Some("%"), encodeComponents=false) ==
301301
Err(Uri.InvalidFragmentError)
302302
assert Uri.make(userinfo=Some("me")) == Err(Uri.UserinfoWithNoHost)
303303
assert Uri.make(port=Some(80)) == Err(Uri.PortWithNoHost)
@@ -324,7 +324,7 @@ assert Result.map(
324324
path="/%20d:o'c#s!",
325325
query=Some("/a?b#c=d:ef"),
326326
fragment=Some("Ur#i-m/ake"),
327-
percentEncodeComponents=true
327+
encodeComponents=true
328328
)
329329
) ==
330330
Ok(
@@ -335,37 +335,37 @@ assert Result.map(
335335
Uri.make(
336336
scheme=Some("http"),
337337
host=Some("[1::1]"),
338-
percentEncodeComponents=true
338+
encodeComponents=true
339339
)
340340
) ==
341341
Ok("http://[1::1]")
342342

343343
// update
344344

345345
let orig = Result.unwrap(Uri.make())
346-
assert Uri.update(orig, scheme=Some(Some("+")), percentEncodeComponents=false) ==
346+
assert Uri.update(orig, scheme=Some(Some("+")), encodeComponents=false) ==
347347
Err(Uri.InvalidSchemeError)
348348
assert Uri.update(
349349
orig,
350350
userinfo=Some(Some("%")),
351351
host=Some(Some("a")),
352-
percentEncodeComponents=false
352+
encodeComponents=false
353353
) ==
354354
Err(Uri.InvalidUserinfoError)
355-
assert Uri.update(orig, host=Some(Some("#")), percentEncodeComponents=false) ==
355+
assert Uri.update(orig, host=Some(Some("#")), encodeComponents=false) ==
356356
Err(Uri.InvalidHostError)
357357
assert Uri.update(
358358
orig,
359359
port=Some(Some(1.1)),
360360
host=Some(Some("a")),
361-
percentEncodeComponents=false
361+
encodeComponents=false
362362
) ==
363363
Err(Uri.InvalidPortError)
364-
assert Uri.update(orig, path=Some("%2"), percentEncodeComponents=false) ==
364+
assert Uri.update(orig, path=Some("%2"), encodeComponents=false) ==
365365
Err(Uri.InvalidPathError)
366-
assert Uri.update(orig, query=Some(Some("#")), percentEncodeComponents=false) ==
366+
assert Uri.update(orig, query=Some(Some("#")), encodeComponents=false) ==
367367
Err(Uri.InvalidQueryError)
368-
assert Uri.update(orig, fragment=Some(Some("%")), percentEncodeComponents=false) ==
368+
assert Uri.update(orig, fragment=Some(Some("%")), encodeComponents=false) ==
369369
Err(Uri.InvalidFragmentError)
370370
assert Uri.update(orig, port=Some(Some(80))) == Err(Uri.PortWithNoHost)
371371

@@ -398,15 +398,15 @@ assert Result.map(
398398
path=Some("/%20d:o'c#s!"),
399399
query=Some(Some("/a?b#c=d:ef")),
400400
fragment=Some(Some("Ur#i-m/ake")),
401-
percentEncodeComponents=true
401+
encodeComponents=true
402402
)
403403
) ==
404404
Ok(
405405
"ht+1-tp://me%40pw@g+r%2Fa*in%3A80:80/%2520d:o'c%23s!?/a?b%23c=d:ef#Ur%23i-m/ake",
406406
)
407407
assert Result.map(
408408
Uri.toString,
409-
Uri.update(orig, host=Some(Some("[1::1]")), percentEncodeComponents=true)
409+
Uri.update(orig, host=Some(Some("[1::1]")), encodeComponents=true)
410410
) ==
411411
Ok("https://me:pw@[1::1]:80/docs?k=v#frag")
412412

@@ -420,7 +420,7 @@ let decoded = "🌾"
420420
assert Uri.decode(encoded) == Ok(decoded)
421421
assert Uri.encode(decoded) == encoded
422422

423-
assert Uri.decode("%2") == Err(Uri.InvalidPercentEncoding)
423+
assert Uri.decode("%2") == Err(Uri.InvalidEncoding)
424424

425425
// encodeQuery/decodeQuery
426426

@@ -429,4 +429,4 @@ let decoded = [("val", "🌾"), ("val🧱2", "x=y&a=b")]
429429
assert Uri.encodeQuery(decoded) == encoded
430430
assert Uri.decodeQuery(encoded) == Ok(decoded)
431431

432-
assert Uri.decodeQuery("%2") == Err(Uri.InvalidPercentEncoding)
432+
assert Uri.decodeQuery("%2") == Err(Uri.InvalidEncoding)

stdlib/uri.gr

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
*/
88
module Uri
99

10-
from "string" include String
11-
from "char" include Char
12-
from "uint8" include Uint8
13-
from "number" include Number
14-
from "bytes" include Bytes
10+
from "array" include Array
1511
from "buffer" include Buffer
12+
from "bytes" include Bytes
13+
from "char" include Char
1614
from "list" include List
17-
from "array" include Array
18-
from "map" include Map
15+
from "number" include Number
1916
from "option" include Option
2017
from "result" include Result
18+
from "string" include String
19+
from "uint8" include Uint8
2120

2221
/**
2322
* Represents a parsed RFC 3986 URI.
@@ -64,14 +63,14 @@ provide enum ResolveReferenceError {
6463
/**
6564
* Represents an error encountered while attempting to percent-decode a string.
6665
*/
67-
provide enum PercentDecodingError {
68-
InvalidPercentEncoding,
66+
provide enum DecodingError {
67+
InvalidEncoding,
6968
}
7069

7170
/**
7271
* Used to specify which characters to percent-encode from a string.
7372
*/
74-
provide enum PercentEncodeSet {
73+
provide enum EncodeSet {
7574
EncodeNonUnreserved,
7675
EncodeUserinfo,
7776
EncodeRegisteredHost,
@@ -108,7 +107,7 @@ let isPchar = char => {
108107
isUnreservedChar(char) || isSubDelim(char) || char == ':' || char == '@'
109108
}
110109

111-
let makePercentEncoder = (encodeSet: PercentEncodeSet) => {
110+
let makeEncoder = (encodeSet: EncodeSet) => {
112111
let shouldEncodeForNonUnreserved = char => !isUnreservedChar(char)
113112

114113
let shouldEncodeForUserinfo = char => {
@@ -159,18 +158,18 @@ let hexValueToChar = val => {
159158
}
160159
}
161160

162-
let percentDecodeValid = (str, onlyUnreserved=false) => {
161+
let decodeValid = (str, onlyUnreserved=false) => {
163162
let bytes = String.encode(str, String.UTF8)
164163
let len = Bytes.length(bytes)
165164
let out = Buffer.make(len)
166-
let cAt = i => Char.fromCode(Uint8.toNumber(Bytes.getUint8(i, bytes)))
165+
let charAt = i => Char.fromCode(Uint8.toNumber(Bytes.getUint8(i, bytes)))
167166
for (let mut i = 0; i < len; i += 1) {
168-
if (i >= len - 2 || cAt(i) != '%') {
167+
if (i >= len - 2 || charAt(i) != '%') {
169168
let byte = Bytes.getUint8(i, bytes)
170169
Buffer.addUint8(byte, out)
171170
} else {
172-
let next = cAt(i + 1)
173-
let nextNext = cAt(i + 2)
171+
let next = charAt(i + 1)
172+
let nextNext = charAt(i + 2)
174173
let pctDecodedVal = charToHexValue(next) * 16 + charToHexValue(nextNext)
175174
if (onlyUnreserved && !isUnreservedChar(Char.fromCode(pctDecodedVal))) {
176175
Buffer.addChar('%', out)
@@ -185,7 +184,7 @@ let percentDecodeValid = (str, onlyUnreserved=false) => {
185184
Buffer.toString(out)
186185
}
187186

188-
let isValidPercentEncoding = str => {
187+
let isValidEncoding = str => {
189188
let chars = String.explode(str)
190189
let len = Array.length(chars)
191190
for (let mut i = 0; i < len; i += 1) {
@@ -201,7 +200,7 @@ let isValidPercentEncoding = str => {
201200

202201
// Lowercase all non-percent-encoded alphabetical characters
203202
let normalizeHost = str => {
204-
let str = percentDecodeValid(str, onlyUnreserved=true)
203+
let str = decodeValid(str, onlyUnreserved=true)
205204

206205
let chars = String.explode(str)
207206
let rec getChars = (i, acc) => {
@@ -259,7 +258,7 @@ let removeDotSegments = path => {
259258
}
260259

261260
/**
262-
* Percent-encodes characters in a string based on the specified `PercentEncodeSet`.
261+
* Percent-encodes characters in a string based on the specified `EncodeSet`.
263262
*
264263
* @param str: The string to encode
265264
* @param encodeSet: An indication for which characters to percent-encode. `EncodeNonUnreserved` by default
@@ -272,7 +271,8 @@ let removeDotSegments = path => {
272271
* @since v0.6.0
273272
*/
274273
provide let encode = (str, encodeSet=EncodeNonUnreserved) => {
275-
let shouldEncode = makePercentEncoder(encodeSet)
274+
let shouldEncode = makeEncoder(encodeSet)
275+
// TODO(#2053): use String.map when implemented
276276
let chars = String.explode(str)
277277
let rec getChars = (i, acc) => {
278278
if (i < 0) {
@@ -308,10 +308,10 @@ provide let encode = (str, encodeSet=EncodeNonUnreserved) => {
308308
* @since v0.6.0
309309
*/
310310
provide let decode = str => {
311-
if (!isValidPercentEncoding(str)) {
312-
Err(InvalidPercentEncoding)
311+
if (isValidEncoding(str)) {
312+
Ok(decodeValid(str))
313313
} else {
314-
Ok(percentDecodeValid(str))
314+
Err(InvalidEncoding)
315315
}
316316
}
317317

@@ -342,9 +342,7 @@ provide let encodeQuery = (urlVals, encodeSet=EncodeNonUnreserved) => {
342342
* @since v0.6.0
343343
*/
344344
provide let decodeQuery = str => {
345-
if (!isValidPercentEncoding(str)) {
346-
Err(InvalidPercentEncoding)
347-
} else {
345+
if (isValidEncoding(str)) {
348346
let parts = Array.toList(String.split("&", str))
349347
Ok(List.map(part => {
350348
match (String.indexOf("=", part)) {
@@ -353,10 +351,12 @@ provide let decodeQuery = str => {
353351
Some(i) => {
354352
let name = String.slice(0, end=i, part)
355353
let val = String.slice(i + 1, part)
356-
(percentDecodeValid(name), percentDecodeValid(val))
354+
(decodeValid(name), decodeValid(val))
357355
},
358356
}
359357
}, parts))
358+
} else {
359+
Err(InvalidEncoding)
360360
}
361361
}
362362

@@ -625,14 +625,14 @@ let parsePath = (i, str, isAbsolute, hasAuthority) => {
625625
if (hasAuthority) {
626626
let endI = Option.unwrap(pathAbempty(i, str))
627627
let path = processPath(
628-
percentDecodeValid(String.slice(i, end=endI, str), onlyUnreserved=true)
628+
decodeValid(String.slice(i, end=endI, str), onlyUnreserved=true)
629629
)
630630
(endI, path)
631631
} else {
632632
let extraOption = if (isAbsolute) pathRootless else pathNoScheme
633633
let endI = Option.unwrap(any([pathAbsolute, extraOption, empty])(i, str))
634634
let path = processPath(
635-
percentDecodeValid(String.slice(i, end=endI, str), onlyUnreserved=true)
635+
decodeValid(String.slice(i, end=endI, str), onlyUnreserved=true)
636636
)
637637
(endI, path)
638638
}
@@ -665,7 +665,7 @@ let parseQuery = (i, str, withDelim=false) => {
665665
(
666666
endI,
667667
Some(
668-
percentDecodeValid(
668+
decodeValid(
669669
String.slice(i + (if (withDelim) 1 else 0), end=endI, str),
670670
onlyUnreserved=true
671671
),
@@ -682,7 +682,7 @@ let parseFragment = (i, str, withDelim=false) => {
682682
(
683683
endI,
684684
Some(
685-
percentDecodeValid(
685+
decodeValid(
686686
String.slice(i + (if (withDelim) 1 else 0), end=endI, str),
687687
onlyUnreserved=true
688688
),
@@ -784,11 +784,11 @@ provide let resolveReference = (base, ref) => {
784784
* @param path: The desired path for the URI. `""` by default
785785
* @param query: `Some(query)` containing the desired query string component or `None` for a query-less URI
786786
* @param fragment: `Some(fragment)` containing the desired fragment component or `None` for a fragment-less URI
787-
* @param percentEncodeComponents: Whether or not to apply percent encoding for each component to remove unsafe characters for each component
787+
* @param encodeComponents: Whether or not to apply percent encoding for each component to remove unsafe characters for each component
788788
*
789789
* @example Uri.make(scheme=Some("https"), host=Some("grain-lang.org")) // https://grain-lang.org
790-
* @example Uri.make(host=Some("g/r@in"), percentEncodeComponents=false) // Err(Uri.InvalidHostError)
791-
* @example Uri.make(scheme=Some("abc"), host=Some("g/r@in"), query=Some("k/ey=v^@l"), percentEncodeComponents=true) // abc://g%2Fr%40in?k/ey=v%5E@l
790+
* @example Uri.make(host=Some("g/r@in"), encodeComponents=false) // Err(Uri.InvalidHostError)
791+
* @example Uri.make(scheme=Some("abc"), host=Some("g/r@in"), query=Some("k/ey=v^@l"), encodeComponents=true) // abc://g%2Fr%40in?k/ey=v%5E@l
792792
* @example Uri.make(port=Some(80)) // Err(Uri.PortWithNoHost)
793793
*
794794
* @since v0.6.0
@@ -801,7 +801,7 @@ provide let make = (
801801
path="",
802802
query=None,
803803
fragment=None,
804-
percentEncodeComponents=false,
804+
encodeComponents=false,
805805
) => {
806806
match ((host, userinfo, port)) {
807807
(None, Some(_), None) => return Err(UserinfoWithNoHost),
@@ -833,7 +833,7 @@ provide let make = (
833833
}
834834
}
835835

836-
let (userinfo, host, path, query, fragment) = if (percentEncodeComponents) {
836+
let (userinfo, host, path, query, fragment) = if (encodeComponents) {
837837
let encodeOption = (val, encodeSet) =>
838838
Option.map(val => encode(val, encodeSet=encodeSet), val)
839839

@@ -915,12 +915,12 @@ enum UpdateAction<a> {
915915
* @param path: `Some(path)` containing the desired updated path component or `None` to maintain the base URI's path
916916
* @param query: `Some(query)` containing the desired updated query string component or `None` to maintain the base URI's query
917917
* @param fragment: `Some(fragment)` containing the desired updated fragment component or `None` to maintain the base URI's fragment
918-
* @param percentEncodeComponents: Whether or not to apply percent encoding for each updated component to remove unsafe characters
918+
* @param encodeComponents: Whether or not to apply percent encoding for each updated component to remove unsafe characters
919919
*
920920
* @example let uri = Result.unwrap(Uri.parse("https://grain-lang.org/docs?k=v")) // Base URI for following examples
921921
* @example Uri.update(uri, scheme=Some(Some("ftp"))) // ftp://grain-lang.org/docs?k=v
922922
* @example Uri.update(uri, query=Some(None)) // https://grain-lang.org/docs
923-
* @example Uri.update(uri, host=Some(Some("g/r@in")), percentEncodeComponents=true) // https://g%2Fr%40in/docs?k=v
923+
* @example Uri.update(uri, host=Some(Some("g/r@in")), encodeComponents=true) // https://g%2Fr%40in/docs?k=v
924924
* @example Uri.update(uri, host=Some(None), port=Some(Some(80))) // Err(Uri.PortWithNoHost)
925925
*
926926
* @since v0.6.0
@@ -934,7 +934,7 @@ provide let update = (
934934
path=None,
935935
query=None,
936936
fragment=None,
937-
percentEncodeComponents=false,
937+
encodeComponents=false,
938938
) => {
939939
let (??) = (new, old) => Option.unwrapWithDefault(old, new)
940940
match ((host ?? uri.host, userinfo ?? uri.userinfo, port ?? uri.port)) {
@@ -972,7 +972,7 @@ provide let update = (
972972
}
973973
}
974974

975-
let (userinfo, host, path, query, fragment) = if (percentEncodeComponents) {
975+
let (userinfo, host, path, query, fragment) = if (encodeComponents) {
976976
let encodeOption = (val, encodeSet) => match (val) {
977977
Some(Some(val)) => Some(Some(encode(val, encodeSet=encodeSet))),
978978
val => val,

0 commit comments

Comments
 (0)