Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanaburayyan committed Aug 11, 2023
1 parent b57d601 commit 83329e8
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 83 deletions.
2 changes: 0 additions & 2 deletions docs/docs/04-standard-library/02-std/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2090,8 +2090,6 @@ Struct.fromJson(json: Json);

Converts a Json to a Struct.

This macro takes a Json object and the Struct definition file name then calls the validate method

###### `json`<sup>Required</sup> <a name="json" id="@winglang/sdk.std.Struct.fromJson.parameter.json"></a>

- *Type:* <a href="#@winglang/sdk.std.Json">Json</a>
Expand Down
12 changes: 6 additions & 6 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ impl<'a> JSifier<'a> {
// Any parents we need to get their properties
for e in extends {
code.line(format!(
"...require(\"{}\")().getSchema().properties,",
"...require(\"{}\")().jsonSchema().properties,",
struct_filename(&e.root.name)
))
}
Expand Down Expand Up @@ -710,7 +710,7 @@ impl<'a> JSifier<'a> {
let mut required: Vec<String> = vec![]; // fields that are required
let mut dependencies: Vec<String> = vec![]; // schemas that need added to validator

code.open("static getSchema() {".to_string());
code.open("static jsonSchema() {".to_string());
code.open("return {");
code.line(format!("id: \"/{}\",", name));
code.line("type: \"object\",".to_string());
Expand Down Expand Up @@ -740,7 +740,7 @@ impl<'a> JSifier<'a> {
// pull in all required fields from parent structs
for e in extends {
code.line(format!(
"...require(\"{}\")().getSchema().required,",
"...require(\"{}\")().jsonSchema().required,",
struct_filename(&e.root.name)
));
}
Expand All @@ -751,14 +751,14 @@ impl<'a> JSifier<'a> {
code.open("$defs: {");
for dep in &dependencies {
code.line(format!(
"\"{}\": {{ type: \"object\", \"properties\": require(\"{}\")().getSchema().properties }},",
"\"{}\": {{ type: \"object\", \"properties\": require(\"{}\")().jsonSchema().properties }},",
dep,
struct_filename(&dep)
));
}
for e in extends {
code.line(format!(
"...require(\"{}\")().getSchema().$defs,",
"...require(\"{}\")().jsonSchema().$defs,",
struct_filename(&e.root.name)
));
}
Expand All @@ -769,7 +769,7 @@ impl<'a> JSifier<'a> {

// create _validate() function
code.open("static fromJson(obj) {");
code.line("return stdStruct._validate(obj, this.getSchema())");
code.line("return stdStruct._validate(obj, this.jsonSchema())");
code.close("}");

// create _toInflightType function that just requires the generated struct file
Expand Down
5 changes: 4 additions & 1 deletion libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4290,7 +4290,10 @@ impl<'a> TypeChecker<'a> {
}
}
Type::Struct(ref s) => {
if property.name == "fromJson" || property.name == "tryFromJson" {
let from_json = "fromJson";
let try_from_json = "tryFromJson";

if property.name == from_json || property.name == try_from_json {
// we need to validate that only structs with all valid json fields can have a fromJson method
for (name, field) in s.fields(true) {
if !field.has_json_representation() {
Expand Down
2 changes: 0 additions & 2 deletions libs/wingsdk/src/std/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class Struct {
/**
* Converts a Json to a Struct
*
* This macro takes a Json object and the Struct definition file name then calls the validate method
*
* @macro ($self$.fromJson($args$))
*/
public static fromJson(json: Json): T1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```js
module.exports = function(stdStruct, fromInline) {
class CheckHitCountOptions {
static getSchema() {
static jsonSchema() {
return {
id: "/CheckHitCountOptions",
type: "object",
Expand All @@ -21,12 +21,12 @@ module.exports = function(stdStruct, fromInline) {
"count",
],
$defs: {
"Source": { type: "object", "properties": require("./Source.Struct.js")().getSchema().properties },
"Source": { type: "object", "properties": require("./Source.Struct.js")().jsonSchema().properties },
}
}
}
static fromJson(obj) {
return stdStruct._validate(obj, this.getSchema())
return stdStruct._validate(obj, this.jsonSchema())
}
static _toInflightType(context) {
return fromInline(`require("./CheckHitCountOptions.Struct.js")(${ context._lift(stdStruct) })`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```js
module.exports = function(stdStruct, fromInline) {
class Point {
static getSchema() {
static jsonSchema() {
return {
id: "/Point",
type: "object",
Expand All @@ -21,7 +21,7 @@ module.exports = function(stdStruct, fromInline) {
}
}
static fromJson(obj) {
return stdStruct._validate(obj, this.getSchema())
return stdStruct._validate(obj, this.jsonSchema())
}
static _toInflightType(context) {
return fromInline(`require("./Point.Struct.js")(${ context._lift(stdStruct) })`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```js
module.exports = function(stdStruct, fromInline) {
class Cat {
static getSchema() {
static jsonSchema() {
return {
id: "/Cat",
type: "object",
Expand All @@ -21,7 +21,7 @@ module.exports = function(stdStruct, fromInline) {
}
}
static fromJson(obj) {
return stdStruct._validate(obj, this.getSchema())
return stdStruct._validate(obj, this.jsonSchema())
}
static _toInflightType(context) {
return fromInline(`require("./Cat.Struct.js")(${ context._lift(stdStruct) })`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```js
module.exports = function(stdStruct, fromInline) {
class Bar {
static getSchema() {
static jsonSchema() {
return {
id: "/Bar",
type: "object",
Expand All @@ -15,12 +15,12 @@ module.exports = function(stdStruct, fromInline) {
"foo",
],
$defs: {
"Foo": { type: "object", "properties": require("./Foo.Struct.js")().getSchema().properties },
"Foo": { type: "object", "properties": require("./Foo.Struct.js")().jsonSchema().properties },
}
}
}
static fromJson(obj) {
return stdStruct._validate(obj, this.getSchema())
return stdStruct._validate(obj, this.jsonSchema())
}
static _toInflightType(context) {
return fromInline(`require("./Bar.Struct.js")(${ context._lift(stdStruct) })`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```js
module.exports = function(stdStruct, fromInline) {
class Name {
static getSchema() {
static jsonSchema() {
return {
id: "/Name",
type: "object",
Expand All @@ -20,7 +20,7 @@ module.exports = function(stdStruct, fromInline) {
}
}
static fromJson(obj) {
return stdStruct._validate(obj, this.getSchema())
return stdStruct._validate(obj, this.jsonSchema())
}
static _toInflightType(context) {
return fromInline(`require("./Name.Struct.js")(${ context._lift(stdStruct) })`);
Expand All @@ -35,7 +35,7 @@ module.exports = function(stdStruct, fromInline) {
```js
module.exports = function(stdStruct, fromInline) {
class Payload {
static getSchema() {
static jsonSchema() {
return {
id: "/Payload",
type: "object",
Expand All @@ -48,12 +48,12 @@ module.exports = function(stdStruct, fromInline) {
"a",
],
$defs: {
"cloud": { type: "object", "properties": require("./cloud.Struct.js")().getSchema().properties },
"cloud": { type: "object", "properties": require("./cloud.Struct.js")().jsonSchema().properties },
}
}
}
static fromJson(obj) {
return stdStruct._validate(obj, this.getSchema())
return stdStruct._validate(obj, this.jsonSchema())
}
static _toInflightType(context) {
return fromInline(`require("./Payload.Struct.js")(${ context._lift(stdStruct) })`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```js
module.exports = function(stdStruct, fromInline) {
class Point {
static getSchema() {
static jsonSchema() {
return {
id: "/Point",
type: "object",
Expand All @@ -21,7 +21,7 @@ module.exports = function(stdStruct, fromInline) {
}
}
static fromJson(obj) {
return stdStruct._validate(obj, this.getSchema())
return stdStruct._validate(obj, this.jsonSchema())
}
static _toInflightType(context) {
return fromInline(`require("./Point.Struct.js")(${ context._lift(stdStruct) })`);
Expand Down
Loading

0 comments on commit 83329e8

Please sign in to comment.