From 613d7ddfae5e93416f830cf9bdd440b4669462b2 Mon Sep 17 00:00:00 2001 From: Uziel Maximo Luduena Date: Fri, 3 Apr 2020 00:39:52 +0200 Subject: [PATCH] Changed Hack Collections with Hack Arrays --- README.md | 24 ++++++------ examples/BaseRouterExample.php | 36 +++++++----------- examples/UriPatternsExample.php | 37 ++++++++----------- .../RequestParametersBase.php | 4 +- src/router/BaseRouter.php | 8 ++-- src/uri-patterns/UriBuilderBase.php | 25 ++++++++----- src/uri-patterns/UriPattern.php | 32 ++++++++-------- tests/lib/TestRouter.php | 8 ++-- 8 files changed, 82 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index dbb2bca..c2fac2b 100644 --- a/README.md +++ b/README.md @@ -25,22 +25,20 @@ A simple typed request router. Example: * callable, but classname is also a * common choice. */ -type TResponder = (function(ImmMap):string); +type TResponder = (function(dict): string); final class BaseRouterExample extends BaseRouter { - protected function getRoutes( - ): ImmMap> { - return ImmMap { - HttpMethod::GET => ImmMap { - '/' => - ($_params) ==> 'Hello, world', - '/user/{user_name}' => - ($params) ==> 'Hello, '.$params['user_name'], - }, - HttpMethod::POST => ImmMap { + <<__Override>> + protected function getRoutes(): dict> { + return dict[ + HttpMethod::GET => dict[ + '/' => ($_params) ==> 'Hello, world', + '/user/{user_name}' => ($params) ==> 'Hello, '.$params['user_name'], + ], + HttpMethod::POST => dict[ '/' => ($_params) ==> 'Hello, POST world', - }, - }; + ], + ]; } } ``` diff --git a/examples/BaseRouterExample.php b/examples/BaseRouterExample.php index 2fd7aa4..239f2d5 100644 --- a/examples/BaseRouterExample.php +++ b/examples/BaseRouterExample.php @@ -22,33 +22,30 @@ * callable, but classname is also a * common choice. */ -type TResponder = (function(dict):string); +type TResponder = (function(dict): string); final class BaseRouterExample extends BaseRouter { <<__Override>> - protected function getRoutes( - ): ImmMap> { - return ImmMap { - HttpMethod::GET => ImmMap { - '/' => - ($_params) ==> 'Hello, world', - '/user/{user_name}' => - ($params) ==> 'Hello, '.$params['user_name'], - }, - HttpMethod::POST => ImmMap { + protected function getRoutes(): dict> { + return dict[ + HttpMethod::GET => dict[ + '/' => ($_params) ==> 'Hello, world', + '/user/{user_name}' => ($params) ==> 'Hello, '.$params['user_name'], + ], + HttpMethod::POST => dict[ '/' => ($_params) ==> 'Hello, POST world', - }, - }; + ], + ]; } } -function get_example_inputs(): ImmVector<(HttpMethod, string)> { - return ImmVector { +function get_example_inputs(): vec<(HttpMethod, string)> { + return vec[ tuple(HttpMethod::GET, '/'), tuple(HttpMethod::GET, '/user/foo'), tuple(HttpMethod::GET, '/user/bar'), tuple(HttpMethod::POST, '/'), - }; + ]; } <<__EntryPoint>> @@ -58,12 +55,7 @@ function main(): noreturn { list($method, $path) = $input; list($responder, $params) = $router->routeMethodAndPath($method, $path); - \printf( - "%s %s\n\t%s\n", - $method, - $path, - $responder(dict($params)), - ); + \printf("%s %s\n\t%s\n", $method, $path, $responder($params)); } exit(0); } diff --git a/examples/UriPatternsExample.php b/examples/UriPatternsExample.php index cb38985..82c5aa3 100644 --- a/examples/UriPatternsExample.php +++ b/examples/UriPatternsExample.php @@ -16,14 +16,14 @@ require_once(__DIR__.'/../vendor/hh_autoload.php'); -use type Facebook\HackRouter\{ +use type Facebook\HackRouter\{ BaseRouter, GetFastRoutePatternFromUriPattern, GetUriBuilderFromUriPattern, HasUriPattern, HttpMethod, RequestParameters, - UriPattern + UriPattern, }; <<__ConsistentConstruct>> @@ -38,12 +38,10 @@ final protected function getRequestParameters(): RequestParameters { return $this->uriParameters; } - public function __construct( - ImmMap $uri_parameter_values, - ) { + public function __construct(dict $uri_parameter_values) { $this->uriParameters = new RequestParameters( static::getUriPattern()->getParameters(), - ImmVector { }, + vec[], $uri_parameter_values, ); } @@ -78,34 +76,33 @@ public function getResponse(): string { type TResponder = classname; final class UriPatternsExample extends BaseRouter { - public static function getControllers(): ImmVector { - return ImmVector { + public static function getControllers(): vec { + return vec[ HomePageController::class, UserPageController::class, - }; + ]; } <<__Override>> - public function getRoutes( - ): ImmMap> { + public function getRoutes(): dict> { $urls_to_controllers = dict[]; foreach (self::getControllers() as $controller) { $pattern = $controller::getFastRoutePattern(); $urls_to_controllers[$pattern] = $controller; } - return ImmMap { - HttpMethod::GET => new ImmMap($urls_to_controllers), - }; + return dict [ + HttpMethod::GET => $urls_to_controllers, + ]; } } -function get_example_paths(): ImmVector { - return ImmVector { +function get_example_paths(): vec { + return vec[ HomePageController::getUriBuilder()->getPath(), UserPageController::getUriBuilder() ->setString('user_name', 'Mr Hankey') ->getPath(), - }; + ]; } function main(): void { @@ -115,11 +112,7 @@ function main(): void { HttpMethod::GET, $path, ); - \printf( - "GET %s\n\t%s\n", - $path, - (new $controller($params))->getResponse(), - ); + \printf("GET %s\n\t%s\n", $path, (new $controller($params))->getResponse()); } } diff --git a/src/request-parameters/RequestParametersBase.php b/src/request-parameters/RequestParametersBase.php index 01d894d..4d39035 100644 --- a/src/request-parameters/RequestParametersBase.php +++ b/src/request-parameters/RequestParametersBase.php @@ -16,14 +16,14 @@ abstract class RequestParametersBase { private dict $requiredSpecs; private dict $optionalSpecs; - protected ImmMap $values; + protected dict $values; public function __construct( Traversable $required_specs, Traversable $optional_specs, KeyedTraversable $values, ) { - $this->values = new ImmMap($values); + $this->values = dict($values); $spec_vector_to_map = ( Traversable $specs ) ==> Dict\pull($specs, $it ==> $it, $it ==> $it->getName()); diff --git a/src/router/BaseRouter.php b/src/router/BaseRouter.php index ea8e955..4b092c1 100644 --- a/src/router/BaseRouter.php +++ b/src/router/BaseRouter.php @@ -20,12 +20,12 @@ abstract protected function getRoutes( final public function routeMethodAndPath( HttpMethod $method, string $path, - ): (TResponder, ImmMap) { + ): (TResponder, dict) { $resolver = $this->getResolver(); try { list($responder, $data) = $resolver->resolve($method, $path); $data = Dict\map($data, $value ==> \urldecode($value)); - return tuple($responder, new ImmMap($data)); + return tuple($responder, $data); } catch (NotFoundException $e) { $allowed = $this->getAllowedMethods($path); if (C\is_empty($allowed)) { @@ -37,7 +37,7 @@ final public function routeMethodAndPath( ) { list($responder, $data) = $resolver->resolve(HttpMethod::GET, $path); $data = Dict\map($data, $value ==> \urldecode($value)); - return tuple($responder, new ImmMap($data)); + return tuple($responder, $data); } throw new MethodNotAllowedException($allowed); @@ -46,7 +46,7 @@ final public function routeMethodAndPath( final public function routeRequest( \Facebook\Experimental\Http\Message\RequestInterface $request, - ): (TResponder, ImmMap) { + ): (TResponder, dict) { $method = HttpMethod::coerce($request->getMethod()); if ($method === null) { throw new MethodNotAllowedException( diff --git a/src/uri-patterns/UriBuilderBase.php b/src/uri-patterns/UriBuilderBase.php index 928863c..758cbc7 100644 --- a/src/uri-patterns/UriBuilderBase.php +++ b/src/uri-patterns/UriBuilderBase.php @@ -10,21 +10,23 @@ namespace Facebook\HackRouter; +use namespace HH\Lib\{C, Vec}; + abstract class UriBuilderBase { - protected ImmVector $parts; - protected ImmMap $parameters; - private Map $values = Map {}; + protected vec $parts; + protected dict $parameters; + private dict $values = dict[]; public function __construct(Traversable $parts) { - $this->parts = new ImmVector($parts); - $parameters = Map {}; + $this->parts = vec($parts); + $parameters = dict[]; foreach ($parts as $part) { if (!$part is RequestParameter) { continue; } $parameters[$part->getName()] = $part; } - $this->parameters = $parameters->immutable(); + $this->parameters = $parameters; } final protected function getPathImpl(): string { @@ -47,11 +49,11 @@ final protected function getPathImpl(): string { $name = $part->getName(); invariant( - $this->values->containsKey($name), + C\contains_key($this->values, $name), 'Parameter "%s" must be set', $name, ); - $uri .= $this->values->at($name); + $uri .= $this->values[$name]; } invariant( \substr($uri, 0, 1) === '/', @@ -71,7 +73,10 @@ classname> $parameter_type, $part !== null, '%s is not a valid parameter - expected one of [%s]', $name, - \implode(', ', $this->parameters->keys()->map($x ==> "'".$x."'")), + \implode( + ', ', + Vec\map_with_key($this->parameters, ($key, $_) ==> "'".$key."'"), + ), ); invariant( \is_a($part, $parameter_type), @@ -81,7 +86,7 @@ classname> $parameter_type, \get_class($part), ); invariant( - !$this->values->containsKey($name), + !C\contains_key($this->values, $name), 'trying to set %s twice', $name, ); diff --git a/src/uri-patterns/UriPattern.php b/src/uri-patterns/UriPattern.php index 2a9ce04..33b881d 100644 --- a/src/uri-patterns/UriPattern.php +++ b/src/uri-patterns/UriPattern.php @@ -10,10 +10,12 @@ namespace Facebook\HackRouter; +use namespace HH\Lib\Vec; + // Non-final so you can extend it with additional convenience // methods. class UriPattern implements HasFastRouteFragment { - private Vector $parts = Vector {}; + private vec $parts = vec[]; final public function appendPart(UriPatternPart $part): this { $this->parts[] = $part; @@ -21,25 +23,25 @@ final public function appendPart(UriPatternPart $part): this { } final public function getFastRouteFragment(): string { - $fragments = $this->parts->map($part ==> $part->getFastRouteFragment()); + $fragments = Vec\map($this->parts, $part ==> $part->getFastRouteFragment()); return \implode('', $fragments); } - final public function getParts(): ImmVector { - return $this->parts->immutable(); + final public function getParts(): vec { + return $this->parts; } - final public function getParameters(): ImmVector { - return $this - ->parts - ->filter($x ==> $x is UriParameter) - ->map( - $x ==> { - assert($x is UriParameter); - return $x; - }, - ) - ->immutable(); + final public function getParameters(): vec { + return Vec\map( + Vec\filter($this->parts, $part ==> $part is UriParameter), + $part ==> { + invariant( + $part is UriParameter, + "Tell the typechecker what's going on", + ); + return $part; + }, + ); } ///// Convenience Methods ///// diff --git a/tests/lib/TestRouter.php b/tests/lib/TestRouter.php index 53cdbd0..17a77ea 100644 --- a/tests/lib/TestRouter.php +++ b/tests/lib/TestRouter.php @@ -25,10 +25,10 @@ public function __construct( <<__Override>> protected function getRoutes( - ): ImmMap> { - return ImmMap { - HttpMethod::GET => new ImmMap($this->routes), - }; + ): dict> { + return dict[ + HttpMethod::GET => $this->routes, + ]; } public function setResolver(IResolver $resolver): this {