Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support normalizing Ds\Collection #527

Merged
merged 2 commits into from
Jun 6, 2024
Merged

Conversation

simPod
Copy link
Contributor

@simPod simPod commented Apr 12, 2024

When I use transformers

(new \CuyZ\Valinor\MapperBuilder())
    ->registerTransformer(fn (\Ds\Set $v) => $v->toArray())
    ->registerTransformer(fn(Value $v) => $v->get())
    ->normalizer(\CuyZ\Valinor\Normalizer\Format::json())

My \Ds\Set<Value> is normalized to array<Value> but the Value is then ignored since the serialization is not recursive for the Set.

This fixes it.

Initially, I wanted to make this work for all IteratorAggregates and Iterators but with then 'iterable class' case does not pass since it is designed to serialize its properties and not inner Iterator.

This changes the behavior of the normalizer regarding iterable objects,
like `ArrayObject`,  `\Ds\Collection` or any other collection.

Before, the properties of the object would be accessed like any other
"basic" object. Now, the object will be treated as any other iterable
value.
@romm
Copy link
Member

romm commented Jun 6, 2024

Thanks @simPod! I've decided to change the behavior regarding iterable objects, which fixes the issue you had with Ds\Collection objects, or any other collection objects. 🙂

@romm romm merged commit c481b07 into CuyZ:master Jun 6, 2024
11 checks passed
@simPod
Copy link
Contributor Author

simPod commented Jun 10, 2024

Awesome!

@simPod simPod deleted the ds-collection branch June 10, 2024 20:53
@simPod
Copy link
Contributor Author

simPod commented Sep 16, 2024

@romm hi, I was thinking whether valinor supports objects as a key in collections 🤔

@romm
Copy link
Member

romm commented Sep 16, 2024

@simPod Hi! Would you have a real-life example of why this feature would be needed? 😊

@simPod
Copy link
Contributor Author

simPod commented Sep 16, 2024

E.g. serde of a ds map:

use \Ds\Map;

/** @var Map<list<int>, string> */
$m = new Map();
$m->put([1], 'le value');
$m->put([1, 2], 'le value2');

$normalizer->normalize($m);

or just a simple generator yield [1]=>'le value'

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.

2 participants