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: @type intrinsic #7151

Merged
merged 31 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9b4c38e
simplify grammar
Chriscbr Sep 20, 2024
9360014
midwork
Chriscbr Sep 20, 2024
bb4e91e
add test case
Chriscbr Sep 20, 2024
371a839
cleaner test
Chriscbr Sep 20, 2024
be5634c
add more examples, fix bug
Chriscbr Sep 23, 2024
eeeb87f
add more simple cases
Chriscbr Sep 23, 2024
ffb125a
resolve user defined type info
Chriscbr Sep 23, 2024
5e82477
midwork
Chriscbr Sep 25, 2024
c153689
test isStatic
Chriscbr Sep 25, 2024
4207542
update vscode dev script
Chriscbr Sep 25, 2024
9c15912
implement toInflight for various types
Chriscbr Sep 25, 2024
6ec10e4
fix missing new keywords
Chriscbr Sep 25, 2024
7e4318a
fix bugs
Chriscbr Sep 25, 2024
758c350
update comments
Chriscbr Sep 25, 2024
cc47bda
improve generated names
Chriscbr Sep 25, 2024
f93f732
add missing docstrings
Chriscbr Sep 26, 2024
afa5880
missing docstrings
Chriscbr Sep 26, 2024
0fb7344
add findAllTypeElements
Chriscbr Sep 26, 2024
a7ac21f
fix inflight lifting
Chriscbr Sep 27, 2024
3c3de0d
move types into std.reflect
Chriscbr Sep 27, 2024
011bb15
Merge branch 'main' into rybickic/type-intrinsic
Chriscbr Sep 27, 2024
2387785
update comment, fix typescript warnings
Chriscbr Sep 27, 2024
4a633da
update comment
Chriscbr Sep 27, 2024
c9c729b
fix multiple emit error
Chriscbr Sep 27, 2024
6523380
better fix
Chriscbr Sep 27, 2024
b4b441f
fix test
Chriscbr Sep 27, 2024
fcd249e
chore: self mutation (e2e-1of2.diff)
monadabot Sep 27, 2024
1d79c24
chore: self mutation (e2e-2of2.diff)
monadabot Sep 27, 2024
683cc63
reorder methods
Chriscbr Sep 27, 2024
2281567
add docs
Chriscbr Sep 27, 2024
441837c
chore: self mutation (build.diff)
monadabot Sep 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/by-example/36-reflection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: Type reflection
id: type-reflection
slug: /type-reflection
sidebar_label: Type reflection
description: Type reflection
keywords: [Wing language, Type reflection]
image: /img/wing-by-example.png
---

The `@type` intrinsic function returns a reflection of the type passed to it.

You can access its `.kind` property to get the kind of the type, and use various helper methods like `.asStruct()`, `.asClass()`, `.asInterface()`, etc. to access related properties.

```js playground example title="main.w"
let generateJsonSchema = (structType: std.reflect.Type): str => {
if let st = structType.asStruct() {
let schema = MutJson {
type: "object",
properties: {},
required: []
};

for name in st.fields.keys() {
let fieldSchema = MutJson {};
let var field = st.fields[name].child;
let var required = true;
if let opt = field.asOptional() {
required = false;
field = opt.child;
}

if field.kind == "str" {
fieldSchema["type"] = "string";
} else if field.kind == "num" {
fieldSchema["type"] = "number";
} // ...handle other types

schema["properties"][name] = fieldSchema;
if required {
// TODO: https://github.com/winglang/wing/issues/6929
unsafeCast(schema["required"])?.push(name);
}
}

return Json.stringify(schema);
}

throw "input must be a struct type";
};

struct User {
name: str;
age: num;
email: str?;
}

log(generateJsonSchema(@type(User)));
```

```bash title="Wing console output"
# Run locally with wing console
{"type":"object","properties":{"age":{"type":"number"},"email":{"type":"string"},"name":{"type":"string"}},"required":["age","name"]}
```


2 changes: 2 additions & 0 deletions packages/@winglang/sdk/src/std/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export * from "./string";
export * from "./struct";
export * from "./test";
export * from "./test-runner";

export * as reflect from "./reflect";
1 change: 1 addition & 0 deletions packages/@winglang/sdk/src/std/reflect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./type";
Loading