Skip to content

Commit

Permalink
feat(compiler)!: change map syntax to use => (#3018)
Browse files Browse the repository at this point in the history
Makes `{}` default to Json as well as changes maps to use `=>` syntax

Closes: #2291

BREAKING CHANGE: `std.Map` syntax now uses `=>` instead of `:`

## Checklist

- [x] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
  • Loading branch information
hasanaburayyan authored Jun 29, 2023
1 parent 33d83ba commit e71cac1
Show file tree
Hide file tree
Showing 44 changed files with 283 additions and 151 deletions.
6 changes: 3 additions & 3 deletions apps/wing-console/console/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"package": "bump-pack -b"
},
"dependencies": {
"@wingconsole/server": "file:../server",
"analytics-node": "^6.2.0",
"express": "^4.18.2"
"express": "^4.18.2",
"@wingconsole/server": "file:../server"
},
"devDependencies": {
"@tailwindcss/forms": "^0.5.3",
Expand Down Expand Up @@ -48,4 +48,4 @@
"vite": "^4.3.9",
"vitest": "^0.31.4"
}
}
}
2 changes: 1 addition & 1 deletion apps/wing-console/console/design-system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@
"vitest": "^0.31.4",
"webpack": "^5.86.0"
}
}
}
6 changes: 3 additions & 3 deletions apps/wing-console/console/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@trpc/client": "^10.30.0",
"@trpc/react-query": "^10.30.0",
"@trpc/server": "^10.30.0",
"@wingconsole/design-system": "file:../design-system",
"classnames": "^2.3.2",
"d3-selection": "^3.0.0",
"d3-zoom": "^3.0.0",
Expand All @@ -45,7 +44,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-lottie-player": "^1.5.4",
"zod": "^3.21.4"
"zod": "^3.21.4",
"@wingconsole/design-system": "file:../design-system"
},
"devDependencies": {
"@babel/core": "^7.22.5",
Expand Down Expand Up @@ -80,4 +80,4 @@
"vitest": "^0.31.4",
"webpack": "^5.86.0"
}
}
}
4 changes: 2 additions & 2 deletions examples/proposed/url-shortener.w
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class UrlShortenerApi {
return cloud.ApiResponse {
status: 302,
headers: Map<str>{
"Content-Type": "text/xml",
Location: fullUrl
"Content-Type" => "text/xml",
Location => fullUrl
}
};
} else {
Expand Down
6 changes: 3 additions & 3 deletions examples/tests/invalid/container_types.w
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ let arr4: Array<num> = arr3;
arr1.someRandomMethod();

//Map tests
let m1: Map<num> = {"a":1, "b":"2", "c":3};
let m2: Map<num> = ["a":1, "b":"2", "c":3];
let m3 = Map<str>{"h":"h"};
let m1: Map<num> = {"a" => 1, "b" => "2", "c" => 3};
let m2: Map<num> = ["a" => 1, "b" => "2", "c" => 3];
let m3 = Map<str>{"h" => "h"};
let m4: Map<num> = m3;
m1.someRandomMethod();

Expand Down
2 changes: 1 addition & 1 deletion examples/tests/invalid/immutable_container_types.w
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let m1 = Map<str>{"a": "hi"};
let m1 = Map<str>{"a" => "hi"};

m1.set("a", "bye");
// ^^^ Unknown symbol "set" (TODO: better error message https://github.com/winglang/wing/issues/1660)
6 changes: 3 additions & 3 deletions examples/tests/invalid/mut_container_types.w
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ let s5: MutSet<num> = s4;
s3.someMethod();

//Map tests
let m1 = MutMap<num>{"hello": "world"};
let m1 = MutMap<num>{"hello" => "world"};
// ^^^^^^^^ Expected type to be "num", but got "str" instead
let m2 = MutMap<str>["hello", "world"];
// ^^^^^^^^^^^ Expected "Array" type, found "MutMap<str>"
let m3: MutMap<num> = {"hello": "world"};
let m3: MutMap<num> = {"hello" => "world"};
// ^^^^^^^^^^^^^^^^^^ Expected type to be "MutMap<num>", but got "Map<str>" instead
let m4 = MutMap<num>{ "hello": 123 };
let m4 = MutMap<num>{ "hello" => 123 };
let m5: MutMap<str> = m4;
// ^^ Expected type to be "MutMap<str>", but got "MutMap<num>" instead
2 changes: 1 addition & 1 deletion examples/tests/sdk_tests/api/delete.w
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ api.delete("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
test "http.delete and http.fetch can preform a call to an api" {
let url = "${api.url}/path?all=true&page=6";
let response: http.Response = http.delete(url);
let fetchResponse: http.Response = http.fetch(url, {method: http_DELETE});
let fetchResponse: http.Response = http.fetch(url, {"method" => http_DELETE});

// TODO: adding a fetch request when the enums

Expand Down
6 changes: 3 additions & 3 deletions examples/tests/sdk_tests/api/get.w
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ api.get("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {

test "http.get and http.fetch can preform a call to an api" {
let url = api.url + "/path";
let getResponse: http.Response = http.get(url, { headers: { "content-type": "application/json" }});
let fetchResponse: http.Response = http.fetch(url, http.RequestOptions { method: http_GET, headers: { "content-type": "application/json" }});
let fetchResponseNoMethod: http.Response = http.fetch(url, http.RequestOptions { headers: { "content-type": "application/json" }});
let getResponse: http.Response = http.get(url, headers: { "content-type" => "application/json" });
let fetchResponse: http.Response = http.fetch(url, http.RequestOptions { method: http_GET, headers: { "content-type" => "application/json" }});
let fetchResponseNoMethod: http.Response = http.fetch(url, http.RequestOptions { headers: { "content-type" => "application/json" }});


assert(getResponse.body == body);
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/sdk_tests/api/patch.w
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ api.patch("/path/{id}", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {

test "http.patch and http.fetch can preform a call to an api" {
let url = "${api.url}/path/${_id}";
let response: http.Response = http.patch(url, http.RequestOptions { headers: { "content-type": "application/json" }, body: Json.stringify(body)});
let fetchResponse: http.Response = http.patch(url, http.RequestOptions {method: http_PATCH, headers: { "content-type": "application/json" }, body: Json.stringify(body)});
let response: http.Response = http.patch(url, http.RequestOptions { headers: { "content-type" => "application/json" }, body: Json.stringify(body)});
let fetchResponse: http.Response = http.patch(url, http.RequestOptions {method: http_PATCH, headers: { "content-type" => "application/json" }, body: Json.stringify(body)});

assert(response.body == _id);
assert(response.status == 200);
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/sdk_tests/api/post.w
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ api.post("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {

test "http.post and http.fetch can preform a call to an api" {
let url = api.url + "/path";
let response: http.Response = http.post(url, http.RequestOptions { headers: { "content-type": "application/json" }, body: Json.stringify(body)});
let fetchResponse: http.Response = http.post(url, http.RequestOptions {method: http_POST, headers: { "content-type": "application/json" }, body: Json.stringify(body)});
let response: http.Response = http.post(url, http.RequestOptions { headers: { "content-type" => "application/json" }, body: Json.stringify(body)});
let fetchResponse: http.Response = http.post(url, http.RequestOptions {method: http_POST, headers: { "content-type" => "application/json" }, body: Json.stringify(body)});

assert(response.body == Json.stringify(body));
assert(response.status == 200);
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/sdk_tests/api/put.w
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ api.put("/path/{id}/nn/{user}", inflight (req: cloud.ApiRequest): cloud.ApiRespo

test "http.put and http.fetch can preform a call to an api" {
let url = "${api.url}/path/${_id}/nn/${user}";
let response: http.Response = http.put(url, http.RequestOptions { headers: { "content-type": "application/json" }, body: Json.stringify(body)});
let fetchResponse: http.Response = http.put(url, http.RequestOptions { method: http_PUT, headers: { "content-type": "application/json" }, body: Json.stringify(body)});
let response: http.Response = http.put(url, http.RequestOptions { headers: { "content-type" => "application/json" }, body: Json.stringify(body)});
let fetchResponse: http.Response = http.put(url, http.RequestOptions { method: http_PUT, headers: { "content-type" => "application/json" }, body: Json.stringify(body)});


assert(response.body == _id);
Expand Down
8 changes: 4 additions & 4 deletions examples/tests/sdk_tests/bucket/events.w
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ let table = new cloud.Table(
name: "key-history",
primaryKey: "_id",
columns: {
_id: cloud.ColumnType.STRING,
key: cloud.ColumnType.STRING,
operation: cloud.ColumnType.STRING,
source: cloud.ColumnType.STRING,
"_id" => cloud.ColumnType.STRING,
"key" => cloud.ColumnType.STRING,
"operation" => cloud.ColumnType.STRING,
"source" => cloud.ColumnType.STRING,
}
);

Expand Down
2 changes: 1 addition & 1 deletion examples/tests/sdk_tests/function/memory_and_env.w
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let f1 = new cloud.Function(inflight () => {
let f2 = new cloud.Function(inflight () => {
c.inc();
// TODO: add assertion to the env here- when util.env could be called inflight
}, cloud.FunctionProps { env: { catName: "Tion" }}) as "env fn";
}, env: { "catName" => "Tion" }) as "env fn";

f2.addEnvironment("catAge", "2");
assert(f2.env.get("catAge") == "2");
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/sdk_tests/std/map.w
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//-----------------------------------------------------------------------------
// keys()
let m = { "hello": 123, "world": 99 };
let m = { "hello" => 123, "world" => 99 };
let mkeys = m.keys();
assert(mkeys.length == 2);
assert(mkeys.at(0) == "hello");
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/sdk_tests/table/add_row.w
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bring cloud;
let table = new cloud.Table(
name: "users",
primaryKey: "name",
columns: { gender: cloud.ColumnType.STRING }
columns: { "gender" => cloud.ColumnType.STRING }
);

let marioInfo = Json { gender: "male", role: "plumber" };
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/sdk_tests/table/list.w
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ bring cloud;
let table = new cloud.Table(
name: "users",
primaryKey: "name",
columns: { gender: cloud.ColumnType.STRING }
columns: { "gender" => cloud.ColumnType.STRING }
);


Expand Down
2 changes: 1 addition & 1 deletion examples/tests/valid/api.w
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let handler = inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
let resp = cloud.ApiResponse {
body: Json.stringify(bodyResponse),
headers: {
"content-type": "application/json"
"content-type" => "application/json"
},
status: 200,
};
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/valid/api_path_vars.w
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let api = new cloud.Api();
let handler = inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
return cloud.ApiResponse {
body: Json.stringify({ user: req.vars.get("name") }),
headers: { "content-type": "application/json" },
headers: { "content-type" => "application/json" },
status: 200
};
};
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/valid/capture_containers.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ bring cloud;

let arr = ["hello", "world"];
let mySet = {"my", "my", "set"};
let myMap = {"hello": 123, "world": 999};
let arrOfMap = [{"bang": 123}];
let myMap = {"hello" => 123, "world" => 999};
let arrOfMap = [{"bang" => 123}];
let j = Json {a: "hello", b: "world"};

test "capture_containers" {
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/valid/capture_mutables.w
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let a = MutArray<str>["hello"];
let s = MutSet<num>{12};
let m = MutMap<bool>{"hello": true};
let m = MutMap<bool>{"hello" => true};

let aCloned = (Array<str>["hello"]).copyMut();

Expand Down
16 changes: 8 additions & 8 deletions examples/tests/valid/container_types.w
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,30 @@ let emptyMap = Map<num>{};
assert(emptyMap.size() == 0);
let emptyMap2 = MutMap<num>{};
assert(emptyMap2.size() == 0);
let m1 = {"a":1, "b":2, "c":3};
let m1 = {"a" => 1, "b" => 2, "c" => 3};
assert(m1.size() == 3);
assert(m1.get("b") == 2);
let m2: Map<num> = {"a":1, "b":2, "c":3};
let m2: Map<num> = {"a" => 1, "b" => 2, "c" => 3};
assert(m2.size() == 3);
assert(m2.get("b") == 2);
let m3 = Map<num> {"a":1, "b":2, "c":3};
let m3 = Map<num> {"a" => 1, "b" => 2, "c" => 3};
assert(m3.size() == 3);
assert(m3.get("b") == 2);
let m4: Map<num> = Map<num> {"a":1, "b":2, "c":3};
let m4: Map<num> = Map<num> {"a" => 1, "b" => 2, "c" => 3};
assert(m4.size() == 3);
assert(m4.get("b") == 2);
let m5 = {"a":bucket1, "b":bucket2, "c":bucket3};
let m5 = {"a" => bucket1, "b" => bucket2, "c" => bucket3};
assert(m5.size() == 3);
assert(m5.get("b") == bucket2);
let m6: Map<cloud.Bucket> = {"a":bucket1, "b":bucket2, "c":bucket3};
let m6: Map<cloud.Bucket> = {"a" => bucket1, "b" => bucket2, "c" => bucket3};
assert(m6.size() == 3);
assert(m6.get("b") == bucket2);
let m7: Map<num> = m1;
assert(m7.size() == 3);
assert(m7.get("b") == 2);
assert(m7.has("b"));
assert(m4.has("boom") == false);
let m8 = {"a": "a1", "b": "b1", "c": "c1"};
let m8 = {"a" => "a1", "b" => "b1", "c" => "c1"};
assert(m8.keys().at(0) == "a");
assert(m8.keys().at(1) == "b");
assert(m8.keys().at(2) == "c");
Expand All @@ -72,7 +72,7 @@ for val in m8.keys() {
for val in m8.values() {
assert(val.endsWith("1"));
}
let m9 = MutMap<str>{"a": "a1", "b": "b1", "c": "c1"};
let m9 = MutMap<str>{"a" => "a1", "b" => "b1", "c" => "c1"};
assert(m9.keys().at(0) == "a");
assert(m9.keys().at(1) == "b");
assert(m9.keys().at(2) == "c");
Expand Down
15 changes: 12 additions & 3 deletions examples/tests/valid/json.w
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ assert(someJson.get("x") == someNumber);
someJson.set("x", 111);
assert(someJson.get("x") == 111);

// assign Map to Json
let x: Json = {cool: "beans"};
let x: Json = {cool :"beans"};

// Nested Gets and Sets
let nestedJson = MutJson {
Expand Down Expand Up @@ -189,4 +188,14 @@ if let val = jsonElements.tryGet("strings")?.tryGet("non")?.tryGet("existant")?.
// tryGetAt chains with missing members
if let val = jsonElements.tryGet("cant")?.tryGetAt(1000)?.tryGetAt(42) {
assert(false); // nothing should have been found
}
}
// Json keyword is optional
let notSpecified = {
foo: "bar"
};

assert(notSpecified.get("foo") == "bar");

// Check that empty {} is a Json
let empty = {};
assert(Json.has(empty, "something") == false);
2 changes: 1 addition & 1 deletion examples/tests/valid/json_static.w
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let tryParsed = Json.tryParse(invalidJson) ?? Json { key: "value" };
assert(tryParsed.get("key") == "value");

// Format to string
let jj = Json {a: 123, b: {c: 456, d: 789}};
let jj = Json {a: 123, b: {c : 456, d : 789}};
let ss = Json.stringify(jj);
assert(ss == "{\"a\":123,\"b\":{\"c\":456,\"d\":789}}");

Expand Down
12 changes: 6 additions & 6 deletions examples/tests/valid/mut_container_types.w
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ assert(s2.has("hello"));
assert(s3.has(bucket2));

//Map tests
let m1 = MutMap<str>{"hello": "world"};
let m2: MutMap<num> = MutMap<num>{"hello": 123};
let m3 = MutMap<cloud.Bucket>{"b1": bucket1, "b2": bucket2};
let m1 = MutMap<str>{"hello" => "world"};
let m2: MutMap<num> = MutMap<num>{"hello" => 123};
let m3 = MutMap<cloud.Bucket>{"b1" => bucket1, "b2" => bucket2};
let m4: MutMap<str> = m1;
let m5 = MutMap<str>{"goodbye": "world"};
let m6 = MutMap<MutMap<str>>{"a": m1, "b": m5};
let m5 = MutMap<str>{"goodbye" => "world"};
let m6 = MutMap<MutMap<str>>{"a" => m1, "b" => m5};
assert(m1.has("hello"));
assert(m2.size() == 1);
assert(m3.get("b1") == bucket1);
Expand All @@ -42,7 +42,7 @@ assert(m6.get("a").get("hello") == "world");

// mutate maps
m1.set("hello", "goodbye");
m6.set("a", MutMap<str>{"foo": "bar"});
m6.set("a", MutMap<str>{"foo" => "bar"});
m2.clear();

assert(m2.size() == 0);
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/valid/resource_captures.w
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class MyResource {
this.myOptStr = "myOptString";
this.arrayOfStr = ["s1", "s2"];
this.mapOfNum = {
k1: 11,
k2: 22
"k1" => 11,
"k2" => 22
};
this.setOfStr = {"s1", "s2", "s1"};

Expand Down
2 changes: 1 addition & 1 deletion examples/tests/valid/resource_captures_globals.w
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let globalStr = "hello";
let globalBool = true;
let globalNum = 42;
let globalArrayOfStr = ["hello", "world"];
let globalMapOfNum = Map<num>{ "a": -5, "b": 2 };
let globalMapOfNum = Map<num>{ "a" => -5, "b" => 2 };
let globalSetOfStr = Set<str>{ "a", "b" };

class First {
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/valid/std_containers.w
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ assert(sSet.size == 2);
assert(immutSet.size == 3);


let sMap = {"one": 1, "two": 2};
let nestedMap = {"a": {"b": {"c": "hello"}}};
let sMap = {"one" => 1, "two" => 2};
let nestedMap = {"a" => {"b" => {"c": "hello"}}};
let mutMap = sMap.copyMut();
mutMap.set("five", 5);
let immutMap = mutMap.copy();
Expand Down
6 changes: 3 additions & 3 deletions examples/tests/valid/table.w
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ let t = new cloud.Table(cloud.TableProps{
name: "simple-table",
primaryKey: "id",
columns: {
id: cloud.ColumnType.STRING,
name: cloud.ColumnType.STRING,
age: cloud.ColumnType.NUMBER,
"id" => cloud.ColumnType.STRING,
"name" => cloud.ColumnType.STRING,
"age" => cloud.ColumnType.NUMBER,
}
});
Loading

0 comments on commit e71cac1

Please sign in to comment.