-
Notifications
You must be signed in to change notification settings - Fork 196
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
Generics support #104
Comments
Upgrades project dependencies. See details in [workflow run]. [Workflow Run]: https://github.com/monadahq/wingsdk/actions/runs/3075234159 ------ *Automatically created by projen via the "upgrade-main" workflow*
Hi, This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. |
Keep |
Hi, This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. |
Some use cases: Input ValidationCloud functions need to validate input if they can be invoke from the outside. After validating the input, the function can safely assume that the input is correct and can be used without further checks. Let's use Zod as an example: bring cloud;
bring zod;
let InputSchema = zod.object({
name: zod.string(),
age: zod.number(),
});
new cloud.Function(inflight (input: unknown) => {
let user = InputSchema.parse(input);
log(user.username);
log(user.age);
}); A different syntax example: bring cloud;
bring zod;
new cloud.Function({
input: zod.object({
name: zod.string(),
age: zod.number(),
}),
handler: inflight (input) => {
// ^? input is inferred to be the type of the input schema
log(input.name);
log(input.age);
},
}); Type-safe Database QueriesIf we can defined what shape of data belongs to a queue or a table, user code will be type safe: bring cloud;
interface User {
id: string;
name: string;
age: number;
}
let users = new cloud.Table<User>(
name: "users",
primaryKey: "id", // Should be prompted by the IDE with "id", "name" and "age"
);
new cloud.Function(inflight () => {
// Can't really add invalid data thanks to the type system.
users.put({
id: "1",
name: "John",
age: 20,
});
}); Enable type-safe Single-Table DesignLearn about Single-Table Design here: https://www.alexdebrie.com/posts/dynamodb-single-table/. A feature like this would require Template Literal Types, bring cloud;
interface UserItem {
pk: `user#${string}`;
userId: string;
name: string;
age: number;
}
interface ProjectItem {
pk: `project#${string}`;
projectId: string;
name: string;
createdAt: number;
}
let table = new cloud.Table<UserItem | ProjectItem>(
name: "table",
primaryKey: "pk",
);
new cloud.Function(inflight () => {
let userId = "user_1";
table.put({
pk: "user#${userId}",
userId: userId,
name: "John",
age: 20,
});
let projectId = "project_1";
table.put({
pk: "project#${projectId}",
projectId: projectId,
name: "Project 1",
createdAt: Date.now(),
});
}); Avoid awkward manual JSON handlingWith generics enabling to define the shape of the data in constructs, there's no need to work with the |
@skyrpex It might be possible to handle several schema validation use cases through bring cloud;
struct Person {
name: str;
age: num;
}
let fn = new cloud.Function(inflight (input: Json) => {
let person = Person.fromJson(input);
log(person.name);
log(person.age);
return person;
); |
Yes but this has some problems:
|
I've been thinking more about the idea of changing the type of the handler you pass to Compare these two cloud functions that accept and return a number/id -- they both require a similar amount of input/output parsing: let fn1 = new cloud.Function(inflight (input: str): str => {
let var val = num.fromStr(input);
val += 1;
return "{val}";
});
let fn2 = new cloud.Function(inflight (input: Json): Json? => {
let var val = num.fromJson(input);
val += 1;
return Json val;
}); And of course, you also have to convert between For now, I'd recommend using wrappers like this:
|
Supporting Generics in Wing could be used to provide useful errors between preflight and inflight code:
To support JSII interoperability, I'd propose that during compilation we might be able to perform type erasure like Java that convert type paramters like
T
intoany
, and add JSII metadata that details any of the erased type information.The text was updated successfully, but these errors were encountered: