Skip to content

Commit 08c4d3b

Browse files
author
Michael Hahn
authored
Merge branch 'master' into make-examples
2 parents 6cf4378 + ff6d9ea commit 08c4d3b

13 files changed

+49
-45
lines changed

src/Codegen/Constraints/Factory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Slack\Hack\JsonSchema\Codegen;
44

5+
use namespace HH\Lib\Str;
6+
57
trait Factory {
68
protected Context $ctx;
79

@@ -13,7 +15,7 @@ protected function generateClassName(string ...$parts): string {
1315
protected function generateTypeName(string $input): string {
1416
$config = $this->ctx->getJsonSchemaCodegenConfig();
1517
$processed = $config->getTypeNameFormatFunction()($input);
16-
return \HH\Lib\Str\format(
18+
return Str\format(
1719
'%s%s%s',
1820
$config->getTypeNamePrefix(),
1921
$processed,

src/Codegen/Constraints/ObjectBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ private function codegenType(
529529
$members = vec[];
530530
foreach ($property_classes as $property => $builder) {
531531
$member = new CodegenShapeMember($property, $builder->getType());
532-
if (!\HH\Lib\C\contains($required, $property)) {
532+
if (!C\contains($required, $property)) {
533533
$member->setIsOptional();
534534
}
535535

src/Codegen/Constraints/RefResolver.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Slack\Hack\JsonSchema\Codegen;
44

5-
use namespace HH\Lib\{C, Str};
5+
use namespace HH\Lib\{C, Str, Vec};
66

77
use namespace Slack\Hack\JsonSchema;
88

@@ -57,12 +57,12 @@ protected function resolveRef(string $ref, Context $ctx): (TSchema, ?string) {
5757

5858
// Find the schema the `ref` is pointing to
5959

60-
$ref = \substr($ref, 1);
61-
$pointers = \HH\Lib\Str\split($ref, '/') |> \HH\Lib\Vec\filter($$);
60+
$ref = Str\slice($ref, 1);
61+
$pointers = Str\split($ref, '/') |> Vec\filter($$, $str ==> $str !== '');
6262

6363
while (C\count($pointers)) {
6464
$pointer = $pointers[0];
65-
$pointers = \HH\Lib\Vec\drop($pointers, 1);
65+
$pointers = Vec\drop($pointers, 1);
6666
$array_schema = Shapes::toArray($current_schema);
6767
$next_schema = $array_schema[$pointer] ?? null;
6868

@@ -97,7 +97,7 @@ protected function getRefFilePath(string $ref): string {
9797
*/
9898
protected function getRefSchemaPath(string $ref): string {
9999
if ($ref[0] === '#') {
100-
return \substr($ref, 1);
100+
return Str\slice($ref, 1);
101101
}
102102

103103
$paths = $this->splitRefPaths($ref);
@@ -112,9 +112,9 @@ protected function getRefSchemaPath(string $ref): string {
112112
* { "$ref": "../../common/defs.json#/devices/tablet" }
113113
*/
114114
protected function splitRefPaths(string $ref): vec<string> {
115-
$paths = \HH\Lib\Vec\filter(\HH\Lib\Str\split($ref, '#'));
115+
$paths = Vec\filter(Str\split($ref, '#'), $str ==> $str !== '');
116116
if ($paths[0][0] === "/") {
117-
$paths[0] = \substr($paths[0], 1);
117+
$paths[0] = Str\slice($paths[0], 1);
118118
}
119119

120120
return $paths;

src/Codegen/Constraints/UntypedBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,12 @@ private function generateGenericAnyOfChecks(vec<SchemaBuilder> $schema_builders,
343343
// schemas. This lets us adjust the current type to be nullable
344344
// (`?<whatever>`)
345345
$non_null_schema_builder = null;
346-
if (\count($schema_builders) === 1) {
346+
if (C\count($schema_builders) === 1) {
347347
$this->current_type = $schema_builders[0]->getType();
348-
} else if (\count($schema_builders) === 2) {
348+
} else if (C\count($schema_builders) === 2) {
349349
$without_null = Vec\filter($schema_builders, $sb ==> !($sb->getBuilder() is NullBuilder));
350350

351-
if (\count($without_null) === 1) {
351+
if (C\count($without_null) === 1) {
352352
$non_null_schema_builder = $without_null[0];
353353

354354
$constraints = vec["class_meth({$non_null_schema_builder

src/Codegen/Utils.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Slack\Hack\JsonSchema\Codegen;
44

5+
use namespace HH\Lib\{Str, Vec};
56
use namespace Facebook\TypeAssert;
67

78
<<__Memoize>>
@@ -19,17 +20,15 @@ function type_assert_shape<T>(mixed $var, string $shape): T {
1920

2021
function sanitize(string $input): string {
2122
return $input
22-
|> \str_replace('_', ' ', $$)
23-
|> \str_replace('-', ' ', $$)
24-
|> \str_replace('.', ' ', $$)
23+
|> Str\replace_every($$, dict['_' => ' ', '-' => ' ', '.' => ' '])
2524
|> \preg_replace("/[^A-Za-z0-9 ]/", '_nan_', $$)
26-
|> \ucwords($$)
27-
|> \str_replace(' ', '', $$);
25+
|> Str\capitalize_words($$)
26+
|> Str\replace($$, ' ', '');
2827
}
2928

3029
function format(string ...$parts): string {
31-
return \HH\Lib\Vec\map($parts, fun('\Slack\Hack\JsonSchema\Codegen\sanitize'))
32-
|> \HH\Lib\Str\join($$, '');
30+
return Vec\map($parts, fun('\Slack\Hack\JsonSchema\Codegen\sanitize'))
31+
|> Str\join($$, '');
3332
}
3433

3534
/**

src/Constraints/EnumConstraint.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Slack\Hack\JsonSchema\Constraints;
44

5+
use namespace HH\Lib\C;
56
use namespace Slack\Hack\JsonSchema;
67

78
class EnumConstraint {
@@ -10,7 +11,7 @@ public static function check(
1011
vec<mixed> $enum,
1112
string $pointer,
1213
): void {
13-
if (!\HH\Lib\C\contains($enum, $input)) {
14+
if (!C\contains($enum, $input)) {
1415
$error = shape(
1516
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,
1617
'constraint' => shape(

src/Exceptions.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Slack\Hack\JsonSchema;
44

5+
use namespace HH\Lib\{Str, Vec};
6+
57
enum FieldErrorCode: string {
68
MISSING_FIELD = 'missing_field';
79
INVALID_TYPE = 'invalid_type';
@@ -52,7 +54,7 @@ public function __construct(
5254
// NB: If we haven't set `pointer` for the errors, set it now. If it is already
5355
// set it means we're bubbling the errors up and don't want to override the
5456
// more specific pointer for nested items.
55-
$errors = \HH\Lib\Vec\map(
57+
$errors = Vec\map(
5658
$errors,
5759
$error ==> {
5860
$error_pointer = $error['pointer'] ?? null;
@@ -64,8 +66,8 @@ public function __construct(
6466
);
6567
$this->errors = $errors;
6668

67-
$formatted_errors = \HH\Lib\Vec\map($errors, $error ==> $error['message'])
68-
|> \HH\Lib\Str\join($$, '');
69+
$formatted_errors = Vec\map($errors, $error ==> $error['message'])
70+
|> Str\join($$, '');
6971
$message = "Error validating field '{$pointer}': {$formatted_errors}";
7072
parent::__construct($message, $code, $previous);
7173
}

src/Utils.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Slack\Hack\JsonSchema;
44

5-
use namespace HH\Lib\Str;
5+
use namespace HH\Lib\{Str, Vec};
66

77
function get_pointer(string $current, string ...$parts): string {
8-
$encoded = \HH\Lib\Vec\map($parts, $path ==> encode_path($path))
9-
|> \HH\Lib\Str\join($$, '/');
8+
$encoded = Vec\map($parts, $path ==> encode_path($path))
9+
|> Str\join($$, '/');
1010

1111
return !Str\is_empty($current) ? "{$current}/{$encoded}" : "/{$encoded}";
1212
}

tests/AddressSchemaValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testNonStringInputs(): void {
5151
]);
5252
$validator->validate();
5353
expect($validator->isValid())->toBeFalse('non string inputs are invalid');
54-
expect(\count($validator->getErrors()))->toBeSame(2);
54+
expect(C\count($validator->getErrors()))->toBeSame(2);
5555
}
5656

5757
public function testAddressWithNumericPostalCode(): void {
@@ -191,7 +191,7 @@ public function testAddressWithFailedFileRef(): void {
191191
expect($validator->isValid())->toBeFalse(
192192
'file reference to phone type should be invalid',
193193
);
194-
expect(\count($validator->getErrors()))->toBeSame(3);
194+
expect(C\count($validator->getErrors()))->toBeSame(3);
195195
}
196196

197197
public function testAddressWithDifferentFileRef(): void {

tests/BaseCodegenTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Slack\Hack\JsonSchema\Tests;
44

5-
use namespace HH\Lib\{C, Str};
5+
use namespace HH\Lib\{C, Math, Str};
66
use function Facebook\FBExpect\expect;
77
use type Slack\Hack\JsonSchema\Validator;
88
use type Slack\Hack\JsonSchema\Codegen\{Codegen, IJsonSchemaCodegenConfig};
@@ -121,7 +121,7 @@ public static function benchmark(string $label, (function(): void) $callback): v
121121
$benchmarks[] = $end - $start;
122122
}
123123

124-
$average = \HH\Lib\Math\sum($benchmarks) / C\count($benchmarks);
124+
$average = Math\sum($benchmarks) / C\count($benchmarks);
125125
\print_r("{$label} took: {$average}\n");
126126
}
127127

0 commit comments

Comments
 (0)