Skip to content

Commit

Permalink
fix: expected type to be Array<Json>, but got Array<Json> instead (
Browse files Browse the repository at this point in the history
…#6999)

fixes #5421
in addition to the examples in the issue, I added one with `MutMap` (since it was also missing).
I noticed that `Set` and `MutSet` aren't legal `Json` values, is that right @Chriscbr? (or should I add them as well) 

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*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
tsuf239 authored Aug 12, 2024
1 parent e433bb0 commit b6f63b6
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/tests/invalid/json_values.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let arr = MutArray<num>[1, 2, 3];
arr.push(4);

let map = MutMap<str>{"a" => "1"};
map.set("b", "2");

let takeJson = (obj: Json) => {

};

takeJson({ values: arr });
// ^^^^^^^^^^^^^^^ "MutArray<num>" is not a legal JSON value
takeJson({ values: map });
// ^^^^^^^^^^^^^^^ "MutMap<str>" is not a legal JSON value
11 changes: 11 additions & 0 deletions examples/tests/valid/json-types.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let extra = MutArray<Json>["bar"];
let base = [Json "foo"];
base.concat(extra.copy());

let base2 = Array<Json>["foo"];

let obj = {
first: "ok",
second: ["cool"].concat(base2)
};

1 change: 1 addition & 0 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions tools/hangar/__snapshots__/invalid.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3303,6 +3303,26 @@ Test Files 1 failed (1)
Duration <DURATION>"
`;

exports[`json_values.test.w 1`] = `
"error: "MutArray<num>" is not a legal JSON value
--> ../../../examples/tests/invalid/json_values.test.w:11:20
|
11 | takeJson({ values: arr });
| ^^^


error: "MutMap<str>" 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 <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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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 <DURATION>
```

0 comments on commit b6f63b6

Please sign in to comment.