From 3083cc10d3df56f973b8af00a2e48234df9e7a9f Mon Sep 17 00:00:00 2001 From: Mark McCulloh Date: Thu, 14 Sep 2023 12:41:58 -0400 Subject: [PATCH] feat(compiler): allow map to be valid json (#4186) 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)*. --- examples/tests/valid/json.main.w | 6 ++++++ libs/wingc/src/type_check.rs | 1 + .../test_corpus/valid/json.main.w_compile_tf-aws.md | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/examples/tests/valid/json.main.w b/examples/tests/valid/json.main.w index 8d9d2de4558..9cd8d710eec 100644 --- a/examples/tests/valid/json.main.w +++ b/examples/tests/valid/json.main.w @@ -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 ], diff --git a/libs/wingc/src/type_check.rs b/libs/wingc/src/type_check.rs index 969c7785c68..ad776ccd667 100644 --- a/libs/wingc/src/type_check.rs +++ b/libs/wingc/src/type_check.rs @@ -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) { diff --git a/tools/hangar/__snapshots__/test_corpus/valid/json.main.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/json.main.w_compile_tf-aws.md index c24ad4c649f..d62788c9d38 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/json.main.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/json.main.w_compile_tf-aws.md @@ -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]);