Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
HHVM/Hack ShapeとTypeAssertで堅実なアプリケーション
Search
yuuki takezawa
April 23, 2018
Programming
710
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
HHVM/Hack ShapeとTypeAssertで堅実なアプリケーション
HHVM/Hack勉強会Vol.1 slide
https://istyle.connpass.com/event/80398/
yuuki takezawa
April 23, 2018
More Decks by yuuki takezawa
See All by yuuki takezawa
なぜAI時代に 「イベント」を中心に考えるのか? / Why focus on "events" in the age of AI?
ytake
4
2.1k
PHPでアクターモデルを活用したSagaパターンの実践法 / php-saga-pattern-with-actor-model
ytake
0
2.5k
PHP ステートレス VS ステートフル 状態管理と並行性 / php-stateless-stateful
ytake
0
320
PHPでアクターモデルを理解・体験しよう / Understand and experience the actor model in PHP
ytake
2
930
再考 アクターモデル/ reconsider actor model
ytake
0
1.6k
GoとアクターモデルでES+CQRSを実践! / proto_actor_es_cqrs
ytake
1
660
Phluxorでアクターモデルを 理解・体験しよう / toolkit-for-flexible-actor-models-in-php-phluxor
ytake
1
380
オブジェクトのおしゃべり大失敗 メッセージングアンチパターン集 / messaging anti-pattern collection
ytake
2
1.3k
DRE/SREのプラクティス融合によるクラウドネイティブなデータ基盤作り / dre_sre
ytake
0
1k
Other Decks in Programming
See All in Programming
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
2
660
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
160
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
220
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
330
AI時代のUIはどこへ行く?その2!
yusukebe
21
7k
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
110
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
1
450
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
20
6.5k
Modding RubyKaigi for Myself
yui_knk
0
920
さぁV100、メモリをお食べ・・・
nilpe
0
140
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
260
Featured
See All Featured
Believing is Seeing
oripsolob
1
140
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
We Have a Design System, Now What?
morganepeng
55
8.2k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
160
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
First, design no harm
axbom
PRO
2
1.2k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
200
HDC tutorial
michielstock
2
700
Producing Creativity
orderedlist
PRO
348
40k
Amusing Abliteration
ianozsvald
1
200
Abbi's Birthday
coloredviolet
2
8k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.5k
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Ͱఏڙͯ͠Έ·͠ΐ͏