Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr committed Sep 27, 2024
1 parent 683cc63 commit 2281567
Showing 1 changed file with 66 additions and 0 deletions.
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"]}
```

0 comments on commit 2281567

Please sign in to comment.