Skip to content

Commit dd097d8

Browse files
authored
Merge pull request #24 from veewee/bugfix-nullable
Fix setting metadata back to null bugs
2 parents d1611b1 + 2972b40 commit dd097d8

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/Metadata/Model/TypeMeta.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use function Psl\Type\bool;
1010
use function Psl\Type\mixed_dict;
1111
use function Psl\Type\non_empty_string;
12+
use function Psl\Type\nullable;
1213
use function Psl\Type\optional;
1314
use function Psl\Type\shape;
1415
use function Psl\Type\string;
@@ -235,7 +236,7 @@ public function enums(): Option
235236
public function withEnums(?array $enums): self
236237
{
237238
$new = clone $this;
238-
$new->enums = from_nullable(optional(vec(string()))->coerce($enums));
239+
$new->enums = from_nullable(nullable(vec(string()))->coerce($enums));
239240

240241
return $new;
241242
}
@@ -254,7 +255,7 @@ public function extends(): Option
254255
public function withExtends(?array $extends): self
255256
{
256257
$new = clone $this;
257-
$new->extends = from_nullable(optional(
258+
$new->extends = from_nullable(nullable(
258259
shape([
259260
'type' => non_empty_string(),
260261
'namespace' => non_empty_string(),
@@ -487,7 +488,7 @@ public function restriction(): Option
487488
public function withRestriction(?array $restriction): self
488489
{
489490
$new = clone $this;
490-
$new->restriction = from_nullable(optional(mixed_dict())->coerce($restriction));
491+
$new->restriction = from_nullable(nullable(mixed_dict())->coerce($restriction));
491492

492493
return $new;
493494
}
@@ -506,7 +507,7 @@ public function unions(): Option
506507
public function withUnions(?array $unions): self
507508
{
508509
$new = clone $this;
509-
$new->unions = from_nullable(optional(
510+
$new->unions = from_nullable(nullable(
510511
vec(
511512
shape([
512513
'type' => non_empty_string(),
@@ -565,7 +566,7 @@ public function arrayType(): Option
565566
public function withArrayType(?array $arrayType): self
566567
{
567568
$new = clone $this;
568-
$new->arrayType = from_nullable(optional(
569+
$new->arrayType = from_nullable(nullable(
569570
shape([
570571
'type' => non_empty_string(),
571572
'itemType' => non_empty_string(),

tests/Unit/Metadata/Collection/TypeMetaTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function test_it_has_enums()
3434
$value = ['hello', 'world'];
3535
$meta = (new TypeMeta())->withEnums($value);
3636
static::assertSame($value, $meta->enums()->unwrapOr(null));
37+
38+
$emptied = $meta->withEnums(null);
39+
static::assertNotSame($meta, $emptied);
40+
static::assertTrue($emptied->enums()->isNone());
3741
}
3842

3943
public function test_it_has_extends()
@@ -44,6 +48,10 @@ public function test_it_has_extends()
4448
];
4549
$meta = (new TypeMeta())->withExtends($value);
4650
static::assertSame($value, $meta->extends()->unwrapOr(null));
51+
52+
$emptied = $meta->withExtends(null);
53+
static::assertNotSame($meta, $emptied);
54+
static::assertTrue($emptied->extends()->isNone());
4755
}
4856

4957
public function test_it_has_fixed()
@@ -135,6 +143,10 @@ public function test_it_has_restriction()
135143
$value = ['mixed' => ['content']];
136144
$meta = (new TypeMeta())->withRestriction($value);
137145
static::assertSame($value, $meta->restriction()->unwrapOr(null));
146+
147+
$emptied = $meta->withRestriction(null);
148+
static::assertNotSame($meta, $emptied);
149+
static::assertTrue($emptied->restriction()->isNone());
138150
}
139151

140152
public function test_it_has_unions()
@@ -148,6 +160,10 @@ public function test_it_has_unions()
148160
];
149161
$meta = (new TypeMeta())->withUnions($value);
150162
static::assertSame($value, $meta->unions()->unwrapOr(null));
163+
164+
$emptied = $meta->withUnions(null);
165+
static::assertNotSame($meta, $emptied);
166+
static::assertTrue($emptied->unions()->isNone());
151167
}
152168

153169
public function test_it_has_use()

0 commit comments

Comments
 (0)