diff --git a/examples/tests/invalid/json_values.test.w b/examples/tests/invalid/json_values.test.w new file mode 100644 index 00000000000..5db74572fa5 --- /dev/null +++ b/examples/tests/invalid/json_values.test.w @@ -0,0 +1,14 @@ +let arr = MutArray[1, 2, 3]; +arr.push(4); + +let map = MutMap{"a" => "1"}; +map.set("b", "2"); + +let takeJson = (obj: Json) => { + +}; + +takeJson({ values: arr }); +// ^^^^^^^^^^^^^^^ "MutArray" is not a legal JSON value +takeJson({ values: map }); +// ^^^^^^^^^^^^^^^ "MutMap" is not a legal JSON value \ No newline at end of file diff --git a/examples/tests/valid/json-types.test.w b/examples/tests/valid/json-types.test.w new file mode 100644 index 00000000000..93f1f315538 --- /dev/null +++ b/examples/tests/valid/json-types.test.w @@ -0,0 +1,11 @@ +let extra = MutArray["bar"]; +let base = [Json "foo"]; +base.concat(extra.copy()); + +let base2 = Array["foo"]; + +let obj = { + first: "ok", + second: ["cool"].concat(base2) +}; + diff --git a/libs/wingc/src/type_check.rs b/libs/wingc/src/type_check.rs index 9427cc7b870..01b20320974 100644 --- a/libs/wingc/src/type_check.rs +++ b/libs/wingc/src/type_check.rs @@ -772,6 +772,7 @@ impl Subtype for Type { (Self::Number, Self::Stringable) => true, (Self::Boolean, Self::Stringable) => true, (Self::Json(_), Self::Stringable) => true, + (Self::Json(_), Self::Json(_)) => true, (Self::MutJson, Self::Stringable) => true, (Self::Enum(_), Self::Stringable) => true, _ => false, diff --git a/tools/hangar/__snapshots__/invalid.ts.snap b/tools/hangar/__snapshots__/invalid.ts.snap index 4c7911553e2..927e744645f 100644 --- a/tools/hangar/__snapshots__/invalid.ts.snap +++ b/tools/hangar/__snapshots__/invalid.ts.snap @@ -3303,6 +3303,26 @@ Test Files 1 failed (1) Duration " `; +exports[`json_values.test.w 1`] = ` +"error: "MutArray" is not a legal JSON value + --> ../../../examples/tests/invalid/json_values.test.w:11:20 + | +11 | takeJson({ values: arr }); + | ^^^ + + +error: "MutMap" is not a legal JSON value + --> ../../../examples/tests/invalid/json_values.test.w:13:20 + | +13 | takeJson({ values: map }); + | ^^^ + +Tests 1 failed (1) +Snapshots 1 skipped +Test Files 1 failed (1) +Duration " +`; + exports[`map_entries.test.w 1`] = ` "error: Expected type to be "str", but got "num" instead --> ../../../examples/tests/invalid/map_entries.test.w:2:20 diff --git a/tools/hangar/__snapshots__/test_corpus/valid/json-types.test.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/json-types.test.w_compile_tf-aws.md new file mode 100644 index 00000000000..bfe427db68e --- /dev/null +++ b/tools/hangar/__snapshots__/test_corpus/valid/json-types.test.w_compile_tf-aws.md @@ -0,0 +1,50 @@ +# [json-types.test.w](../../../../../examples/tests/valid/json-types.test.w) | compile | tf-aws + +## main.tf.json +```json +{ + "//": { + "metadata": { + "backend": "local", + "stackName": "root" + }, + "outputs": {} + }, + "provider": { + "aws": [ + {} + ] + } +} +``` + +## preflight.cjs +```cjs +"use strict"; +const $stdlib = require('@winglang/sdk'); +const $macros = require("@winglang/sdk/lib/macros"); +const $platforms = ((s) => !s ? [] : s.split(';'))(process.env.WING_PLATFORMS); +const $outdir = process.env.WING_SYNTH_DIR ?? "."; +const $wing_is_test = process.env.WING_IS_TEST === "true"; +const std = $stdlib.std; +const $helpers = $stdlib.helpers; +const $extern = $helpers.createExternRequire(__dirname); +const $PlatformManager = new $stdlib.platform.PlatformManager({platformPaths: $platforms}); +class $Root extends $stdlib.std.Resource { + constructor($scope, $id) { + super($scope, $id); + $helpers.nodeof(this).root.$preflightTypesMap = { }; + let $preflightTypesMap = {}; + $helpers.nodeof(this).root.$preflightTypesMap = $preflightTypesMap; + const extra = ["bar"]; + const base = ["foo"]; + (base.concat($macros.__MutArray_copy(false, extra, ))); + const base2 = ["foo"]; + const obj = ({"first": "ok", "second": (["cool"].concat(base2))}); + } +} +const $APP = $PlatformManager.createApp({ outdir: $outdir, name: "json-types.test", rootConstruct: $Root, isTestEnvironment: $wing_is_test, entrypointDir: process.env['WING_SOURCE_DIR'], rootId: process.env['WING_ROOT_ID'] }); +$APP.synth(); +//# sourceMappingURL=preflight.cjs.map +``` + diff --git a/tools/hangar/__snapshots__/test_corpus/valid/json-types.test.w_test_sim.md b/tools/hangar/__snapshots__/test_corpus/valid/json-types.test.w_test_sim.md new file mode 100644 index 00000000000..5bed517f057 --- /dev/null +++ b/tools/hangar/__snapshots__/test_corpus/valid/json-types.test.w_test_sim.md @@ -0,0 +1,12 @@ +# [json-types.test.w](../../../../../examples/tests/valid/json-types.test.w) | test | sim + +## stdout.log +```log +pass ─ json-types.test.wsim (no tests) + +Tests 1 passed (1) +Snapshots 1 skipped +Test Files 1 passed (1) +Duration +``` +