Skip to content

Conversation

jlabedo
Copy link
Contributor

@jlabedo jlabedo commented Oct 2, 2025

Why is this change proposed?

Allow defining static function for converters, eg. :

class PriceWasChanged
{
    public function __construct(private string $productId, private float $price) {}

    public function getProductId(): string
    {
        return $this->productId;
    }

    public function getPrice(): float
    {
        return $this->price;
    }

    #[Converter]
    public static function fromArray(array $data): self
    {
        return new self($data['productId'], $data['price']);
    }

    #[Converter]
    public static function toArray(self $object): array
    {
        return [
            'productId' => $object->productId,
            'price' => $object->price,
        ];
    }
}

Description of changes

For projecting benchmark: added static converters and disabled JMS that a substantial impact on this benchmark

Pull Request Contribution Terms

  • I have read and agree to the contribution terms outlined in CONTRIBUTING.

@jlabedo jlabedo requested a review from dgafka October 2, 2025 11:59
@lifinsky
Copy link
Contributor

lifinsky commented Oct 2, 2025

Hi! #523 (comment)

I’ll try to submit two bug reports by the end of the day that blocked us from upgrading from version 1.256 and above.

* @return stdClass[]
*/
#[Converter]
public static function convert(array $data): iterable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case for allowing converter to work on arrays, instead of specific singular types?
Converter would be called multiple times for given type, meaning string => stdClass, will cover string[] => stdClass[]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a copy/paste of an existing test case, I'm not changing the behavior here.
But as you mention, I would have liked the converters to only be able to convert from array to object or from object to array (only normalization and denormalization), that would have allowed me to do converter resolution at compile time in #526

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have liked the converters to only be able to convert from array to object or from object to array

@jlabedo I don't think we can do that. Things like Enums converted to scalars in headers would stop working.
But you can point me to the place where it will be needed and we think of the solution there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a copy/paste of an existing test case, I'm not changing the behavior here.

Oki then, I would expect this one to fail :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jlabedo I don't think we can do that. Things like Enums converted to scalars in headers would stop working.

You are right, I meant from scalar to object and from object to scalar. And by scalar I mean int,float,bool,string,arrayOfScalars.

But you can point me to the place where it will be needed and we think of the solution there

Nowhere in Ecotone, but it could be a BC if people have used this feature.

@dgafka dgafka merged commit d2239be into ecotoneframework:main Oct 2, 2025
7 checks passed
@dgafka
Copy link
Member

dgafka commented Oct 3, 2025

@jlabedo can you update the docs? You can do it directly in the documention repo, as each merge republishes the doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants