-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add support for optional shape fields & nested shapes * Simplify CodegenShapeMember * Don't use public class properties * Add support for CodegenShape_FUTURE * Remove old codegen * Use correct codegen file * Prefer vec * Don't accept old shape
- Loading branch information
1 parent
355d40b
commit db6d9c5
Showing
8 changed files
with
267 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HackCodegen; | ||
|
||
final class CodegenShapeMember { | ||
|
||
private bool $isOptional = false; | ||
|
||
public function __construct( | ||
private string $name, | ||
private mixed $type, | ||
) { | ||
invariant( | ||
is_string($type) || $type instanceof CodegenShape_FUTURE, | ||
"You must provide either a string or shape", | ||
); | ||
} | ||
|
||
public function setIsOptional(bool $value = true): this { | ||
$this->isOptional = $value; | ||
return $this; | ||
} | ||
|
||
public function getIsOptional(): bool { | ||
return $this->isOptional; | ||
} | ||
|
||
public function getName(): string { | ||
return $this->name; | ||
} | ||
|
||
public function getType(): string { | ||
if ($this->type instanceof ICodeBuilderRenderer) { | ||
return $this->type->render(); | ||
} | ||
|
||
invariant($this->type !== null, "Somehow type is null"); | ||
invariant(is_string($this->type), "Somehow type is not a string"); | ||
return $this->type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HackCodegen; | ||
|
||
/** | ||
* Generate code for a shape. Please don't use this class directly; instead use | ||
* the function codegenShape_FUTURE. E.g.: | ||
* | ||
* ``` | ||
* codegenShape_FUTURE( | ||
* new CodegenShapeMember('x', 'int'), | ||
* (new CodegenShapeMember('y', 'int'))->setIsOptional(), | ||
* ) | ||
* ``` | ||
* | ||
*/ | ||
final class CodegenShape_FUTURE implements ICodeBuilderRenderer { | ||
|
||
use HackBuilderRenderer; | ||
|
||
private ?string $manualAttrsID = null; | ||
|
||
public function __construct( | ||
protected IHackCodegenConfig $config, | ||
private vec<CodegenShapeMember> $members, | ||
) { | ||
} | ||
|
||
public function setManualAttrsID(?string $id = null): this { | ||
$this->manualAttrsID = $id; | ||
return $this; | ||
} | ||
|
||
public function appendToBuilder(HackBuilder $builder): HackBuilder { | ||
$builder->addLine('shape(')->indent(); | ||
|
||
foreach ($this->members as $member) { | ||
$prefix = $member->getIsOptional() ? '?' : ''; | ||
$builder->addLinef( | ||
"%s'%s' => %s,", | ||
$prefix, | ||
$member->getName(), | ||
$member->getType(), | ||
); | ||
} | ||
|
||
$manual_id = $this->manualAttrsID; | ||
if ($manual_id !== null) { | ||
$builder | ||
->ensureNewLine() | ||
->startManualSection($manual_id) | ||
->ensureEmptyLine() | ||
->endManualSection(); | ||
} | ||
|
||
return $builder->unindent()->add(')'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
@generated | ||
!@#$%codegentest:testMultipleNestedShapes | ||
shape( | ||
'test' => shape( | ||
?'point' => shape( | ||
'x' => int, | ||
'y' => int, | ||
), | ||
'url' => string, | ||
), | ||
?'key' => string, | ||
) | ||
!@#$%codegentest:testNestedShape | ||
shape( | ||
'url' => string, | ||
?'point' => shape( | ||
'x' => int, | ||
'y' => int, | ||
), | ||
) | ||
!@#$%codegentest:testNestedShapeLegacy | ||
shape( | ||
'url' => string, | ||
?'point' => shape( | ||
'x' => int, | ||
'y' => int, | ||
), | ||
) | ||
!@#$%codegentest:testShape | ||
shape( | ||
'x' => int, | ||
'y' => int, | ||
'url' => string, | ||
) | ||
!@#$%codegentest:testShapeOptionalFields | ||
shape( | ||
?'x' => int, | ||
?'y' => int, | ||
'url' => string, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?hh // strict | ||
/* | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace Facebook\HackCodegen; | ||
|
||
final class CodegenShapeFutureTest extends CodegenBaseTest { | ||
|
||
public function testShape(): void { | ||
$shape = $this | ||
->getCodegenFactory() | ||
->codegenShape_FUTURE( | ||
new CodegenShapeMember('x', 'int'), | ||
new CodegenShapeMember('y', 'int'), | ||
new CodegenShapeMember('url', 'string'), | ||
); | ||
|
||
$this->assertUnchanged($shape->render()); | ||
} | ||
|
||
public function testShapeOptionalFields(): void { | ||
$shape = $this | ||
->getCodegenFactory() | ||
->codegenShape_FUTURE( | ||
(new CodegenShapeMember('x', 'int'))->setIsOptional(), | ||
(new CodegenShapeMember('y', 'int'))->setIsOptional(), | ||
new CodegenShapeMember('url', 'string'), | ||
); | ||
|
||
$this->assertUnchanged($shape->render()); | ||
} | ||
|
||
public function testNestedShape(): void { | ||
$nested = $this | ||
->getCodegenFactory() | ||
->codegenShape_FUTURE( | ||
new CodegenShapeMember('x', 'int'), | ||
new CodegenShapeMember('y', 'int'), | ||
); | ||
|
||
$shape = $this | ||
->getCodegenFactory() | ||
->codegenShape_FUTURE( | ||
new CodegenShapeMember('url', 'string'), | ||
(new CodegenShapeMember('point', $nested))->setIsOptional(), | ||
); | ||
|
||
$this->assertUnchanged($shape->render()); | ||
} | ||
|
||
public function testMultipleNestedShapes(): void { | ||
$first = $this | ||
->getCodegenFactory() | ||
->codegenShape_FUTURE( | ||
new CodegenShapeMember('x', 'int'), | ||
new CodegenShapeMember('y', 'int'), | ||
); | ||
|
||
$second = $this | ||
->getCodegenFactory() | ||
->codegenShape_FUTURE( | ||
(new CodegenShapeMember('point', $first))->setIsOptional(), | ||
new CodegenShapeMember('url', 'string'), | ||
); | ||
|
||
$shape = $this | ||
->getCodegenFactory() | ||
->codegenShape_FUTURE( | ||
new CodegenShapeMember('test', $second), | ||
(new CodegenShapeMember('key', 'string'))->setIsOptional(), | ||
); | ||
|
||
$this->assertUnchanged($shape->render()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters