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

Convert Nested Object #15

Open
andreluiz1987 opened this issue Jun 9, 2022 · 0 comments
Open

Convert Nested Object #15

andreluiz1987 opened this issue Jun 9, 2022 · 0 comments

Comments

@andreluiz1987
Copy link

Hi.
Congratulations on the lib, I was looking for a solution similar to Java's mapstruct for typeScript and I found yours.

I have a problem and maybe you can help me. When I have a nested object I can't mapper correctly.

My code:

class Event {
    event_id: string = "xpto";
    product: Product = new Product;
}

class Product {
    id: Number = 0;
    name: string = "any-name";
}

class Template {
    id: Number = 0;
    name: string = "";
}

@Mapper({ sourceType: Product, targetType: Template }, [
    { source: "name", target: "name" }
])
class ProductToTemplateMapper extends Converter<Product, Template> {}

@Mapper({ sourceType: Event, targetType: Template }, [
    { source: "event_id", target: "id"},
    { source: "product", target: "name", converter: ProductToTemplateMapper}
])
class EventToTemplateMapper extends Converter<Event, Template> {}

const event = new Event();
const mapper = new EventToTemplateMapper();
const template = mapper.convert(event);
console.log(template);

the result is this:
{ id: 'xpto', name: Template { id: 0, name: 'any-name' } }

but I would like it to be:
{ id: 'xpto', name: 'any-name' } }

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

1 participant