Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
HHVM/Hack ShapeとTypeAssertで堅実なアプリケーション
yuuki takezawa
April 23, 2018
Programming
1
240
HHVM/Hack ShapeとTypeAssertで堅実なアプリケーション
HHVM/Hack勉強会Vol.1 slide
https://istyle.connpass.com/event/80398/
yuuki takezawa
April 23, 2018
Tweet
Share
More Decks by yuuki takezawa
See All by yuuki takezawa
ytake
4
920
ytake
0
5.4k
ytake
3
6.9k
ytake
2
2.6k
ytake
7
10k
ytake
3
270
ytake
1
1.7k
ytake
4
1.7k
ytake
1
3.3k
Other Decks in Programming
See All in Programming
mu2in
0
140
hr01
1
1.2k
kyoheig3
0
420
makicamel
1
180
77web
0
210
masayaaoyama
4
540
freekmurze
0
200
manfredsteyer
PRO
0
260
canon1ky
3
350
takaram
1
1.2k
cwozaki
1
1.8k
line_developers_tw
1
460
Featured
See All Featured
lara
16
2.6k
kastner
54
1.9k
hatefulcrawdad
257
17k
lemiorhan
626
42k
bkeepers
408
57k
matthewcrist
73
7.5k
gr2m
83
11k
dotmariusz
94
5.1k
danielanewman
1
470
morganepeng
92
14k
scottboms
251
11k
shpigford
165
19k
Transcript
shapeͱTypeAssertͰ ݎ࿚ͳΞϓϦέʔγϣϯ yuuki takezawa (ytake)
εϥΠυͰར༻͍ͯ͠Δαϯϓϧίʔυ https://github.com/istyle-inc/example-hack- strict-api
<?hh ෆ࣮֬ͳAPI
Ͳ͏ͬͯܕΛࢦఆ͢Δ͔ • Collection (Map, Vector etc) • Generics • shape
• tuple
[ ‘message’ => ‘hackstudy’, ‘date’ => ‘2018/04/20’ ];
ྻอূ͞ΕΔ͔ • ͍ΘΏΔPHPͷྻɺܕͷ੍͕ͳ͍ ࣗ༝ͳϋογϡςʔϒϧ • ྻͰ͋Εserialize -> json response •
messageʹint͕ೖͬͨΒʁ • date͕intͩͬͨΒʁ • Ωʔ͕૿͑ͨΓݮͬͨΓ • typo?
ෆ࣮֬Ͱ͔͠ͳ͍ʂ
<?hh shape
shape( 'message' => string, 'date' => string, );
<?hh // strict class Serialize { public function execute(shape( 'message'
=> string, 'date' => string, ) $shape): string { return serialize($shape); } }
$serialize = new \Serialize(); $serialize->execute(shape( 'message' => 'hackstudy', 'date' =>
'2018/04/20', ));
shapeΛͯ͠େৎ • ྻͱಉʹར༻͢Δ͜ͱ͕Ͱ͖ΔͷͰɺ ઃఆͳͲΛ͢ɺ֎෦Ϧιʔεʹର੍ͯ͠Λ͔͚Δ ͳͲʹ༻్ʹόονϦ • foreachshapeʹର࣮ͯ͠ߦͰ͖ͳ͍ • it doesn't
implement Traversable or Container. • []͑·ͤΜ • toArrayͰมͯ͠ྑ͍
foreach͍ͨ͠ΜͰ͚͢Ͳ
<?hh shape + Collection
type CollectableShape = shape( 'id' => int, 'name' => string,
);
public function executeCollection(shape( 'message' => string, 'date' => string, 'tables'
=> Vector<CollectableShape>, ) $shape): string { $v = Shapes::idx($shape, 'tables', Vector{ }); return serialize( array_merge( Shapes::toArray($shape), ['tables' => $v->toArray()] ) ); }
CollectionΛΈ߹Θͤͯଟछଟ༷ʹ • ྻʹมͨ͠Γɺ͘͠ྻΛදݱͨ͠Γ • ৭ʑͳ༻్Ͱ৭ʑ੍Λ͔͚Δ͜ͱ͕Ͱ͖Δ • Ͱshape
֎෦ϦιʔεshapeͰͳ͍
function nullthrows<T>( ?T $data, string $message = 'unexpected null’ ):
T { invariant( $data !== null, $message, ); return $data; }
APIͷΓͷνΣοΫʁ • ྻʹมͨ͋͠ͱʹgenerics+invariant͢Δʁ • ҰͭҰͭεΩϟϯʁ
hhvm/type-assert
ෆ࣮֬ͳΛΞαʔτʂ • primitiveܕͪΖΜɺmapͳͲʹ • is_aͳͲநԽ • Ұ൪ͷۄ matches_type_structure
<?hh // strict use namespace Facebook\TypeAssert; class Foo { const
type TAPIResponse = shape( 'id' => int, 'user' => string, 'data' => shape(/* ... */), ); public static function getAPIResponse(): self::TAPIResponse { $json_string = file_get_contents('https://api.example.com'); $array = json_decode($json_string, /* associative = */ true); return TypeAssert\matches_type_structure( type_structure(self::class, 'TAPIResponse'), $array, ); } }
matches_type_structure • ReflectionTypeAlias ΫϥεͷgetTypeStructureϝιουྻΛ ฦͨ͢Ίਏ͍(HackͳΒجຊstirctͰॻ͘͜ͱ͕ଟ͍) https://github.com/facebook/hhvm/blob/master/hphp/hack/hhi/ reflection.hhi#L308 • ܕ௨ΓͩͬͨΒͦͷ··௨աͯ͠ཉ͍͠ͷͰɺ Genericsʹͯ͠΄͍͠ʂ
type_structure ؔར༻ͷΈରԠ https://github.com/facebook/hhvm/blob/master/hphp/hack/hhi/ typestructure.hhi#L94
matches_type_structure • ݕࠪ͢Δܕconst stringͰ͋Δඞཁ͕͋ΔͷͰɺ ಈతʹੜͰ͖ͳ͍ • shapeͰͻͨ͢Βॻ͔͘͠ͳ͍
json decodeʹؔͯ͠ • ίʔϧͨ͠APIͷΓͳͲʹ͏͜ͱ͕Ͱ͖Δ • JSONʹؔͯ͠json_decodeͰ ͍͔ͭ͘Hack͚Φϓγϣϯ͕͋Γ • JSON_FB_COLLECTIONS =>
Map JSON_FB_HACK_ARRAYS => dict https://github.com/facebook/hhvm/blob/master/hphp/ hack/hhi/stdlib/builtins_json.hhi#L41
ΞϓϦέʔγϣϯ͕ฦ٫͢Δͷʹ ద༻ ݎ࿚ͳϨεϙϯεΛఏڙ͢Δ
ΞϓϦέʔγϣϯͰ࣮༻͢Δʹ • HackͳΒϚΠΫϩαʔϏεʹ͓͚ΔAPI Gatewayͱ͔ • ฦ٫͢ΔΛ୲อͯ͋͛͠ΔΑ͏ʹͨ͠΄͏͕ྑ͍ • ϞμϯͳͷͰ͋ΕϛυϧΣΞΛ༻ҙͯ͠ɺ ࣗΒͷϨεϙϯεΛௐΔ
ΦϨΦϨHackϑϨʔϜϫʔΫͰ ΈࠐΜͰΈͨ https://github.com/ytake/nazg-skeleton
࣮ફ • ΞΠελΠϧͰHAL - Hypertext Application Language Λ࠾༻͍ͯ͠Δ • ߏΛshapeͰදݱ͢Δ
• https://techblog.istyle.co.jp/2018/04/18/ hack%E3%81%A7%E4%BD%9C%E3%82%8B%E5%A0 %85%E5%AE%9F%E3%81%AAapi/
// goal { "id":1234, "name":"ytake", "title":"type-assert for api response", "_links":{
"self":{ “href":"/", "type":"application/json" } }, "_embedded":{ "enviroments":[ { "name":"HHVM/Hack", "_links":{ "self":{ "href":"https://docs.hhvm.com/" } } } ] } }
// shapeͰhal+jsonΛදݱ͢Δ const type embeddedLinks = shape( 'name' => string,
'_links' => shape( 'self' => shape( 'href' => string ) ) ); const type hateoasStructure = shape( 'id' => int, 'name' => string, 'title' => string, '_links' => shape( 'self' => shape( 'href' => string, 'type' => string ) ), '_embedded' => shape( 'enviroments' => array<self::embeddedLinks> ) );
// ར༻ྫɹ͜͜ͰImmMap new ImmMap([ 'id' => 1234, 'name' => 'ytake',
'title' => 'type-assert for api response', 'embedded' => [ [ 'name' => 'HHVM/Hack', 'url' => 'https://docs.hhvm.com/' ], ] ]);
࣮ફ • ImmMapΛड͚औΓhal+jsonʹม • HalରԠϥΠϒϥϦPHPͰ͍͔ͭ͋͘Δ HackPHP͔ΒΕ͍ͯͬͯΔͨΊɺ PHPϥΠϒϥϦ͏ཧ༝ͳ͍ͷͰࣗͰ࡞Δ(ͨ) • https://github.com/ytake/hhhal
protected Vector<HalResource> $vec = Vector{}; public function __construct( protected ImmMap<mixed,
mixed> $resource ) {}
$map = $this->resource ->filterWithKey(($k, $v) ==> $k != 'embedded') ->toMap();
$hal = new HalResource($map); $hal->withLink(new Link( 'self', new Vector([ new LinkResource( '/', shape('type' => 'application/json')) ]), )); // hal+jsonରԠϥΠϒϥϦ࣮ྫ (Hack)
$embedded = $this->resource->get('embedded'); if(is_array($embedded)) { foreach($embedded as $row) { $embeddedResource
= new HalResource(); foreach($row as $key => $value) { if($key === 'url') { $embeddedResource->withLink( new Link('self', new Vector([new LinkResource($value)])) ); continue; } $embeddedResource->addResource(strval($key), $value); } $this->vec->add($embeddedResource); } }
// γϦΞϥΠζ͢Δͱhal+jsonରԠͷjson͕ग़ྗ͞Ε·͢(Hack) $hal = $hal->withEmbedded('enviroments', $this->vec); $serialize = new Serializer(new
JsonSerializer(), $hal); return $serialize->toArray();
࣮ફ • ϛυϧΣΞͷ࠷ޙʹ࣮ߦ͢ΔΑ͏ʹ͢Δ • PSR-15४ڌͷϛυϧΣΞͰ͋ΕͳΜͰ
public function process( ServerRequestInterface $request, RequestHandlerInterface $handler, ): ResponseInterface {
$response = $handler->handle($request); // ϛυϧΣΞͷޙͰಈ͘ $decode = json_decode($response->getBody()->getContents(), true); TypeAssert\matches_type_structure( type_structure(self::class, 'hateoasStructure'), $decode, ); return $response; }
<?hh // stirct use Psr\Log\LoggerInterface; use Ytake\HHContainer\Scope; use Ytake\HHContainer\ServiceModule; use
Ytake\HHContainer\FactoryContainer; use App\Middleware\ResponseAssertMiddleware; final class MiddlewareServiceModule extends ServiceModule { public function provide(FactoryContainer $container): void { $container->set( ResponseAssertMiddleware::class, $container ==> new ResponseAssertMiddleware() ); // containerͷόΠϯυྫ } }
// routeshapeͰෆ࣮֬͞Λͳ͘͢ʂ \Nazg\Http\HttpMethod::GET => ImmMap { '/' => shape( 'middleware'
=> ImmVector { App\Middleware\ResponseAssertMiddleware::class, App\Action\IndexAction::class, }, ) },
·ͱΊ • strictҎ֎ͭΒ͍ • mixedͭΒ͍ • ϚΠΫϩαʔϏεͰೖ͢ΔͳΒtype-assertΛ͓קΊ • inputͱoutput ৴༻͍͚͗ͯ͢͠ͳ͍
• null͕ʂܕ͕ʂͱݴΘΕͨΒHackͰఏڙͯ͠Έ·͠ΐ͏