Skip to content

Commit 83945fd

Browse files
committed
Document built-in key and value renderers
1 parent f09f5fc commit 83945fd

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

docs/_data/nav_docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- title: Hack Builder
88
items:
99
- id: hack-builder-blocks
10+
- id: hack-builder-keys-and-values
1011

1112
# n title:, 1 items: per title:, n id: per items:
1213
# - title: A
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
docid: hack-builder-keys-and-values
3+
title: Keys & Values
4+
layout: docs
5+
permalink: /docs/hack-builder/keys-and-values/
6+
---
7+
8+
Hack Codegen has an extensible system for rendering values, used by:
9+
10+
- `$hack_builder->addValue()`
11+
- `$hack_builder->addAssignment()`
12+
- `$hack_builder->addReturn()`
13+
- `$hack_builder->addReturnCase()`
14+
15+
This is based on the `IHackBuilderValueRenderer<T>` interface, which provides
16+
`public function render(IHackCodegenConfig $config, T $input): string` which
17+
returns PHP code.
18+
19+
Basic Built-ins
20+
---------------
21+
22+
Hack Codegen provides several implementation of this interface, accessed via
23+
the [`HackBuilderValues`](https://github.com/hhvm/hack-codegen/blob/master/src/key-value-render/HackBuilderValues.php)
24+
class:
25+
26+
- `HackBuilderValues::literal()`: returns the $input as literal code
27+
- `HackBuilderValues::export()`: returns code that recreates `$input`; this is
28+
similar to `var_export()`
29+
- `HackBuilderValues::classname()`: renders the input as a `classname<T>`
30+
31+
For example:
32+
33+
``` php
34+
<?hh
35+
$builder->addValue(
36+
'foo',
37+
HackBuilderValues::export(),
38+
);
39+
```
40+
41+
Collection Built-Ins
42+
--------------------
43+
44+
As collections can be nested, the renderers are nested; the built-in value container
45+
renderers are:
46+
47+
- `HackBuilderValues::valueArray($value_renderer)`
48+
- `HackBuilderValues::vector($value_renderer)`
49+
- `HackBuilderValues::immVector($value_renderer)`
50+
- `HackBuilderValues::set($value_renderer)`
51+
- `HackBuilderValues::immSet($value_renderer)`
52+
53+
For example:
54+
``` php
55+
<?hh
56+
57+
$builder
58+
->addValue(
59+
Vector { 'foo' },
60+
HackBuilderValues::vector(
61+
HackBuilderValues::export(),
62+
),
63+
)
64+
->addValue(
65+
Vector { Vector { 'foo ' } },
66+
HackBuilderValues::vector(
67+
HackBuilderValues::vector(
68+
HackBuilderValues::export(),
69+
)
70+
),
71+
);
72+
```
73+
74+
`IHackBuilderKeyRenderer<T>` is similar to `IHackBuilderValueRenderer<T>`, however
75+
it requires that `T` is an `arraykey` (`string` or `int`); this is used by `Map`,
76+
`Set`, key-value arrays, and so on - key renderers are required to implement this
77+
interface.
78+
79+
The built-in key-value container renderers are:
80+
81+
- `HackBuilderValues::keyValueArray($key_renderer, $value_renderer)`
82+
- `HackBuilderValues::map($key_renderer, $value_renderer)`
83+
- `HackBuilderValues::immMap($key_renderer, $value_renderer)`

0 commit comments

Comments
 (0)