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

[core] How can I use mapWith with arrays? #253

Closed
micalevisk opened this issue Feb 2, 2021 · 3 comments
Closed

[core] How can I use mapWith with arrays? #253

micalevisk opened this issue Feb 2, 2021 · 3 comments

Comments

@micalevisk
Copy link
Contributor

micalevisk commented Feb 2, 2021

Hey!

I'm trying to use mapWith when the source and destination values are arrays. Is there a way to do this?

What I've done:

mapper.createMap(User, UserDto).forMember(
  (d) => d.someArrayField,
  mapWith(
    () => XDto,
    (s) => [], // the mapper will return an XDto instance instead of this value
    () => X
  )
)

I see that is a limitation due to these lines:

(source, mapper) => {
const sourceValue = withSourceValue(source);
return mapper.map(

since if I use mapper.mapArray instead, it will work as I expected.

To test that you could use this:

(d) => d.avatar,
mapWith(
() => AvatarVm,
(s) => s.avatar,
() => Avatar

but changing .avatar to .addresses, AvatarVm to AddressVm and Avatar to Address (I didn't test this tbh)


My use case is basically like this:

class A {
  // ...
  @AutoMap(() => B)
  b: B[]
}

class B {
  // ...
  @AutoMap(() => C)
  c: C
}

class C {}

class AVm {
  @AutoMap(() => CVm)
  c: CVm[] // I want to extract this from B[]
}

class CVm {}
@micalevisk
Copy link
Contributor Author

I've managed to do this kind of mapping with @nartc/automapper so the migration guide should have some thoughts about that, right? 😕

@micalevisk micalevisk changed the title [core] Can I use mapWith with arrays? [core] How can I use mapWith with arrays? Feb 2, 2021
@nartc nartc closed this as completed in 0eef348 Feb 2, 2021
@nartc
Copy link
Owner

nartc commented Feb 2, 2021

@micalevisk Thanks for reporting. Please try 2.0.1 out

@micalevisk
Copy link
Contributor Author

@nartc it works now! ty

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

No branches or pull requests

2 participants