Skip to content

Commit

Permalink
feat(compiler): allow map to be valid json (#4186)
Browse files Browse the repository at this point in the history
Fixes #3894

Not sure if it was explicitly left out for a reason. For literals, it obviously looks a little ugly, but there's no logical issue as far as I can see.

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
MarkMcCulloh authored Sep 14, 2023
1 parent 7bedd9a commit 3083cc1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/tests/valid/json.main.w
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ bring cloud;
let jsonNumber = Json 123;
let jsonBool = Json true;
let jsonArray = Json [ 1, 2, 3 ];
let jsonMap = Json { "1" => 1, "2" => 2, "3" => 3 };
let jsonObj = Json { boom: 123 };

for j in [jsonNumber, jsonBool, jsonArray, jsonMap, jsonObj] {
assert(j == Json.parse(Json.stringify(j)));
}

let jsonMutObj = MutJson {
hello: 123,
world: [ 1, "cat", 3 ],
Expand Down
1 change: 1 addition & 0 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,7 @@ impl TypeRef {
Type::MutJson | Type::Json(None) => true,
Type::Inferred(..) => true,
Type::Array(v) => v.is_json_legal_value(),
Type::Map(v) => v.is_json_legal_value(),
Type::Optional(v) => v.is_json_legal_value(),
Type::Struct(ref s) => {
for (_, t) in s.fields(true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ class $Root extends $stdlib.std.Resource {
const jsonNumber = 123;
const jsonBool = true;
const jsonArray = [1, 2, 3];
const jsonMap = ({"1": 1,"2": 2,"3": 3});
const jsonObj = ({"boom": 123});
for (const j of [jsonNumber, jsonBool, jsonArray, jsonMap, jsonObj]) {
{((cond) => {if (!cond) throw new Error("assertion failed: j == Json.parse(Json.stringify(j))")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(j,(JSON.parse(((args) => { return JSON.stringify(args[0], null, args[1]?.indent) })([j]))))))};
}
const jsonMutObj = ({"hello": 123,"world": [1, "cat", 3],"boom boom": ({"hello": 1233})});
const message = "Coolness";
((obj, args) => { obj[args[0]] = args[1]; })(jsonMutObj, ["hello",message]);
Expand Down

0 comments on commit 3083cc1

Please sign in to comment.