Commit 717fc809 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Prend en charge correctement les paramètres optionnels.

parent 0f799235
......@@ -192,7 +192,17 @@ final class DirectClientCall implements ClientCall
private function transpose(string $name, array $properties): string
{
foreach ($properties as $property) {
$parts[] = sprintf('%s: %s%s', $property->getName(), $name, $property->getUsage());
$attrRef = $property->getUsage($name);
$expr = $property->getType()->castToStringOrStringArray($attrRef);
if ($expr !== $attrRef && $property->isNullable()) {
$expr = sprintf(
'(%s ? %s : undefined)',
$property->getType()->checkType($attrRef),
$expr
);
}
$parts[] = sprintf('%s: %s', $property->getObjectKey(), $expr);
}
return '{ ' . implode(', ', $parts) . ' }';
......
......@@ -60,6 +60,22 @@ final class Alias extends AbstractType implements Declaration
return $this->name;
}
/**
* {@inheritdoc}
*/
public function castToStringOrStringArray(string $expr): string
{
return $this->target->castToStringOrStringArray($expr);
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
{
return $this->target->checkType($expr);
}
/**
* {@inheritdoc}
*/
......
......@@ -19,6 +19,8 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types;
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class AnonymousObject.
*/
......@@ -57,6 +59,28 @@ class AnonymousObject extends AbstractType implements Object
return $this->getDeclarationBody(false);
}
/**
* {@inheritdoc}
*/
public function castToStringOrStringArray(string $expr): string
{
return sprintf('JSON.stringify(%s)', $expr);
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
{
foreach ($this->properties as $prop) {
if ($prop->isIdentifier()) {
return sprintf('%s in %s', TypescriptHelper::quoteString($prop->getName()), $expr);
}
}
return BuiltinType::get('object')->checkType($expr);
}
/**
* {@inheritdoc}
*/
......
......@@ -20,6 +20,7 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types;
use Irstea\NgModelGeneratorBundle\Models\NamedTrait;
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class BuiltinType.
......@@ -46,6 +47,26 @@ final class BuiltinType extends AbstractType
return $this->name;
}
/**
* {@inheritdoc}
*/
public function castToStringOrStringArray(string $expr): string
{
switch ($this->name) {
case 'string':
return $expr;
case 'number':
case 'boolean':
return sprintf('%s.toString()', $expr);
case 'undefined':
case 'null':
case 'never':
return '';
default:
return sprintf('JSON.stringify(%s)', $expr);
}
}
/**
* @param string $name
*
......@@ -62,6 +83,14 @@ final class BuiltinType extends AbstractType
return $instances[$name];
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
{
return sprintf('(typeof %s === %s)', $expr, TypescriptHelper::quoteString($this->name));
}
/**
* {@inheritdoc}
*/
......
......@@ -47,6 +47,27 @@ final class Collection extends AbstractType
return sprintf('Array<%s>', $this->itemType->getUsage());
}
/**
* {@inheritdoc}
*/
public function castToStringOrStringArray(string $expr): string
{
$cast = $this->itemType->castToStringOrStringArray('x');
if ($cast !== 'x') {
return sprintf('%s.map(x => %s)', $expr, $cast);
}
return $expr;
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
{
return sprintf('Array.isArray(%s)', $expr);
}
/**
* {@inheritdoc}
*/
......
......@@ -87,6 +87,18 @@ class ObjectClass extends AnonymousObject implements Declaration
return 'extends ' . implode(', ', $extends);
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
{
if ($this->kind === 'class') {
return sprintf('(%s instanceof %s)', $expr, $this->name);
}
return 'true';
}
/**
* {@inheritdoc}
*/
......
......@@ -128,19 +128,30 @@ final class Property implements \IteratorAggregate
return sprintf(
'%s%s%s: %s',
$this->isReadonly ? 'readonly ' : '',
TypescriptHelper::isValidIdentifier($this->name) ? $this->name : TypescriptHelper::quoteString($this->name),
$this->getObjectKey(),
$this->isNullable ? '?' : '',
$this->type->getUsage()
);
}
/**
* @return string
*/
public function getObjectKey(): string
{
return TypescriptHelper::isValidIdentifier($this->name) ? $this->name : TypescriptHelper::quoteString($this->name);
}
/**
* {@inheritdoc}
*/
public function getUsage(): string
public function getUsage(string $varName): string
{
return TypescriptHelper::isValidIdentifier($this->name) ? '.' . $this->name :
'[' . TypescriptHelper::quoteString($this->name) . ']';
return $varName . (
TypescriptHelper::isValidIdentifier($this->name)
? ('.' . $this->name)
: ('[' . TypescriptHelper::quoteString($this->name) . ']')
);
}
/**
......
......@@ -73,6 +73,22 @@ class Reference extends AbstractType
return $this->dereference()->getUsage();
}
/**
* {@inheritdoc}
*/
public function castToStringOrStringArray(string $expr): string
{
return $this->dereference()->castToStringOrStringArray($expr);
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
{
return $this->dereference()->checkType($expr);
}
/**
* {@inheritdoc}
*/
......
......@@ -29,6 +29,20 @@ interface Type extends \IteratorAggregate
*/
public function getUsage(): string;
/**
* @param string $expr
*
* @return string
*/
public function castToStringOrStringArray(string $expr): string;
/**
* @param string $expr
*
* @return string
*/
public function checkType(string $expr): string;
/**
* @return Type
*/
......
......@@ -25,7 +25,7 @@ namespace Irstea\NgModelGeneratorBundle\Models\Types;
final class Union extends AbstractType
{
/**
* @var array Type
* @var Type[]
*/
private $types = [];
......@@ -53,6 +53,38 @@ final class Union extends AbstractType
return implode(' | ', array_keys($reprs));
}
/**
* {@inheritdoc}
*/
public function castToStringOrStringArray(string $expr): string
{
$cast = $expr;
foreach ($this->types as $type) {
$check = $type->checkType($expr);
if ($check) {
$typeCast = $type->castToStringOrStringArray($expr);
if ($typeCast !== $cast) {
$cast = sprintf('(%s ? %s : %s)', $check, $type->castToStringOrStringArray($expr), $cast);
}
}
}
return $cast;
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
{
$checks = [];
foreach ($this->types as $type) {
$checks[$type->checkType($expr)] = true;
}
return '(' . implode(' || ', array_keys($checks)) . ')';
}
/**
* @return \Generator|Type[]
*/
......
......@@ -44,7 +44,7 @@ final class TypescriptHelper
*
* @return string
*/
public static function quoteString(string $string, string $delimiter = '"'): string
public static function quoteString(string $string, string $delimiter = "'"): string
{
if (strpos($string, $delimiter) !== false) {
if ($delimiter === "'" && strpos($string, '"') === false) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment