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

New Function to Perform Class Transformation #683

Open
benoit-scnd opened this issue Jun 26, 2023 · 4 comments
Open

New Function to Perform Class Transformation #683

benoit-scnd opened this issue Jun 26, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@benoit-scnd
Copy link

Feature Request

A new function named classTransform that converts a given input into an instance of a specific class.

The function could have the following signature:

typia.classTransform<T>(input: Primitive<T>): T;

This function takes two arguments:

  • input: The primitive input to be converted.
  • T: The target class that the input should be transformed into.

The function should return an instance of the class T with the given input.

This function would be particularly useful for converting raw input data into class instances. This issue is critical as it prevents proper type validation and transformation of the incoming request body, a common requirement in type-safe server-side applications. (see samchon/nestia#431)

Example Usage

Here is an example of how this function could be used:

class Foo {
  constructor(public value: string) {}

  bar() {
    return this.value;
  }
}

const input = {value: "test"};
const transformed = typia.classTransform<Test>(input);
console.log(transformed.bar()); // Should log: "test"
@benoit-scnd
Copy link
Author

@samchon ,

Here is the way to do a nested conversion with the package class-transformer:

import { Type, plainToClass } from 'class-transformer';

class Bar {
  value: string;
}

class Foo {
  @Type(() => Bar)
  bar: Bar;
}

const foo = plainToClass(Foo, { bar: { value: 'test' } });

So the challenge is to find a way to guess the type without decorator and rewrite the plainToClass function to obtain better performances.

@samchon
Copy link
Owner

samchon commented Jun 29, 2023

Will try it but cannot sure 100% success

@samchon samchon self-assigned this Jun 29, 2023
@samchon samchon added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jun 29, 2023
@marcesengel
Copy link

This would be very much appreciated - would it be possible for you to suggest the different components needed for this and to tell us where you'd like them to be integrated? That'd make it a lot easier to create a reasonable PR without having to read/understand the entire codebase first.

Maybe a short introduction to the workings of the different components (folders) would also do. Thanks!

@samchon
Copy link
Owner

samchon commented Jul 30, 2023

@marcesengel Implementation code of clone() may be helpful, and seems like almost same with this feature.

Looking at below codes, then you may understand how typia's function has been developed.

If you want to implement this feature by yourself, add classify<T>() function declarations in module.ts file, and implement your logic on a new file ClasstifyProgrammer.ts. After that, link module.ts file's function declaration to the ClassifyProgrammer by adding ClassifyTransformer from CallExpressionTransformer.ts.

Additional functions like assertClassify(), you can make it easily just by combining like AssertCloneProgrammer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants