Skip to content

Commit 4525842

Browse files
committed
Add HackBuilderKeys::lambda() and HackBuilderValues::lambda()
More convenient than subclassing for one-offs. fixes #31
1 parent e540fe6 commit 4525842

10 files changed

+101
-11
lines changed

src/key-value-render/HackBuilderClassnameRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Facebook\HackCodegen;
1313

14-
final class HackBuilderClassnameRender
14+
final class HackBuilderClassnameRenderer
1515
implements IHackBuilderKeyRenderer<string>, IHackBuilderValueRenderer<string> {
1616
final public function render(
1717
IHackCodegenConfig $_,

src/key-value-render/HackBuilderKeyExportRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Facebook\HackCodegen;
1313

14-
final class HackBuilderKeyExportRender
14+
final class HackBuilderKeyExportRenderer
1515
implements IHackBuilderKeyRenderer<arraykey> {
1616
final public function render(
1717
IHackCodegenConfig $_,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?hh // strict
2+
/*
3+
* Copyright (c) 2015, Facebook, Inc.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the BSD-style license found in the
7+
* LICENSE file in the root directory of this source tree. An additional grant
8+
* of patent rights can be found in the PATENTS file in the same directory.
9+
*
10+
*/
11+
12+
namespace Facebook\HackCodegen;
13+
14+
final class HackBuilderKeyLambdaRenderer<T as arraykey>
15+
implements IHackBuilderKeyRenderer<T> {
16+
public function __construct(
17+
private (function(IHackCodegenConfig, T):string) $callback,
18+
) {
19+
}
20+
21+
final public function render(
22+
IHackCodegenConfig $config,
23+
T $value,
24+
): string {
25+
$callback = $this->callback;
26+
return $callback($config, $value);
27+
}
28+
}

src/HackBuilderKeys.php renamed to src/key-value-render/HackBuilderKeys.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@
1515
* passing a constant such as MyEnum::Value
1616
*/
1717
public static function literal(): IHackBuilderKeyRenderer<string> {
18-
return new HackBuilderLiteralRender();
18+
return new HackBuilderLiteralRenderer();
1919
}
2020

2121
/* The key will be exported to be rendered according the type. E.g. an int
2222
* will be rendered without changes but a string will be rendered with quotes.
2323
*/
2424
public static function export(): IHackBuilderKeyRenderer<arraykey> {
25-
return new HackBuilderKeyExportRender();
25+
return new HackBuilderKeyExportRenderer();
2626
}
2727

28-
/* The key will be renderered as a classname<T> */
28+
/** The key will be renderered as a classname<T> */
2929
public static function classname(): IHackBuilderKeyRenderer<string> {
30-
return new HackBuilderClassnameRender();
30+
return new HackBuilderClassnameRenderer();
31+
}
32+
33+
/**
34+
* The key will be rendered with the specified lambda
35+
*/
36+
public static function lambda<T as arraykey>(
37+
(function(IHackCodegenConfig, T): string) $render,
38+
): IHackBuilderKeyRenderer<T> {
39+
return new HackBuilderKeyLambdaRenderer($render);
3140
}
3241
}

src/key-value-render/HackBuilderLiteralRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Facebook\HackCodegen;
1313

14-
final class HackBuilderLiteralRender
14+
final class HackBuilderLiteralRenderer
1515
implements IHackBuilderKeyRenderer<string>, IHackBuilderValueRenderer<string> {
1616
final public function render(
1717
IHackCodegenConfig $_,

src/key-value-render/HackBuilderValueExportRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Facebook\HackCodegen;
1313

14-
final class HackBuilderValueExportRender
14+
final class HackBuilderValueExportRenderer
1515
implements IHackBuilderValueRenderer<mixed> {
1616
final public function render(
1717
IHackCodegenConfig $_,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?hh // strict
2+
/*
3+
* Copyright (c) 2015, Facebook, Inc.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the BSD-style license found in the
7+
* LICENSE file in the root directory of this source tree. An additional grant
8+
* of patent rights can be found in the PATENTS file in the same directory.
9+
*
10+
*/
11+
12+
namespace Facebook\HackCodegen;
13+
14+
final class HackBuilderValueLambdaRenderer<T>
15+
implements IHackBuilderValueRenderer<T> {
16+
public function __construct(
17+
private (function(IHackCodegenConfig, T):string) $callback,
18+
) {
19+
}
20+
21+
final public function render(
22+
IHackCodegenConfig $config,
23+
T $value,
24+
): string {
25+
$callback = $this->callback;
26+
return $callback($config, $value);
27+
}
28+
}

src/HackBuilderValues.php renamed to src/key-value-render/HackBuilderValues.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
// The value will be used literally, which is useful for example when
1515
// passing a constant such as MyEnum::Value
1616
public static function literal(): IHackBuilderValueRenderer<string> {
17-
return new HackBuilderLiteralRender();
17+
return new HackBuilderLiteralRenderer();
1818
}
1919

2020
// The value will be exported to be rendered according the type. E.g. an int
2121
// will be rendered without changes but a string will be rendered with quotes.
2222
public static function export(): IHackBuilderValueRenderer<mixed> {
23-
return new HackBuilderValueExportRender();
23+
return new HackBuilderValueExportRenderer();
2424
}
2525

2626
public static function valueArray<Tv>(
@@ -95,6 +95,15 @@ public static function shapeWithPerKeyRendering(
9595

9696
/* The key will be renderered as a classname<T> */
9797
public static function classname(): IHackBuilderValueRenderer<string> {
98-
return new HackBuilderClassnameRender();
98+
return new HackBuilderClassnameRenderer();
99+
}
100+
101+
/**
102+
* The value will be rendered with the specified lambda
103+
*/
104+
public static function lambda<T>(
105+
(function(IHackCodegenConfig, T): string) $render,
106+
): IHackBuilderValueRenderer<T> {
107+
return new HackBuilderValueLambdaRenderer($render);
99108
}
100109
}

test/HackBuilderTest.codegen

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ if ($value <= 0) {
6363
return 2;
6464
}
6565

66+
!@#$%codegentest:testLambdaMap
67+
Map {
68+
'key:foo' => 'value:bar',
69+
}
6670
!@#$%codegentest:testLiteralMap
6771
Map {
6872
MY_ENUM::A => ANOTHER_ENUM::A,

test/HackBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,18 @@ public function testClassnameMap(): void {
377377
);
378378
$this->assertUnchanged($body->getCode());
379379
}
380+
381+
public function testLambdaMap(): void {
382+
$body = $this->getHackBuilder()
383+
->addValue(
384+
Map { 'foo' => 'bar' },
385+
HackBuilderValues::map(
386+
HackBuilderKeys::lambda(($_config, $v) ==> "'key:$v'"),
387+
HackBuilderValues::lambda(($_config, $v) ==> "'value:$v'",)
388+
),
389+
);
390+
$this->assertUnchanged($body->getCode());
391+
}
380392
}
381393

382394
final class TestAnotherCodegenConfig implements IHackCodegenConfig {

0 commit comments

Comments
 (0)