Skip to content

Commit d7a3578

Browse files
authored
Merge pull request #62 from slackhq/ih_format
Run hackfmt on repo
2 parents 074b299 + 7130c01 commit d7a3578

15 files changed

+232
-282
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ lint:
1919
docker run -v `pwd`:/app -it slack/hack-json-schema ./vendor/bin/hhast-lint
2020

2121
format:
22-
docker run -v `pwd`:/app -it slack/hack-json-schema find {src,tests} -type f -name "*.hack" -exec hackfmt -i {} \;
22+
docker run -v `pwd`:/app -it slack/hack-json-schema find {src,tests} -type f -name "*.hack" -o -name "*.php" -exec hackfmt -i {} \;
2323

2424
typecheck:
2525
docker run -v `pwd`:/app -it slack/hack-json-schema /bin/bash -c './vendor/bin/hh-autoload && hh_server --check .'

src/Codegen/Constraints/ArrayBuilder.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,8 @@ private function buildItemsMultiSchema(
192192

193193
$constraints = vec[];
194194
foreach ($schemas as $index => $schema) {
195-
$schema_builder = new SchemaBuilder(
196-
$this->ctx,
197-
$this->generateClassName('items', $this->suffix, (string)$index),
198-
$schema,
199-
);
195+
$schema_builder =
196+
new SchemaBuilder($this->ctx, $this->generateClassName('items', $this->suffix, (string)$index), $schema);
200197
$schema_builder->build();
201198

202199
$constraints[] = "{$schema_builder->getClassName()}::check<>";

src/Codegen/Constraints/BaseBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected function addHackEnumConstraintCheck(HackBuilder $hb): void {
119119
$hack_enum_value is nonnull,
120120
"'%s' must contain only values of type %s",
121121
$rc->getName(),
122-
$schema_type === TSchemaType::INTEGER_T ? 'int' : 'string'
122+
$schema_type === TSchemaType::INTEGER_T ? 'int' : 'string',
123123
);
124124
$hack_enum_values[] = $hack_enum_value;
125125
}
@@ -132,13 +132,13 @@ protected function addHackEnumConstraintCheck(HackBuilder $hb): void {
132132
$enum_value is string,
133133
"Enum value '%s' is not a valid value for '%s'",
134134
\print_r($enum_value, true),
135-
$rc->getName()
135+
$rc->getName(),
136136
);
137137
invariant(
138138
C\contains_key($hack_enum_values, $enum_value),
139139
"Enum value '%s' is unexpectedly not present in '%s'",
140140
\print_r($enum_value, true),
141-
$rc->getName()
141+
$rc->getName(),
142142
);
143143
}
144144
}
@@ -148,8 +148,8 @@ protected function addHackEnumConstraintCheck(HackBuilder $hb): void {
148148
vec[
149149
'$typed',
150150
Str\format('\%s::class', $rc->getName()),
151-
'$pointer'
152-
]
151+
'$pointer',
152+
],
153153
);
154154
}
155155

src/Codegen/Constraints/ObjectBuilder.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ protected function getCheckMethodCode(
138138

139139
$additional_properties = $this->typed_schema['additionalProperties'] ?? null;
140140
$is_additional_properties_boolean = $additional_properties is nonnull && $additional_properties is bool;
141-
$allow_any_additional_properties = $additional_properties is nonnull &&
142-
$is_additional_properties_boolean &&
143-
$additional_properties;
144-
$discard_additional_properties = $additional_properties === false &&
145-
$this->ctx->shouldDiscardAdditionalProperties();
141+
$allow_any_additional_properties =
142+
$additional_properties is nonnull && $is_additional_properties_boolean && $additional_properties;
143+
$discard_additional_properties =
144+
$additional_properties === false && $this->ctx->shouldDiscardAdditionalProperties();
146145

147146
$discard_all = $discard_additional_properties &&
148147
($properties is null || C\count($properties) == 0) &&
@@ -411,11 +410,8 @@ protected function getCheckMethodCode(
411410
} else if ($additional_properties is nonnull) {
412411
$schema = type_assert_shape($additional_properties, 'Slack\Hack\JsonSchema\Codegen\TSchema');
413412

414-
$additional_properties_builder = new SchemaBuilder(
415-
$this->ctx,
416-
$this->generateClassName($this->suffix, 'additional_properties'),
417-
$schema,
418-
);
413+
$additional_properties_builder =
414+
new SchemaBuilder($this->ctx, $this->generateClassName($this->suffix, 'additional_properties'), $schema);
419415
$additional_properties_builder->build();
420416
$additional_properties_class_name = $additional_properties_builder->getClassName();
421417

@@ -529,9 +525,8 @@ private function codegenType(
529525
$additional_properties = $this->typed_schema['additionalProperties'] ?? null;
530526
$defaults = $this->getDefaults();
531527

532-
$allow_subtyping = $additional_properties is nonnull && $additional_properties is bool
533-
? $additional_properties
534-
: true;
528+
$allow_subtyping =
529+
$additional_properties is nonnull && $additional_properties is bool ? $additional_properties : true;
535530

536531
$members = vec[];
537532
foreach ($property_classes as $property => $builder) {

src/Codegen/Constraints/UntypedBuilder.php

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,8 @@ protected function getCheckMethod(): CodegenMethod {
7979
private function generateNotChecks(vec<TSchema> $schemas, HackBuilder $hb): void {
8080
$constraints = vec[];
8181
foreach ($schemas as $index => $schema) {
82-
$schema_builder = new SchemaBuilder(
83-
$this->ctx,
84-
$this->generateClassName($this->suffix, 'not', (string)$index),
85-
$schema,
86-
);
82+
$schema_builder =
83+
new SchemaBuilder($this->ctx, $this->generateClassName($this->suffix, 'not', (string)$index), $schema);
8784
$schema_builder->build();
8885
$constraints[] = "{$schema_builder->getClassName()}::check<>";
8986
}
@@ -134,11 +131,8 @@ private function generateNotChecks(vec<TSchema> $schemas, HackBuilder $hb): void
134131
private function generateOneOfChecks(vec<TSchema> $schemas, HackBuilder $hb): void {
135132
$constraints = vec[];
136133
foreach ($schemas as $index => $schema) {
137-
$schema_builder = new SchemaBuilder(
138-
$this->ctx,
139-
$this->generateClassName($this->suffix, 'oneOf', (string)$index),
140-
$schema,
141-
);
134+
$schema_builder =
135+
new SchemaBuilder($this->ctx, $this->generateClassName($this->suffix, 'oneOf', (string)$index), $schema);
142136
$schema_builder->build();
143137
$constraints[] = "{$schema_builder->getClassName()}::check<>";
144138
}
@@ -220,11 +214,8 @@ public function getMergedAllOfChecks(): ?TSchema {
220214
$schemas = $this->typed_schema['allOf'] ?? vec[]
221215
|> Vec\reverse($$); // Reverse for parity with non-strict output; this shouldn't actually matter.
222216
foreach ($schemas as $index => $schema) {
223-
$schema_builder = new SchemaBuilder(
224-
$this->ctx,
225-
$this->generateClassName($this->suffix, 'allOf', (string)$index),
226-
$schema
227-
);
217+
$schema_builder =
218+
new SchemaBuilder($this->ctx, $this->generateClassName($this->suffix, 'allOf', (string)$index), $schema);
228219
$schema = $schema_builder->getResolvedSchema();
229220
if (Shapes::keyExists($schema, 'allOf')) {
230221
$builder = new UntypedBuilder($this->ctx, 'allOf', $schema);
@@ -294,15 +285,13 @@ public function getMergedAllOfChecks(): ?TSchema {
294285
|> Vec\unique($$);
295286

296287
if (Shapes::keyExists($schema, 'minProperties')) {
297-
$min_properties = $min_properties is null
298-
? $schema['minProperties']
299-
: Math\maxva($min_properties, $schema['minProperties']);
288+
$min_properties =
289+
$min_properties is null ? $schema['minProperties'] : Math\maxva($min_properties, $schema['minProperties']);
300290
}
301291

302292
if (Shapes::keyExists($schema, 'maxProperties')) {
303-
$max_properties = $max_properties is null
304-
? $schema['maxProperties']
305-
: Math\minva($max_properties, $schema['maxProperties']);
293+
$max_properties =
294+
$max_properties is null ? $schema['maxProperties'] : Math\minva($max_properties, $schema['maxProperties']);
306295
}
307296

308297
$coerce = $coerce || Shapes::idx($schema, 'coerce', false);
@@ -331,11 +320,7 @@ public function getMergedAllOfChecks(): ?TSchema {
331320
}
332321

333322
private function generateMergedAllOfChecks(TSchema $schema, HackBuilder $hb): void {
334-
$schema_builder = new SchemaBuilder(
335-
$this->ctx,
336-
$this->generateClassName($this->suffix, 'allOf'),
337-
$schema
338-
);
323+
$schema_builder = new SchemaBuilder($this->ctx, $this->generateClassName($this->suffix, 'allOf'), $schema);
339324
$schema_builder->build();
340325
$this->current_type = $schema_builder->getType();
341326
$hb->addReturnf('%s::check($input, $pointer)', $schema_builder->getClassName());
@@ -344,11 +329,8 @@ private function generateMergedAllOfChecks(TSchema $schema, HackBuilder $hb): vo
344329
private function generateMixedAllOfChecks(vec<TSchema> $schemas, HackBuilder $hb): void {
345330
$constraints = vec[];
346331
foreach ($schemas as $index => $schema) {
347-
$schema_builder = new SchemaBuilder(
348-
$this->ctx,
349-
$this->generateClassName($this->suffix, 'allOf', (string)$index),
350-
$schema,
351-
);
332+
$schema_builder =
333+
new SchemaBuilder($this->ctx, $this->generateClassName($this->suffix, 'allOf', (string)$index), $schema);
352334
$schema_builder->build();
353335
$constraints[] = "{$schema_builder->getClassName()}::check<>";
354336
}
@@ -454,10 +436,8 @@ private function getOptimizedAnyOfTypes(vec<SchemaBuilder> $schema_builders): ?T
454436
$property_type = TSchemaType::assert($property_type);
455437

456438
if ($property_type === TSchemaType::STRING_T && C\contains($required, $property_name)) {
457-
$typed_property_schema = type_assert_shape(
458-
$property_schema,
459-
'Slack\Hack\JsonSchema\Codegen\TStringSchema',
460-
);
439+
$typed_property_schema =
440+
type_assert_shape($property_schema, 'Slack\Hack\JsonSchema\Codegen\TStringSchema');
461441

462442
$enum = $typed_property_schema['enum'] ?? null;
463443
if ($enum is nonnull && C\count($enum) === 1) {

src/Codegen/ValidatorBuilder.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,8 @@ private function getCodegenFile(string $filename, ?string $namespace, string $cl
7676
private function buildCodegenClass(string $classname, CodegenFile $file): void {
7777
$class = $this->cg->codegenClass($classname);
7878

79-
$root = new RootBuilder(
80-
$this->codegenConfig,
81-
$this->cg,
82-
$this->jsonSchemaCodegenConfig,
83-
$this->schema,
84-
$class,
85-
$file,
86-
);
79+
$root =
80+
new RootBuilder($this->codegenConfig, $this->cg, $this->jsonSchemaCodegenConfig, $this->schema, $class, $file);
8781

8882
$root->build();
8983

src/Constraints/HackEnumConstraint.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
* and `hackEnum` may still be preferable to manually-written validation in those cases.
1515
*/
1616
final class HackEnumConstraint {
17-
public static function check<T as arraykey>(mixed $input, \HH\enumname<T> $enum_class, string $pointer): T {
18-
$typed = $enum_class::coerce($input);
19-
if ($typed is nonnull) {
20-
return $typed;
21-
}
22-
23-
$error = shape(
24-
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,
25-
'constraint' => shape(
26-
'type' => JsonSchema\FieldErrorConstraint::ENUM,
27-
'expected' => keyset($enum_class::getNames()),
28-
'got' => $input,
29-
),
30-
'message' => 'must be a valid enum value',
31-
);
32-
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
17+
public static function check<T as arraykey>(mixed $input, \HH\enumname<T> $enum_class, string $pointer): T {
18+
$typed = $enum_class::coerce($input);
19+
if ($typed is nonnull) {
20+
return $typed;
3321
}
22+
23+
$error = shape(
24+
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,
25+
'constraint' => shape(
26+
'type' => JsonSchema\FieldErrorConstraint::ENUM,
27+
'expected' => keyset($enum_class::getNames()),
28+
'got' => $input,
29+
),
30+
'message' => 'must be a valid enum value',
31+
);
32+
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
33+
}
3434
}

src/Constraints/NumberMultipleOfConstraint.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ class NumberMultipleOfConstraint {
1717
public static function check(num $dividend, num $devisor, string $pointer): void {
1818
invariant($devisor > 0, 'multipleOf 0 or a negative number does not make sense. Use a positive non-zero number.');
1919

20-
$remainer = Math\abs(
21-
$dividend is int && $devisor is int ? $dividend % $devisor : \fmod((float)$dividend, (float)$devisor),
22-
);
20+
$remainer =
21+
Math\abs($dividend is int && $devisor is int ? $dividend % $devisor : \fmod((float)$dividend, (float)$devisor));
2322

2423
$error = shape(
2524
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,

src/Constraints/ObjectMaxPropertiesConstraint.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
use namespace Slack\Hack\JsonSchema;
66

77
class ObjectMaxPropertiesConstraint {
8-
public static function check(int $num_properties, int $max_properties, string $pointer): void {
9-
if ($num_properties > $max_properties) {
10-
$error = shape(
11-
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,
12-
'constraint' => shape(
13-
'type' => JsonSchema\FieldErrorConstraint::MAX_PROPERTIES,
14-
'expected' => $max_properties,
15-
'got' => $num_properties,
16-
),
17-
'message' => "no more than {$max_properties} properties allowed",
18-
);
19-
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
20-
}
8+
public static function check(int $num_properties, int $max_properties, string $pointer): void {
9+
if ($num_properties > $max_properties) {
10+
$error = shape(
11+
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,
12+
'constraint' => shape(
13+
'type' => JsonSchema\FieldErrorConstraint::MAX_PROPERTIES,
14+
'expected' => $max_properties,
15+
'got' => $num_properties,
16+
),
17+
'message' => "no more than {$max_properties} properties allowed",
18+
);
19+
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
2120
}
21+
}
2222
}

src/Constraints/ObjectMinPropertiesConstraint.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
use namespace Slack\Hack\JsonSchema;
66

77
class ObjectMinPropertiesConstraint {
8-
public static function check(int $num_properties, int $min_properties, string $pointer): void {
9-
if ($num_properties < $min_properties) {
10-
$error = shape(
11-
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,
12-
'constraint' => shape(
13-
'type' => JsonSchema\FieldErrorConstraint::MIN_PROPERTIES,
14-
'expected' => $min_properties,
15-
'got' => $num_properties,
16-
),
17-
'message' => "must have minimum {$min_properties} properties",
18-
);
19-
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
20-
}
8+
public static function check(int $num_properties, int $min_properties, string $pointer): void {
9+
if ($num_properties < $min_properties) {
10+
$error = shape(
11+
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,
12+
'constraint' => shape(
13+
'type' => JsonSchema\FieldErrorConstraint::MIN_PROPERTIES,
14+
'expected' => $min_properties,
15+
'got' => $num_properties,
16+
),
17+
'message' => "must have minimum {$min_properties} properties",
18+
);
19+
throw new JsonSchema\InvalidFieldException($pointer, vec[$error]);
2120
}
21+
}
2222
}

0 commit comments

Comments
 (0)