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(compiler): descriptions in generated JSON schemas #7190

Merged
merged 9 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions docs/by-example/32-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ image: /img/wing-by-example.png

Wing incorporates a [lightweight testing framework](/docs/concepts/tests), which is built around the `wing test` command and the `test` keyword.

```js playground example title="main.w"
```js playground example{valid: false} title="main.w"
bring math;
bring cloud;
let b = new cloud.Bucket();
Expand All @@ -31,7 +31,7 @@ test "bucket starts empty" {
}

test "this test fails" {
throw("test throws an exception fails");
throw "test throws an exception fails";
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/02-concepts/04-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ test "bucket starts empty" {
}

test "this test should fail" {
throw("test throws an exception fails");
throw "test throws an exception fails";
}
```

Expand Down
5 changes: 4 additions & 1 deletion packages/@winglang/sdk/scripts/docgen.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readdir, writeFile, readFile } from "node:fs/promises";
import { readdir, writeFile, readFile, rm } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Language, Documentation } from "@winglang/jsii-docgen";
Expand Down Expand Up @@ -184,6 +184,9 @@ await generateResourceApiDocs("target-sim", {
jsiiModule: "sim",
});

// TODO: this file isn't generated correctly
await rm(getStdlibDocsDir("std/reflect") + ".md");

console.log(
`${ANSI_LINE_CLEAR}${ANSI_LINE_CLEAR}${docCounter} Docs Generated!`
);
Original file line number Diff line number Diff line change
Expand Up @@ -849,28 +849,3 @@ exports[`removing a key will call onDelete method 1`] = `
"root/my_bucket/OnDelete/OnMessage0 stopped",
]
`;

exports[`update an object in bucket 1`] = `
[
"root/my_bucket/OnCreate started",
"Server listening on http://127.0.0.1:<port>",
"root/my_bucket started",
"root/my_bucket/Policy started",
"root/my_bucket/OnCreate/OnMessage0 started",
"root/my_bucket/OnCreate/Policy started",
"root/my_bucket/OnCreate/TopicEventMapping0 started",
"Sending message (message=1.txt, subscriber=sim-3).",
"InvokeAsync (payload="1.txt").",
"Publish (messages=1.txt).",
"Put (key=1.txt).",
"Put (key=1.txt).",
"I am done",
"root/my_bucket/Policy stopped",
"Closing server on http://127.0.0.1:<port>",
"root/my_bucket stopped",
"root/my_bucket/OnCreate/Policy stopped",
"root/my_bucket/OnCreate/TopicEventMapping0 stopped",
"root/my_bucket/OnCreate stopped",
"root/my_bucket/OnCreate/OnMessage0 stopped",
]
`;
1 change: 0 additions & 1 deletion packages/@winglang/sdk/test/target-sim/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ test("update an object in bucket", async () => {

// THEN
await s.stop();
expect(listMessages(s)).toMatchSnapshot();
// The bucket notification topic should only publish one message, since the
// second put() call counts as an update, not a create.
expect(listMessages(s).filter((m) => m.includes(`Publish`))).toHaveLength(1);
Expand Down
46 changes: 45 additions & 1 deletion packages/@winglang/wingc/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use regex::Regex;
use crate::{
ast::{AccessModifier, Phase},
closure_transform::CLOSURE_CLASS_PREFIX,
jsify::codemaker::CodeMaker,
jsify::{codemaker::CodeMaker, escape_javascript_string},
type_check::{
jsii_importer::is_construct_base, symbol_env::SymbolEnvKind, Class, ClassLike, Enum, FunctionSignature, Interface,
Namespace, Struct, SymbolKind, Type, TypeRef, VariableInfo, VariableKind, CLASS_INFLIGHT_INIT_NAME,
Expand Down Expand Up @@ -47,6 +47,50 @@ impl Docs {
markdown.to_string().trim().to_string()
}

pub fn to_escaped_string(&self) -> String {
let mut contents = String::new();
if let Some(summary) = &self.summary {
contents.push_str(summary);
}
if let Some(remarks) = &self.remarks {
contents.push_str("\n\n");
contents.push_str(remarks);
}
if let Some(example) = &self.example {
contents.push_str("\n@example ");
contents.push_str(example);
}
if let Some(returns) = &self.returns {
contents.push_str("\n@returns ");
contents.push_str(returns);
}
if let Some(deprecated) = &self.deprecated {
contents.push_str("\n@deprecated ");
contents.push_str(deprecated);
}
if let Some(see) = &self.see {
contents.push_str("\n@see ");
contents.push_str(see);
}
if let Some(default) = &self.default {
contents.push_str("\n@default ");
contents.push_str(default);
}
if let Some(stability) = &self.stability {
contents.push_str("\n@stability ");
contents.push_str(stability);
}
if let Some(_) = &self.subclassable {
contents.push_str("\n@subclassable");
}
for (k, v) in self.custom.iter() {
contents.push_str(&format!("\n@{} ", k));
contents.push_str(v);
}

escape_javascript_string(&contents)
}

pub(crate) fn with_summary(summary: &str) -> Docs {
Docs {
summary: Some(summary.to_string()),
Expand Down
4 changes: 2 additions & 2 deletions packages/@winglang/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl<'a> JSifier<'a> {

code.line(format!(
"const {flat_name} = $stdlib.std.Struct._createJsonSchema({});",
schema_code.to_string().replace("\n", "").replace(" ", "")
schema_code.to_string()
));
}
code
Expand Down Expand Up @@ -2704,7 +2704,7 @@ fn lookup_span(span: &WingSpan, files: &Files) -> String {
result
}

fn escape_javascript_string(s: &str) -> String {
pub fn escape_javascript_string(s: &str) -> String {
let mut result = String::new();

// escape all escapable characters -- see the section "Escape sequences" in
Expand Down
117 changes: 76 additions & 41 deletions packages/@winglang/wingc/src/json_schema_generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
docs::Docs,
jsify::{codemaker::CodeMaker, JSifier},
type_check::{symbol_env::SymbolEnv, Struct, Type, UnsafeRef},
};
Expand All @@ -13,107 +14,141 @@ impl JsonSchemaGenerator {
fn get_struct_env_properties(&self, env: &SymbolEnv) -> CodeMaker {
let mut code = CodeMaker::default();
for (field_name, entry) in env.symbol_map.iter() {
code.line(format!(
"{}: {},",
let var_info = entry.kind.as_variable().unwrap();
let docs = var_info.docs.clone();
code.append(format!(
"{}:{},",
field_name,
self.get_struct_schema_field(&entry.kind.as_variable().unwrap().type_)
self.get_struct_schema_field(&entry.kind.as_variable().unwrap().type_, docs)
));
}
code
}

fn get_struct_schema_required_fields(&self, env: &SymbolEnv) -> CodeMaker {
let mut code = CodeMaker::default();
code.open("required: [");
code.append("required:[");
for (field_name, entry) in env.symbol_map.iter() {
if !matches!(*entry.kind.as_variable().unwrap().type_, Type::Optional(_)) {
code.line(format!("\"{}\",", field_name));
code.append(format!("\"{}\",", field_name));
}
}
code.close("]");
code.append("]");
code
}

fn get_struct_schema_field(&self, typ: &UnsafeRef<Type>) -> String {
fn get_struct_schema_field(&self, typ: &UnsafeRef<Type>, docs: Option<Docs>) -> String {
match **typ {
Type::String | Type::Number | Type::Boolean => {
format!("{{ type: \"{}\" }}", JSifier::jsify_type(typ).unwrap())
let jsified_type = JSifier::jsify_type(typ).unwrap();
match docs {
Some(docs) => format!(
"{{type:\"{}\",description:\"{}\"}}",
jsified_type,
docs.to_escaped_string()
),
None => format!("{{type:\"{}\"}}", jsified_type),
}
}
Type::Struct(ref s) => {
let mut code = CodeMaker::default();
code.open("{");
code.line("type: \"object\",");
code.open("properties: {");
code.add_code(self.get_struct_env_properties(&s.env));
code.close("},");
code.add_code(self.get_struct_schema_required_fields(&s.env));
code.close("}");
code.append("{");
code.append("type:\"object\",");
code.append("properties:{");
code.append(self.get_struct_env_properties(&s.env));
code.append("},");
code.append(self.get_struct_schema_required_fields(&s.env));
let docs = docs.unwrap_or(s.docs.clone());
code.append(format!(",description:\"{}\"", docs.to_escaped_string()));
code.append("}");
code.to_string()
}
Type::Array(ref t) | Type::Set(ref t) => {
let mut code = CodeMaker::default();
code.open("{");
code.append("{");

code.line("type: \"array\",");
code.append("type:\"array\"");

if matches!(**typ, Type::Set(_)) {
code.line("uniqueItems: true,");
code.append(",uniqueItems:true");
}

code.line(format!("items: {}", self.get_struct_schema_field(t)));
code.append(format!(",items:{}", self.get_struct_schema_field(t, None)));

if let Some(docs) = docs {
code.append(format!(",description:\"{}\"", docs.to_escaped_string()));
}

code.close("}");
code.append("}");
code.to_string()
}
Type::Map(ref t) => {
let mut code = CodeMaker::default();
code.open("{");
code.append("{");

code.line("type: \"object\",");
code.line(format!(
"patternProperties: {{ \".*\": {} }}",
self.get_struct_schema_field(t)
code.append("type:\"object\",");
code.append(format!(
"patternProperties: {{\".*\":{}}}",
self.get_struct_schema_field(t, None)
));

code.close("}");
if let Some(docs) = docs {
code.append(format!("description:\"{}\",", docs.to_escaped_string()));
}

code.append("}");
code.to_string()
}
Type::Optional(t) => self.get_struct_schema_field(&t),
Type::Json(_) => "{ type: [\"object\", \"string\", \"boolean\", \"number\", \"array\"] }".to_string(),
Type::Optional(ref t) => self.get_struct_schema_field(t, docs),
Type::Json(_) => match docs {
Some(docs) => format!(
"{{type:[\"object\",\"string\",\"boolean\",\"number\",\"array\"],description:\"{}\"}}",
docs.to_escaped_string()
),
None => "{type:[\"object\",\"string\",\"boolean\",\"number\",\"array\"]}".to_string(),
},
Type::Enum(ref enu) => {
let choices = enu
.values
.iter()
.map(|(s, _)| format!("\"{}\"", s))
.collect::<Vec<String>>()
.join(", ");
format!("{{ type: \"string\", enum: [{}] }}", choices)
let docs = docs.unwrap_or(enu.docs.clone());
format!(
"{{type:\"string\",enum:[{}],description:\"{}\"}}",
choices,
docs.to_escaped_string()
)
}
_ => "{ type: \"null\" }".to_string(),
_ => match docs {
Some(docs) => format!("{{type:\"null\",description:\"{}\" }}", docs.to_escaped_string()),
None => "{type:\"null\"}".to_string(),
},
}
}

pub fn create_from_struct(&self, struct_: &Struct) -> CodeMaker {
let mut code = CodeMaker::default();

code.open("{");
code.line(format!("$id: \"/{}\",", struct_.name));
code.line("type: \"object\",".to_string());
code.append("{");
code.append(format!("$id:\"/{}\",", struct_.name));
code.append("type:\"object\",".to_string());

code.open("properties: {");
code.append("properties:{");

code.add_code(self.get_struct_env_properties(&struct_.env));
code.append(self.get_struct_env_properties(&struct_.env));

//close properties
code.close("},");
code.append("},");

code.add_code(self.get_struct_schema_required_fields(&struct_.env));
code.append(self.get_struct_schema_required_fields(&struct_.env));

// close schema
code.close("}");
code.append(format!(",description:\"{}\"", struct_.docs.to_escaped_string()));

let cleaned = code.to_string().replace("\n", "").replace(" ", "");
// close schema
code.append("}");

CodeMaker::one_line(cleaned)
code
}
}
2 changes: 1 addition & 1 deletion tests/doc_examples/invalid/04-tests.md_example_5/main.w

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/doc_examples/invalid/32-testing.md_example_1/main.w

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/doc_examples/valid/09-arrays.md_example_1/main.w

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading