-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for generating hack enums from json def #81
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e3ae0a3
wip
scobb cd54e99
wip
scobb 0010d7c
no need to double add hack constraint
scobb b19a901
invariant. skip reflection.
scobb fcd58cf
remove leading slashes.
scobb a8602e6
tests.
scobb 63ea141
revert unneeded change.
scobb 6ec0431
hackfmt
scobb 4691ad1
invariant for slash
scobb a06beb8
tabs -> 2 spaces.
scobb 4415f14
typo in json
scobb 10c04d8
darray -> dict
scobb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm it’s a bummer that we have to fork here. I bet we could update the pre-existing branch to drop the leading
\
without causing issues, though it would require regenerating a lot of code.