Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 613d7dd

Browse files
author
Uziel Maximo Luduena
committed
Changed Hack Collections with Hack Arrays
1 parent 3b57c2b commit 613d7dd

File tree

8 files changed

+82
-92
lines changed

8 files changed

+82
-92
lines changed

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,20 @@ A simple typed request router. Example:
2525
* callable, but classname<MyWebControllerBase> is also a
2626
* common choice.
2727
*/
28-
type TResponder = (function(ImmMap<string, string>):string);
28+
type TResponder = (function(dict<string, string>): string);
2929
3030
final class BaseRouterExample extends BaseRouter<TResponder> {
31-
protected function getRoutes(
32-
): ImmMap<HttpMethod, ImmMap<string, TResponder>> {
33-
return ImmMap {
34-
HttpMethod::GET => ImmMap {
35-
'/' =>
36-
($_params) ==> 'Hello, world',
37-
'/user/{user_name}' =>
38-
($params) ==> 'Hello, '.$params['user_name'],
39-
},
40-
HttpMethod::POST => ImmMap {
31+
<<__Override>>
32+
protected function getRoutes(): dict<HttpMethod, dict<string, TResponder>> {
33+
return dict[
34+
HttpMethod::GET => dict[
35+
'/' => ($_params) ==> 'Hello, world',
36+
'/user/{user_name}' => ($params) ==> 'Hello, '.$params['user_name'],
37+
],
38+
HttpMethod::POST => dict[
4139
'/' => ($_params) ==> 'Hello, POST world',
42-
},
43-
};
40+
],
41+
];
4442
}
4543
}
4644
```

examples/BaseRouterExample.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,30 @@
2222
* callable, but classname<MyWebControllerBase> is also a
2323
* common choice.
2424
*/
25-
type TResponder = (function(dict<string, string>):string);
25+
type TResponder = (function(dict<string, string>): string);
2626

2727
final class BaseRouterExample extends BaseRouter<TResponder> {
2828
<<__Override>>
29-
protected function getRoutes(
30-
): ImmMap<HttpMethod, ImmMap<string, TResponder>> {
31-
return ImmMap {
32-
HttpMethod::GET => ImmMap {
33-
'/' =>
34-
($_params) ==> 'Hello, world',
35-
'/user/{user_name}' =>
36-
($params) ==> 'Hello, '.$params['user_name'],
37-
},
38-
HttpMethod::POST => ImmMap {
29+
protected function getRoutes(): dict<HttpMethod, dict<string, TResponder>> {
30+
return dict[
31+
HttpMethod::GET => dict[
32+
'/' => ($_params) ==> 'Hello, world',
33+
'/user/{user_name}' => ($params) ==> 'Hello, '.$params['user_name'],
34+
],
35+
HttpMethod::POST => dict[
3936
'/' => ($_params) ==> 'Hello, POST world',
40-
},
41-
};
37+
],
38+
];
4239
}
4340
}
4441

45-
function get_example_inputs(): ImmVector<(HttpMethod, string)> {
46-
return ImmVector {
42+
function get_example_inputs(): vec<(HttpMethod, string)> {
43+
return vec[
4744
tuple(HttpMethod::GET, '/'),
4845
tuple(HttpMethod::GET, '/user/foo'),
4946
tuple(HttpMethod::GET, '/user/bar'),
5047
tuple(HttpMethod::POST, '/'),
51-
};
48+
];
5249
}
5350

5451
<<__EntryPoint>>
@@ -58,12 +55,7 @@ function main(): noreturn {
5855
list($method, $path) = $input;
5956

6057
list($responder, $params) = $router->routeMethodAndPath($method, $path);
61-
\printf(
62-
"%s %s\n\t%s\n",
63-
$method,
64-
$path,
65-
$responder(dict($params)),
66-
);
58+
\printf("%s %s\n\t%s\n", $method, $path, $responder($params));
6759
}
6860
exit(0);
6961
}

examples/UriPatternsExample.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
require_once(__DIR__.'/../vendor/hh_autoload.php');
1818

19-
use type Facebook\HackRouter\{
19+
use type Facebook\HackRouter\{
2020
BaseRouter,
2121
GetFastRoutePatternFromUriPattern,
2222
GetUriBuilderFromUriPattern,
2323
HasUriPattern,
2424
HttpMethod,
2525
RequestParameters,
26-
UriPattern
26+
UriPattern,
2727
};
2828

2929
<<__ConsistentConstruct>>
@@ -38,12 +38,10 @@ final protected function getRequestParameters(): RequestParameters {
3838
return $this->uriParameters;
3939
}
4040

41-
public function __construct(
42-
ImmMap<string, string> $uri_parameter_values,
43-
) {
41+
public function __construct(dict<string, string> $uri_parameter_values) {
4442
$this->uriParameters = new RequestParameters(
4543
static::getUriPattern()->getParameters(),
46-
ImmVector { },
44+
vec[],
4745
$uri_parameter_values,
4846
);
4947
}
@@ -78,34 +76,33 @@ public function getResponse(): string {
7876
type TResponder = classname<WebController>;
7977

8078
final class UriPatternsExample extends BaseRouter<TResponder> {
81-
public static function getControllers(): ImmVector<TResponder> {
82-
return ImmVector {
79+
public static function getControllers(): vec<TResponder> {
80+
return vec[
8381
HomePageController::class,
8482
UserPageController::class,
85-
};
83+
];
8684
}
8785

8886
<<__Override>>
89-
public function getRoutes(
90-
): ImmMap<HttpMethod, ImmMap<string, TResponder>> {
87+
public function getRoutes(): dict<HttpMethod, dict<string, TResponder>> {
9188
$urls_to_controllers = dict[];
9289
foreach (self::getControllers() as $controller) {
9390
$pattern = $controller::getFastRoutePattern();
9491
$urls_to_controllers[$pattern] = $controller;
9592
}
96-
return ImmMap {
97-
HttpMethod::GET => new ImmMap($urls_to_controllers),
98-
};
93+
return dict [
94+
HttpMethod::GET => $urls_to_controllers,
95+
];
9996
}
10097
}
10198

102-
function get_example_paths(): ImmVector<string> {
103-
return ImmVector {
99+
function get_example_paths(): vec<string> {
100+
return vec[
104101
HomePageController::getUriBuilder()->getPath(),
105102
UserPageController::getUriBuilder()
106103
->setString('user_name', 'Mr Hankey')
107104
->getPath(),
108-
};
105+
];
109106
}
110107

111108
function main(): void {
@@ -115,11 +112,7 @@ function main(): void {
115112
HttpMethod::GET,
116113
$path,
117114
);
118-
\printf(
119-
"GET %s\n\t%s\n",
120-
$path,
121-
(new $controller($params))->getResponse(),
122-
);
115+
\printf("GET %s\n\t%s\n", $path, (new $controller($params))->getResponse());
123116
}
124117
}
125118

src/request-parameters/RequestParametersBase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ abstract class RequestParametersBase {
1616
private dict<string, RequestParameter> $requiredSpecs;
1717
private dict<string, RequestParameter> $optionalSpecs;
1818

19-
protected ImmMap<string, string> $values;
19+
protected dict<string, string> $values;
2020

2121
public function __construct(
2222
Traversable<RequestParameter> $required_specs,
2323
Traversable<RequestParameter> $optional_specs,
2424
KeyedTraversable<string, string> $values,
2525
) {
26-
$this->values = new ImmMap($values);
26+
$this->values = dict($values);
2727
$spec_vector_to_map = (
2828
Traversable<RequestParameter> $specs
2929
) ==> Dict\pull($specs, $it ==> $it, $it ==> $it->getName());

src/router/BaseRouter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ abstract protected function getRoutes(
2020
final public function routeMethodAndPath(
2121
HttpMethod $method,
2222
string $path,
23-
): (TResponder, ImmMap<string, string>) {
23+
): (TResponder, dict<string, string>) {
2424
$resolver = $this->getResolver();
2525
try {
2626
list($responder, $data) = $resolver->resolve($method, $path);
2727
$data = Dict\map($data, $value ==> \urldecode($value));
28-
return tuple($responder, new ImmMap($data));
28+
return tuple($responder, $data);
2929
} catch (NotFoundException $e) {
3030
$allowed = $this->getAllowedMethods($path);
3131
if (C\is_empty($allowed)) {
@@ -37,7 +37,7 @@ final public function routeMethodAndPath(
3737
) {
3838
list($responder, $data) = $resolver->resolve(HttpMethod::GET, $path);
3939
$data = Dict\map($data, $value ==> \urldecode($value));
40-
return tuple($responder, new ImmMap($data));
40+
return tuple($responder, $data);
4141
}
4242

4343
throw new MethodNotAllowedException($allowed);
@@ -46,7 +46,7 @@ final public function routeMethodAndPath(
4646

4747
final public function routeRequest(
4848
\Facebook\Experimental\Http\Message\RequestInterface $request,
49-
): (TResponder, ImmMap<string, string>) {
49+
): (TResponder, dict<string, string>) {
5050
$method = HttpMethod::coerce($request->getMethod());
5151
if ($method === null) {
5252
throw new MethodNotAllowedException(

src/uri-patterns/UriBuilderBase.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@
1010

1111
namespace Facebook\HackRouter;
1212

13+
use namespace HH\Lib\{C, Vec};
14+
1315
abstract class UriBuilderBase {
14-
protected ImmVector<UriPatternPart> $parts;
15-
protected ImmMap<string, RequestParameter> $parameters;
16-
private Map<string, string> $values = Map {};
16+
protected vec<UriPatternPart> $parts;
17+
protected dict<string, RequestParameter> $parameters;
18+
private dict<string, string> $values = dict[];
1719

1820
public function __construct(Traversable<UriPatternPart> $parts) {
19-
$this->parts = new ImmVector($parts);
20-
$parameters = Map {};
21+
$this->parts = vec($parts);
22+
$parameters = dict[];
2123
foreach ($parts as $part) {
2224
if (!$part is RequestParameter) {
2325
continue;
2426
}
2527
$parameters[$part->getName()] = $part;
2628
}
27-
$this->parameters = $parameters->immutable();
29+
$this->parameters = $parameters;
2830
}
2931

3032
final protected function getPathImpl(): string {
@@ -47,11 +49,11 @@ final protected function getPathImpl(): string {
4749

4850
$name = $part->getName();
4951
invariant(
50-
$this->values->containsKey($name),
52+
C\contains_key($this->values, $name),
5153
'Parameter "%s" must be set',
5254
$name,
5355
);
54-
$uri .= $this->values->at($name);
56+
$uri .= $this->values[$name];
5557
}
5658
invariant(
5759
\substr($uri, 0, 1) === '/',
@@ -71,7 +73,10 @@ classname<TypedUriParameter<T>> $parameter_type,
7173
$part !== null,
7274
'%s is not a valid parameter - expected one of [%s]',
7375
$name,
74-
\implode(', ', $this->parameters->keys()->map($x ==> "'".$x."'")),
76+
\implode(
77+
', ',
78+
Vec\map_with_key($this->parameters, ($key, $_) ==> "'".$key."'"),
79+
),
7580
);
7681
invariant(
7782
\is_a($part, $parameter_type),
@@ -81,7 +86,7 @@ classname<TypedUriParameter<T>> $parameter_type,
8186
\get_class($part),
8287
);
8388
invariant(
84-
!$this->values->containsKey($name),
89+
!C\contains_key($this->values, $name),
8590
'trying to set %s twice',
8691
$name,
8792
);

src/uri-patterns/UriPattern.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,38 @@
1010

1111
namespace Facebook\HackRouter;
1212

13+
use namespace HH\Lib\Vec;
14+
1315
// Non-final so you can extend it with additional convenience
1416
// methods.
1517
class UriPattern implements HasFastRouteFragment {
16-
private Vector<UriPatternPart> $parts = Vector {};
18+
private vec<UriPatternPart> $parts = vec[];
1719

1820
final public function appendPart(UriPatternPart $part): this {
1921
$this->parts[] = $part;
2022
return $this;
2123
}
2224

2325
final public function getFastRouteFragment(): string {
24-
$fragments = $this->parts->map($part ==> $part->getFastRouteFragment());
26+
$fragments = Vec\map($this->parts, $part ==> $part->getFastRouteFragment());
2527
return \implode('', $fragments);
2628
}
2729

28-
final public function getParts(): ImmVector<UriPatternPart> {
29-
return $this->parts->immutable();
30+
final public function getParts(): vec<UriPatternPart> {
31+
return $this->parts;
3032
}
3133

32-
final public function getParameters(): ImmVector<UriParameter> {
33-
return $this
34-
->parts
35-
->filter($x ==> $x is UriParameter)
36-
->map(
37-
$x ==> {
38-
assert($x is UriParameter);
39-
return $x;
40-
},
41-
)
42-
->immutable();
34+
final public function getParameters(): vec<UriParameter> {
35+
return Vec\map(
36+
Vec\filter($this->parts, $part ==> $part is UriParameter),
37+
$part ==> {
38+
invariant(
39+
$part is UriParameter,
40+
"Tell the typechecker what's going on",
41+
);
42+
return $part;
43+
},
44+
);
4345
}
4446

4547
///// Convenience Methods /////

tests/lib/TestRouter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function __construct(
2525

2626
<<__Override>>
2727
protected function getRoutes(
28-
): ImmMap<HttpMethod, ImmMap<string, T>> {
29-
return ImmMap {
30-
HttpMethod::GET => new ImmMap($this->routes),
31-
};
28+
): dict<HttpMethod, dict<string, T>> {
29+
return dict[
30+
HttpMethod::GET => $this->routes,
31+
];
3232
}
3333

3434
public function setResolver(IResolver<T> $resolver): this {

0 commit comments

Comments
 (0)