-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generating hack enums from json def (#81)
* wip * wip * no need to double add hack constraint * invariant. skip reflection. * remove leading slashes. * tests. * revert unneeded change. * hackfmt * invariant for slash * tabs -> 2 spaces. * typo in json * darray -> dict
- Loading branch information
Showing
5 changed files
with
202 additions
and
40 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
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,35 @@ | ||
<?hh // strict | ||
|
||
namespace Slack\Hack\JsonSchema\Tests; | ||
|
||
|
||
use type Slack\Hack\JsonSchema\Tests\Generated\GeneratedHackEnumSchemaValidator; | ||
|
||
final class GeneratedHackEnumSchemaValidatorTest extends BaseCodegenTestCase { | ||
|
||
<<__Override>> | ||
public static async function beforeFirstTestAsync(): Awaitable<void> { | ||
$ret = self::getBuilder('generated-hack-enum-schema.json', 'GeneratedHackEnumSchemaValidator'); | ||
$ret['codegen']->build(); | ||
require_once($ret['path']); | ||
} | ||
public function testStringEnum(): void { | ||
$cases = vec[ | ||
shape( | ||
'input' => dict['enum_string' => 'one'], | ||
'output' => dict['enum_string' => 'one'], | ||
'valid' => true, | ||
), | ||
shape( | ||
'input' => dict['enum_string' => 'four'], | ||
'valid' => false, | ||
), | ||
shape( | ||
'input' => dict['enum_string' => 1], | ||
'valid' => false, | ||
), | ||
]; | ||
|
||
$this->expectCases($cases, $input ==> new GeneratedHackEnumSchemaValidator($input)); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
tests/examples/codegen/GeneratedHackEnumSchemaValidator.php
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,85 @@ | ||
<?hh // strict | ||
/** | ||
* This file is generated. Do not modify it manually! | ||
* | ||
* To re-generate this file run `make test` | ||
* | ||
* | ||
* @generated SignedSource<<ff65e010c6ed29ad1f95451eac4f8a17>> | ||
*/ | ||
namespace Slack\Hack\JsonSchema\Tests\Generated; | ||
use namespace Slack\Hack\JsonSchema; | ||
use namespace Slack\Hack\JsonSchema\Constraints; | ||
|
||
type TGeneratedHackEnumSchemaValidator = shape( | ||
?'enum_string' => myCoolTestEnum, | ||
... | ||
); | ||
|
||
|
||
enum myCoolTestEnum : string as string { | ||
ONE = 'one'; | ||
TWO = 'two'; | ||
THREE = 'three'; | ||
} | ||
|
||
final class GeneratedHackEnumSchemaValidatorPropertiesEnumString { | ||
|
||
private static bool $coerce = false; | ||
|
||
public static function check(mixed $input, string $pointer): myCoolTestEnum { | ||
$typed = Constraints\StringConstraint::check($input, $pointer, self::$coerce); | ||
|
||
$typed = Constraints\HackEnumConstraint::check( | ||
$typed, | ||
myCoolTestEnum::class, | ||
$pointer, | ||
); | ||
return $typed; | ||
} | ||
} | ||
|
||
final class GeneratedHackEnumSchemaValidator | ||
extends JsonSchema\BaseValidator<TGeneratedHackEnumSchemaValidator> { | ||
|
||
private static bool $coerce = false; | ||
|
||
public static function check( | ||
mixed $input, | ||
string $pointer, | ||
): TGeneratedHackEnumSchemaValidator { | ||
$typed = Constraints\ObjectConstraint::check($input, $pointer, self::$coerce); | ||
|
||
$errors = vec[]; | ||
$output = shape(); | ||
|
||
/*HHAST_IGNORE_ERROR[UnusedVariable] Some loops generated with this statement do not use their $value*/ | ||
foreach ($typed as $key => $value) { | ||
/* HH_IGNORE_ERROR[4051] allow dynamic access to preserve input. See comment in the codegen lib for reasoning and alternatives if needed. */ | ||
$output[$key] = $value; | ||
} | ||
|
||
if (\HH\Lib\C\contains_key($typed, 'enum_string')) { | ||
try { | ||
$output['enum_string'] = GeneratedHackEnumSchemaValidatorPropertiesEnumString::check( | ||
$typed['enum_string'], | ||
JsonSchema\get_pointer($pointer, 'enum_string'), | ||
); | ||
} catch (JsonSchema\InvalidFieldException $e) { | ||
$errors = \HH\Lib\Vec\concat($errors, $e->errors); | ||
} | ||
} | ||
|
||
if (\HH\Lib\C\count($errors)) { | ||
throw new JsonSchema\InvalidFieldException($pointer, $errors); | ||
} | ||
|
||
/* HH_IGNORE_ERROR[4163] */ | ||
return $output; | ||
} | ||
|
||
<<__Override>> | ||
protected function process(): TGeneratedHackEnumSchemaValidator { | ||
return self::check($this->input, $this->pointer); | ||
} | ||
} |
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,11 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"enum_string": { | ||
"type": "string", | ||
"enum": ["one", "two", "three"], | ||
"generateHackEnum": true, | ||
"hackEnum": "myCoolTestEnum" | ||
} | ||
} | ||
} |