Skip to content

Commit

Permalink
Drop discriminator field mapper, use value directly
Browse files Browse the repository at this point in the history
  • Loading branch information
olsavmic committed Sep 9, 2024
1 parent c11c1be commit d5ffb0f
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 70 deletions.
8 changes: 1 addition & 7 deletions src/Compiler/Mapper/Object/MapDiscriminatedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use ShipMonk\InputMapper\Compiler\Exception\CannotCompileMapperException;
use ShipMonk\InputMapper\Compiler\Mapper\GenericMapperCompiler;
use ShipMonk\InputMapper\Compiler\Mapper\MapperCompiler;
use ShipMonk\InputMapper\Compiler\Mapper\Scalar\MapString;
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\MapNullable;
use ShipMonk\InputMapper\Compiler\Php\PhpCodeBuilder;
use ShipMonk\InputMapper\Compiler\Type\GenericTypeParameter;
use ShipMonk\InputMapper\Compiler\Type\PhpDocTypeUtils;
Expand Down Expand Up @@ -80,10 +78,6 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi

$discriminatorRawValue = $builder->arrayDimFetch($value, $discriminatorFieldNameValue);
$discriminatorPath = $builder->arrayImmutableAppend($path, $discriminatorFieldNameValue);
$discriminatorMapperMethodName = $builder->uniqMethodName('mapDiscriminatorField');
$discriminatorMapperMethod = $builder->mapperMethod($discriminatorMapperMethodName, new MapNullable(new MapString()))->makePrivate()->getNode();
$discriminatorMapperCall = $builder->methodCall($builder->var('this'), $discriminatorMapperMethodName, [$discriminatorRawValue, $discriminatorPath]);
$builder->addMethod($discriminatorMapperMethod);

$validMappingKeys = array_keys($this->subtypeCompilers);

Expand Down Expand Up @@ -121,7 +115,7 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
),
);

$matchedSubtype = $builder->match($discriminatorMapperCall, $subtypeMatchArms);
$matchedSubtype = $builder->match($discriminatorRawValue, $subtypeMatchArms);

return new CompiledExpr(
$matchedSubtype,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function array_key_exists;
use function implode;
use function is_array;
use function is_string;

/**
* Generated mapper by {@see MapDiscriminatedObject}. Do not edit directly.
Expand All @@ -36,32 +35,13 @@ public function map(mixed $data, array $path = []): HierarchicalParentInput
throw MappingFailedException::missingKey($path, 'type');
}

return match ($this->mapDiscriminatorField($data['type'], [...$path, 'type'])) {
return match ($data['type']) {
'childOne' => $this->mapChildOne($data, $path),
'childTwo' => $this->mapChildTwo($data, $path),
default => throw MappingFailedException::incorrectValue($data['type'], [...$path, 'type'], 'one of ' . implode(', ', ['childOne', 'childTwo'])),
};
}

/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
private function mapDiscriminatorField(mixed $data, array $path = []): ?string
{
if ($data === null) {
$mapped = null;
} else {
if (!is_string($data)) {
throw MappingFailedException::incorrectType($data, $path, 'string');
}

$mapped = $data;
}

return $mapped;
}

/**
* @param list<string|int> $path
* @throws MappingFailedException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function array_key_exists;
use function implode;
use function is_array;
use function is_string;

/**
* Generated mapper by {@see MapDiscriminatedObject}. Do not edit directly.
Expand All @@ -36,31 +35,12 @@ public function map(mixed $data, array $path = []): HierarchicalWithEnumParentIn
throw MappingFailedException::missingKey($path, 'type');
}

return match ($this->mapDiscriminatorField($data['type'], [...$path, 'type'])) {
return match ($data['type']) {
'childOne' => $this->mapChildOne($data, $path),
default => throw MappingFailedException::incorrectValue($data['type'], [...$path, 'type'], 'one of ' . implode(', ', ['childOne'])),
};
}

/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
private function mapDiscriminatorField(mixed $data, array $path = []): ?string
{
if ($data === null) {
$mapped = null;
} else {
if (!is_string($data)) {
throw MappingFailedException::incorrectType($data, $path, 'string');
}

$mapped = $data;
}

return $mapped;
}

/**
* @param list<string|int> $path
* @throws MappingFailedException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function array_key_exists;
use function implode;
use function is_array;
use function is_string;

/**
* Generated mapper by {@see MapDiscriminatedObject}. Do not edit directly.
Expand All @@ -36,31 +35,12 @@ public function map(mixed $data, array $path = []): HierarchicalWithNoTypeFieldP
throw MappingFailedException::missingKey($path, '$type');
}

return match ($this->mapDiscriminatorField($data['$type'], [...$path, '$type'])) {
return match ($data['$type']) {
'childOne' => $this->mapChildOne($data, $path),
default => throw MappingFailedException::incorrectValue($data['$type'], [...$path, '$type'], 'one of ' . implode(', ', ['childOne'])),
};
}

/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
private function mapDiscriminatorField(mixed $data, array $path = []): ?string
{
if ($data === null) {
$mapped = null;
} else {
if (!is_string($data)) {
throw MappingFailedException::incorrectType($data, $path, 'string');
}

$mapped = $data;
}

return $mapped;
}

/**
* @param list<string|int> $path
* @throws MappingFailedException
Expand Down

0 comments on commit d5ffb0f

Please sign in to comment.