From a1f7d56bf40f95488b3848cadecc7906ffa686cc Mon Sep 17 00:00:00 2001 From: partha04patel Date: Sun, 14 Apr 2024 14:12:18 -0400 Subject: [PATCH 01/11] feat: add delete method for MutJson class --- examples/tests/sdk_tests/std/json.test.w | 21 +++++++++++++++++++++ libs/wingsdk/src/std/json.ts | 13 +++++++++++++ 2 files changed, 34 insertions(+) diff --git a/examples/tests/sdk_tests/std/json.test.w b/examples/tests/sdk_tests/std/json.test.w index d8f3b72d474..56a4f327323 100644 --- a/examples/tests/sdk_tests/std/json.test.w +++ b/examples/tests/sdk_tests/std/json.test.w @@ -178,4 +178,25 @@ test "deepCopy(), deepCopyMut()" { assert(copy != copyMut); assert(copyMut.get("object") == mutation); +} + +test "delete() for MutJson" { + let mutObj = MutJson { x: 1, y: 2 }; + mutObj.delete("x"); + let assertThrows = (expected: str, block: (): void) => { + let var error = false; + try { + block(); + } catch actual { + assert(actual == expected); + error = true; + } + assert(error); + }; + + let JSON_PROPERTY_DOES_NOT_EXIST_ERROR = "Json property \"x\" does not exist"; + assertThrows(JSON_PROPERTY_DOES_NOT_EXIST_ERROR, () => { + mutObj.get("x"); + }); + assert(mutObj.delete("random key that doesn't exist") == true); } \ No newline at end of file diff --git a/libs/wingsdk/src/std/json.ts b/libs/wingsdk/src/std/json.ts index 2a7673107d5..6ad5d69095c 100644 --- a/libs/wingsdk/src/std/json.ts +++ b/libs/wingsdk/src/std/json.ts @@ -453,4 +453,17 @@ export class MutJson { public tryAsBool(): boolean | undefined { throw new Error("Macro"); } + + /** + * Removes the specified element from a map. + * + * @macro (delete ($self$)[$args$]) + * + * @param key The key + * @returns true if the given key is no longer present + */ + public delete(key: string): boolean { + key; + throw new Error("Macro"); + } } From 6b75dd032f4b1762f55010852883d9859fe744f4 Mon Sep 17 00:00:00 2001 From: "monada-bot[bot]" Date: Sun, 14 Apr 2024 19:54:49 +0000 Subject: [PATCH 02/11] chore: self mutation (build.diff) Signed-off-by: monada-bot[bot] --- docs/docs/04-standard-library/std/json.md | 17 +++++++++++++++++ .../snapshots/completions/mut_json_methods.snap | 12 ++++++++++++ libs/wingsdk/src/std/json.ts | 10 +++++----- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/docs/docs/04-standard-library/std/json.md b/docs/docs/04-standard-library/std/json.md index 234c64c424a..03fc290a470 100644 --- a/docs/docs/04-standard-library/std/json.md +++ b/docs/docs/04-standard-library/std/json.md @@ -417,6 +417,7 @@ Mutable Json. | asBool | Convert Json element to boolean if possible. | | asNum | Convert Json element to number if possible. | | asStr | Convert Json element to string if possible. | +| delete | Removes the specified element from a map. | | get | Returns the value associated with the specified Json key. | | getAt | Returns a specified element at a given index from MutJson Array. | | set | Adds or updates an element in MutJson with a specific key and value. | @@ -453,6 +454,22 @@ asStr(): str Convert Json element to string if possible. +##### `delete` + +```wing +delete(key: str): bool +``` + +Removes the specified element from a map. + +###### `key`Required + +- *Type:* str + +The key. + +--- + ##### `get` ```wing diff --git a/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap b/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap index c26469afc94..0e72951d3d4 100644 --- a/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap +++ b/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap @@ -25,6 +25,18 @@ source: libs/wingc/src/lsp/completions.rs value: "```wing\nasStr: (): str\n```\n---\nConvert Json element to string if possible.\n\n### Returns\na string." sortText: ff|asStr insertText: asStr() +- label: delete + kind: 2 + detail: "(key: str): bool" + documentation: + kind: markdown + value: "```wing\ndelete: (key: str): bool\n```\n---\nRemoves the specified element from a map.\n### Parameters\n- `key` — `str` — The key.\n\n### Returns\ntrue if the given key is no longer present" + sortText: ff|delete + insertText: delete($1) + insertTextFormat: 2 + command: + title: triggerParameterHints + command: editor.action.triggerParameterHints - label: get kind: 2 detail: "(key: str): MutJson" diff --git a/libs/wingsdk/src/std/json.ts b/libs/wingsdk/src/std/json.ts index 6ad5d69095c..a585d9d993e 100644 --- a/libs/wingsdk/src/std/json.ts +++ b/libs/wingsdk/src/std/json.ts @@ -454,7 +454,7 @@ export class MutJson { throw new Error("Macro"); } - /** + /** * Removes the specified element from a map. * * @macro (delete ($self$)[$args$]) @@ -462,8 +462,8 @@ export class MutJson { * @param key The key * @returns true if the given key is no longer present */ - public delete(key: string): boolean { - key; - throw new Error("Macro"); - } + public delete(key: string): boolean { + key; + throw new Error("Macro"); + } } From 76541081f9c13ae10c4d95b16563581bd2fdb91c Mon Sep 17 00:00:00 2001 From: "monada-bot[bot]" Date: Sun, 14 Apr 2024 19:54:49 +0000 Subject: [PATCH 03/11] chore: self mutation (e2e-2of2.diff) Signed-off-by: monada-bot[bot] --- .../test_corpus/sdk_tests/std/json.test.w_test_sim.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_test_sim.md b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_test_sim.md index 42f62dffa96..1ff2de3e9ea 100644 --- a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_test_sim.md +++ b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/json.test.w_test_sim.md @@ -4,6 +4,7 @@ ```log pass ─ json.test.wsim » root/env0/test:has() pass ─ json.test.wsim » root/env1/test:get() +pass ─ json.test.wsim » root/env10/test:delete() for MutJson pass ─ json.test.wsim » root/env2/test:getAt() pass ─ json.test.wsim » root/env3/test:set() pass ─ json.test.wsim » root/env4/test:setAt() @@ -13,7 +14,7 @@ pass ─ json.test.wsim » root/env7/test:parse() pass ─ json.test.wsim » root/env8/test:tryParse() pass ─ json.test.wsim » root/env9/test:deepCopy(), deepCopyMut() -Tests 10 passed (10) +Tests 11 passed (11) Snapshots 1 skipped Test Files 1 passed (1) Duration From 654cedbd03bd04c5852fc2f6b393200b72343b77 Mon Sep 17 00:00:00 2001 From: partha04patel Date: Mon, 15 Apr 2024 16:28:53 -0400 Subject: [PATCH 04/11] feat(sdk): added has() method to MutJson class --- examples/tests/sdk_tests/std/json.test.w | 21 ++++----------------- examples/tests/valid/json.test.w | 2 +- examples/tests/valid/json_static.test.w | 4 ++-- libs/wingsdk/src/std/json.ts | 24 ++++++++++++++++++------ 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/examples/tests/sdk_tests/std/json.test.w b/examples/tests/sdk_tests/std/json.test.w index 56a4f327323..7339077e79e 100644 --- a/examples/tests/sdk_tests/std/json.test.w +++ b/examples/tests/sdk_tests/std/json.test.w @@ -6,8 +6,8 @@ bring cloud; test "has()" { let obj = Json { key1: 1, key2: 2}; - assert(Json.has(obj, "key1") == true); - assert(Json.has(obj, "key3") == false); + assert(obj.has("key1") == true); + assert(obj.has("key3") == false); } test "get()" { @@ -183,20 +183,7 @@ test "deepCopy(), deepCopyMut()" { test "delete() for MutJson" { let mutObj = MutJson { x: 1, y: 2 }; mutObj.delete("x"); - let assertThrows = (expected: str, block: (): void) => { - let var error = false; - try { - block(); - } catch actual { - assert(actual == expected); - error = true; - } - assert(error); - }; - - let JSON_PROPERTY_DOES_NOT_EXIST_ERROR = "Json property \"x\" does not exist"; - assertThrows(JSON_PROPERTY_DOES_NOT_EXIST_ERROR, () => { - mutObj.get("x"); - }); + assert(mutObj.has("x") == false); + assert(mutObj.has("y")==true); assert(mutObj.delete("random key that doesn't exist") == true); } \ No newline at end of file diff --git a/examples/tests/valid/json.test.w b/examples/tests/valid/json.test.w index fac7da364d5..83d0579264a 100644 --- a/examples/tests/valid/json.test.w +++ b/examples/tests/valid/json.test.w @@ -206,7 +206,7 @@ assert(notSpecified.get("foo") == "bar"); // Check that empty {} is a Json let empty = {}; -assert(Json.has(empty, "something") == false); +assert(empty.has("something") == false); struct Base { base: str; diff --git a/examples/tests/valid/json_static.test.w b/examples/tests/valid/json_static.test.w index 812a9351d31..360baee950e 100644 --- a/examples/tests/valid/json_static.test.w +++ b/examples/tests/valid/json_static.test.w @@ -58,6 +58,6 @@ test "Access Json static inflight" { // Check whether some key exists in a json test "has key or not" { let hasCheck = Json {a: "hello", b: "wing"}; - assert(Json.has(hasCheck, "a") == true); - assert(Json.has(hasCheck, "c") == false); + assert(hasCheck.has("a") == true); + assert(hasCheck.has("c") == false); } \ No newline at end of file diff --git a/libs/wingsdk/src/std/json.ts b/libs/wingsdk/src/std/json.ts index a585d9d993e..8ccee05e016 100644 --- a/libs/wingsdk/src/std/json.ts +++ b/libs/wingsdk/src/std/json.ts @@ -156,17 +156,15 @@ export class Json { throw new Error("Macro"); } - /** + /** * Checks if a Json object has a given key * - * @macro ((json, key) => { return json.hasOwnProperty(key); })($args$) - * - * @param json The json object to inspect + * @macro ((obj, key) => { return obj.hasOwnProperty(key); })($self$,$args$) + * * @param key The key to check * @returns Boolean value corresponding to whether the key exists */ - public static has(json: Json, key: string): boolean { - json; + public has(key: string): boolean { key; throw new Error("Macro"); } @@ -462,8 +460,22 @@ export class MutJson { * @param key The key * @returns true if the given key is no longer present */ + public delete(key: string): boolean { key; throw new Error("Macro"); } + + /** + * Checks if a Json object has a given key + * + * @macro ((obj, key) => { return obj.hasOwnProperty(key); })($self$,$args$) + * + * @param key The key to check + * @returns Boolean value corresponding to whether the key exists + */ + public has(key: string): boolean { + key; + throw new Error("Macro"); + } } From 5865ebb16519d5742aa8e5edea00ca4fcb9f22b1 Mon Sep 17 00:00:00 2001 From: "monada-bot[bot]" Date: Mon, 15 Apr 2024 20:52:26 +0000 Subject: [PATCH 05/11] chore: self mutation (e2e-1of2.diff) Signed-off-by: monada-bot[bot] --- .../test_corpus/valid/json.test.w_compile_tf-aws.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/hangar/__snapshots__/test_corpus/valid/json.test.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/json.test.w_compile_tf-aws.md index 4190779ac7e..930a2c339f1 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/json.test.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/json.test.w_compile_tf-aws.md @@ -210,7 +210,7 @@ class $Root extends $stdlib.std.Resource { const notSpecified = ({"foo": "bar"}); $helpers.assert($helpers.eq(((obj, args) => { if (obj[args] === undefined) throw new Error(`Json property "${args}" does not exist`); return obj[args] })(notSpecified, "foo"), "bar"), "notSpecified.get(\"foo\") == \"bar\""); const empty = ({}); - $helpers.assert($helpers.eq(((json, key) => { return json.hasOwnProperty(key); })(empty, "something"), false), "Json.has(empty, \"something\") == false"); + $helpers.assert($helpers.eq(((obj, key) => { return obj.hasOwnProperty(key); })(empty,"something"), false), "empty.has(\"something\") == false"); const arrayStruct = [({"foo": "", "stuff": []})]; const setStruct = new Set([({"foo": "", "stuff": []})]); const mapStruct = ({["1"]: ({"foo": "", "stuff": []})}); From 2d1ebb47d25b5341c785207969ddcded078231c8 Mon Sep 17 00:00:00 2001 From: "monada-bot[bot]" Date: Mon, 15 Apr 2024 20:52:26 +0000 Subject: [PATCH 06/11] chore: self mutation (e2e-2of2.diff) Signed-off-by: monada-bot[bot] --- .../test_corpus/valid/json_static.test.w_compile_tf-aws.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tools/hangar/__snapshots__/test_corpus/valid/json_static.test.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/json_static.test.w_compile_tf-aws.md index 5001699ac15..cbef2880146 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/json_static.test.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/json_static.test.w_compile_tf-aws.md @@ -25,7 +25,7 @@ module.exports = function({ $jj, $std_Json }) { ```cjs "use strict"; const $helpers = require("@winglang/sdk/lib/helpers"); -module.exports = function({ $std_Json }) { +module.exports = function({ }) { class $Closure2 { constructor({ }) { const $obj = (...args) => this.handle(...args); @@ -34,8 +34,8 @@ module.exports = function({ $std_Json }) { } async handle() { const hasCheck = ({"a": "hello", "b": "wing"}); - $helpers.assert($helpers.eq(((json, key) => { return json.hasOwnProperty(key); })(hasCheck, "a"), true), "Json.has(hasCheck, \"a\") == true"); - $helpers.assert($helpers.eq(((json, key) => { return json.hasOwnProperty(key); })(hasCheck, "c"), false), "Json.has(hasCheck, \"c\") == false"); + $helpers.assert($helpers.eq(((obj, key) => { return obj.hasOwnProperty(key); })(hasCheck,"a"), true), "hasCheck.has(\"a\") == true"); + $helpers.assert($helpers.eq(((obj, key) => { return obj.hasOwnProperty(key); })(hasCheck,"c"), false), "hasCheck.has(\"c\") == false"); } } return $Closure2; @@ -121,7 +121,6 @@ class $Root extends $stdlib.std.Resource { static _toInflightType() { return ` require("${$helpers.normalPath(__dirname)}/inflight.$Closure2-1.cjs")({ - $std_Json: ${$stdlib.core.liftObject($stdlib.core.toLiftableModuleType(std.Json, "@winglang/sdk/std", "Json"))}, }) `; } From bf4d56d50904b4956a356a4c319e065d8bc3d029 Mon Sep 17 00:00:00 2001 From: partha04patel Date: Mon, 15 Apr 2024 22:37:51 -0400 Subject: [PATCH 07/11] put constructor before instance has() method for eslint --- libs/wingsdk/src/std/json.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/wingsdk/src/std/json.ts b/libs/wingsdk/src/std/json.ts index 8ccee05e016..26a08e70a5a 100644 --- a/libs/wingsdk/src/std/json.ts +++ b/libs/wingsdk/src/std/json.ts @@ -155,22 +155,20 @@ export class Json { str; throw new Error("Macro"); } - - /** + private constructor() {} + /** * Checks if a Json object has a given key * * @macro ((obj, key) => { return obj.hasOwnProperty(key); })($self$,$args$) - * + * * @param key The key to check * @returns Boolean value corresponding to whether the key exists */ - public has(key: string): boolean { + public has(key: string): boolean { key; throw new Error("Macro"); } - private constructor() {} - /** * Returns the value associated with the specified Json key * @@ -470,7 +468,7 @@ export class MutJson { * Checks if a Json object has a given key * * @macro ((obj, key) => { return obj.hasOwnProperty(key); })($self$,$args$) - * + * * @param key The key to check * @returns Boolean value corresponding to whether the key exists */ From 5e3b69bab2a95c8aa3022e92b63d158c55bba70d Mon Sep 17 00:00:00 2001 From: "monada-bot[bot]" Date: Tue, 16 Apr 2024 08:50:37 +0000 Subject: [PATCH 08/11] chore: self mutation (build.diff) Signed-off-by: monada-bot[bot] --- docs/docs/04-standard-library/std/json.md | 59 +++++++++++-------- .../snapshots/completions/json_statics.snap | 12 ---- .../completions/mut_json_methods.snap | 12 ++++ .../completions/optional_chaining.snap | 12 ++++ .../completions/optional_chaining_auto.snap | 21 +++++++ .../completions/parentheses_expression.snap | 12 ++++ .../static_completions_after_expression.snap | 12 ---- .../static_json_after_expression.snap | 12 ---- ...tatic_json_after_expression_statement.snap | 12 ---- .../snapshots/hovers/static_method_root.snap | 2 +- 10 files changed, 92 insertions(+), 74 deletions(-) diff --git a/docs/docs/04-standard-library/std/json.md b/docs/docs/04-standard-library/std/json.md index 03fc290a470..35d96f60bf8 100644 --- a/docs/docs/04-standard-library/std/json.md +++ b/docs/docs/04-standard-library/std/json.md @@ -21,6 +21,7 @@ Immutable Json. | asStr | Convert Json element to string if possible. | | get | Returns the value associated with the specified Json key. | | getAt | Returns a specified element at a given index from Json Array. | +| has | Checks if a Json object has a given key. | | tryAsBool | Convert Json element to boolean if possible. | | tryAsNum | Convert Json element to number if possible. | | tryAsStr | Convert Json element to string if possible. | @@ -85,6 +86,22 @@ The index of the element in the Json Array to return. --- +##### `has` + +```wing +has(key: str): bool +``` + +Checks if a Json object has a given key. + +###### `key`Required + +- *Type:* str + +The key to check. + +--- + ##### `tryAsBool` ```wing @@ -149,7 +166,6 @@ The index of the element in the Json Array to return. | deepCopyMut | Creates a mutable deep copy of the Json. | | delete | Deletes a key in a given Json. | | entries | Returns the entries from the Json. | -| has | Checks if a Json object has a given key. | | keys | Returns the keys from the Json. | | parse | Parse a string into a Json. | | stringify | Formats Json as string. | @@ -230,30 +246,6 @@ map to get the entries from. --- -##### `has` - -```wing -Json.has(json: Json, key: str); -``` - -Checks if a Json object has a given key. - -###### `json`Required - -- *Type:* Json - -The json object to inspect. - ---- - -###### `key`Required - -- *Type:* str - -The key to check. - ---- - ##### `keys` ```wing @@ -420,6 +412,7 @@ Mutable Json. | delete | Removes the specified element from a map. | | get | Returns the value associated with the specified Json key. | | getAt | Returns a specified element at a given index from MutJson Array. | +| has | Checks if a Json object has a given key. | | set | Adds or updates an element in MutJson with a specific key and value. | | setAt | Set element in MutJson Array with a specific key and value. | | tryAsBool | Convert Json element to boolean if possible. | @@ -502,6 +495,22 @@ The index of the element in the MutJson Array to return. --- +##### `has` + +```wing +has(key: str): bool +``` + +Checks if a Json object has a given key. + +###### `key`Required + +- *Type:* str + +The key to check. + +--- + ##### `set` ```wing diff --git a/libs/wingc/src/lsp/snapshots/completions/json_statics.snap b/libs/wingc/src/lsp/snapshots/completions/json_statics.snap index 365b8c2425c..ad6eb5ddf4c 100644 --- a/libs/wingc/src/lsp/snapshots/completions/json_statics.snap +++ b/libs/wingc/src/lsp/snapshots/completions/json_statics.snap @@ -49,18 +49,6 @@ source: libs/wingc/src/lsp/completions.rs command: title: triggerParameterHints command: editor.action.triggerParameterHints -- label: has - kind: 2 - detail: "(json: Json, key: str): bool" - documentation: - kind: markdown - value: "```wing\nstatic has: (json: Json, key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `json` — `Json` — The json object to inspect.\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" - sortText: ff|has - insertText: has($1) - insertTextFormat: 2 - command: - title: triggerParameterHints - command: editor.action.triggerParameterHints - label: keys kind: 2 detail: "(json: any): Array" diff --git a/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap b/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap index 0e72951d3d4..f3a29c0b31e 100644 --- a/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap +++ b/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap @@ -61,6 +61,18 @@ source: libs/wingc/src/lsp/completions.rs command: title: triggerParameterHints command: editor.action.triggerParameterHints +- label: has + kind: 2 + detail: "(key: str): bool" + documentation: + kind: markdown + value: "```wing\nhas: (key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" + sortText: ff|has + insertText: has($1) + insertTextFormat: 2 + command: + title: triggerParameterHints + command: editor.action.triggerParameterHints - label: set kind: 2 detail: "(key: str, value: MutJson): void" diff --git a/libs/wingc/src/lsp/snapshots/completions/optional_chaining.snap b/libs/wingc/src/lsp/snapshots/completions/optional_chaining.snap index 9ffa80db684..59086c30f09 100644 --- a/libs/wingc/src/lsp/snapshots/completions/optional_chaining.snap +++ b/libs/wingc/src/lsp/snapshots/completions/optional_chaining.snap @@ -49,6 +49,18 @@ source: libs/wingc/src/lsp/completions.rs command: title: triggerParameterHints command: editor.action.triggerParameterHints +- label: has + kind: 2 + detail: "(key: str): bool" + documentation: + kind: markdown + value: "```wing\nhas: (key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" + sortText: ff|has + insertText: has($1) + insertTextFormat: 2 + command: + title: triggerParameterHints + command: editor.action.triggerParameterHints - label: tryAsBool kind: 2 detail: "(): bool?" diff --git a/libs/wingc/src/lsp/snapshots/completions/optional_chaining_auto.snap b/libs/wingc/src/lsp/snapshots/completions/optional_chaining_auto.snap index c5129c8a827..ed82e23a65e 100644 --- a/libs/wingc/src/lsp/snapshots/completions/optional_chaining_auto.snap +++ b/libs/wingc/src/lsp/snapshots/completions/optional_chaining_auto.snap @@ -94,6 +94,27 @@ source: libs/wingc/src/lsp/completions.rs command: title: triggerParameterHints command: editor.action.triggerParameterHints +- label: has + kind: 2 + detail: "(key: str): bool" + documentation: + kind: markdown + value: "```wing\nhas: (key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" + sortText: ff|has + insertText: has($1) + insertTextFormat: 2 + additionalTextEdits: + - range: + start: + line: 2 + character: 12 + end: + line: 2 + character: 13 + newText: "?." + command: + title: triggerParameterHints + command: editor.action.triggerParameterHints - label: tryAsBool kind: 2 detail: "(): bool?" diff --git a/libs/wingc/src/lsp/snapshots/completions/parentheses_expression.snap b/libs/wingc/src/lsp/snapshots/completions/parentheses_expression.snap index 9ffa80db684..59086c30f09 100644 --- a/libs/wingc/src/lsp/snapshots/completions/parentheses_expression.snap +++ b/libs/wingc/src/lsp/snapshots/completions/parentheses_expression.snap @@ -49,6 +49,18 @@ source: libs/wingc/src/lsp/completions.rs command: title: triggerParameterHints command: editor.action.triggerParameterHints +- label: has + kind: 2 + detail: "(key: str): bool" + documentation: + kind: markdown + value: "```wing\nhas: (key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" + sortText: ff|has + insertText: has($1) + insertTextFormat: 2 + command: + title: triggerParameterHints + command: editor.action.triggerParameterHints - label: tryAsBool kind: 2 detail: "(): bool?" diff --git a/libs/wingc/src/lsp/snapshots/completions/static_completions_after_expression.snap b/libs/wingc/src/lsp/snapshots/completions/static_completions_after_expression.snap index 365b8c2425c..ad6eb5ddf4c 100644 --- a/libs/wingc/src/lsp/snapshots/completions/static_completions_after_expression.snap +++ b/libs/wingc/src/lsp/snapshots/completions/static_completions_after_expression.snap @@ -49,18 +49,6 @@ source: libs/wingc/src/lsp/completions.rs command: title: triggerParameterHints command: editor.action.triggerParameterHints -- label: has - kind: 2 - detail: "(json: Json, key: str): bool" - documentation: - kind: markdown - value: "```wing\nstatic has: (json: Json, key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `json` — `Json` — The json object to inspect.\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" - sortText: ff|has - insertText: has($1) - insertTextFormat: 2 - command: - title: triggerParameterHints - command: editor.action.triggerParameterHints - label: keys kind: 2 detail: "(json: any): Array" diff --git a/libs/wingc/src/lsp/snapshots/completions/static_json_after_expression.snap b/libs/wingc/src/lsp/snapshots/completions/static_json_after_expression.snap index 365b8c2425c..ad6eb5ddf4c 100644 --- a/libs/wingc/src/lsp/snapshots/completions/static_json_after_expression.snap +++ b/libs/wingc/src/lsp/snapshots/completions/static_json_after_expression.snap @@ -49,18 +49,6 @@ source: libs/wingc/src/lsp/completions.rs command: title: triggerParameterHints command: editor.action.triggerParameterHints -- label: has - kind: 2 - detail: "(json: Json, key: str): bool" - documentation: - kind: markdown - value: "```wing\nstatic has: (json: Json, key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `json` — `Json` — The json object to inspect.\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" - sortText: ff|has - insertText: has($1) - insertTextFormat: 2 - command: - title: triggerParameterHints - command: editor.action.triggerParameterHints - label: keys kind: 2 detail: "(json: any): Array" diff --git a/libs/wingc/src/lsp/snapshots/completions/static_json_after_expression_statement.snap b/libs/wingc/src/lsp/snapshots/completions/static_json_after_expression_statement.snap index 365b8c2425c..ad6eb5ddf4c 100644 --- a/libs/wingc/src/lsp/snapshots/completions/static_json_after_expression_statement.snap +++ b/libs/wingc/src/lsp/snapshots/completions/static_json_after_expression_statement.snap @@ -49,18 +49,6 @@ source: libs/wingc/src/lsp/completions.rs command: title: triggerParameterHints command: editor.action.triggerParameterHints -- label: has - kind: 2 - detail: "(json: Json, key: str): bool" - documentation: - kind: markdown - value: "```wing\nstatic has: (json: Json, key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `json` — `Json` — The json object to inspect.\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" - sortText: ff|has - insertText: has($1) - insertTextFormat: 2 - command: - title: triggerParameterHints - command: editor.action.triggerParameterHints - label: keys kind: 2 detail: "(json: any): Array" diff --git a/libs/wingc/src/lsp/snapshots/hovers/static_method_root.snap b/libs/wingc/src/lsp/snapshots/hovers/static_method_root.snap index 04e5543ad8d..e1800e030c5 100644 --- a/libs/wingc/src/lsp/snapshots/hovers/static_method_root.snap +++ b/libs/wingc/src/lsp/snapshots/hovers/static_method_root.snap @@ -3,7 +3,7 @@ source: libs/wingc/src/lsp/hover.rs --- contents: kind: markdown - value: "```wing\nclass Json\n```\n---\nImmutable Json.\n\n### Methods\n- `asBool` — `(): bool` — Convert Json element to boolean if possible.\n- `asNum` — `(): num` — Convert Json element to number if possible.\n- `asStr` — `(): str` — Convert Json element to string if possible.\n- `deepCopy` — `(json: MutJson): Json` — Creates an immutable deep copy of the Json.\n- `deepCopyMut` — `(json: Json): MutJson` — Creates a mutable deep copy of the Json.\n- `delete` — `(json: MutJson, key: str): void` — Deletes a key in a given Json.\n- `entries` — `(json: Json): Array` — Returns the entries from the Json.\n- `get` — `(key: str): Json` — Returns the value associated with the specified Json key.\n- `getAt` — `(index: num): Json` — Returns a specified element at a given index from Json Array.\n- `has` — `(json: Json, key: str): bool` — Checks if a Json object has a given key.\n- `keys` — `(json: any): Array` — Returns the keys from the Json.\n- `parse` — `(str: str): Json` — Parse a string into a Json.\n- `stringify` — `(json: any, options: JsonStringifyOptions?): str` — Formats Json as string.\n- `tryAsBool` — `(): bool?` — Convert Json element to boolean if possible.\n- `tryAsNum` — `(): num?` — Convert Json element to number if possible.\n- `tryAsStr` — `(): str?` — Convert Json element to string if possible.\n- `tryGet` — `(key: str): Json?` — Optionally returns an specified element from the Json.\n- `tryGetAt` — `(index: num): Json?` — Optionally returns a specified element at a given index from Json Array.\n- `tryParse` — `(str: str?): Json?` — Try to parse a string into a Json.\n- `values` — `(json: Json): Array` — Returns the values from the Json." + value: "```wing\nclass Json\n```\n---\nImmutable Json.\n\n### Methods\n- `asBool` — `(): bool` — Convert Json element to boolean if possible.\n- `asNum` — `(): num` — Convert Json element to number if possible.\n- `asStr` — `(): str` — Convert Json element to string if possible.\n- `deepCopy` — `(json: MutJson): Json` — Creates an immutable deep copy of the Json.\n- `deepCopyMut` — `(json: Json): MutJson` — Creates a mutable deep copy of the Json.\n- `delete` — `(json: MutJson, key: str): void` — Deletes a key in a given Json.\n- `entries` — `(json: Json): Array` — Returns the entries from the Json.\n- `get` — `(key: str): Json` — Returns the value associated with the specified Json key.\n- `getAt` — `(index: num): Json` — Returns a specified element at a given index from Json Array.\n- `has` — `(key: str): bool` — Checks if a Json object has a given key.\n- `keys` — `(json: any): Array` — Returns the keys from the Json.\n- `parse` — `(str: str): Json` — Parse a string into a Json.\n- `stringify` — `(json: any, options: JsonStringifyOptions?): str` — Formats Json as string.\n- `tryAsBool` — `(): bool?` — Convert Json element to boolean if possible.\n- `tryAsNum` — `(): num?` — Convert Json element to number if possible.\n- `tryAsStr` — `(): str?` — Convert Json element to string if possible.\n- `tryGet` — `(key: str): Json?` — Optionally returns an specified element from the Json.\n- `tryGetAt` — `(index: num): Json?` — Optionally returns a specified element at a given index from Json Array.\n- `tryParse` — `(str: str?): Json?` — Try to parse a string into a Json.\n- `values` — `(json: Json): Array` — Returns the values from the Json." range: start: line: 1 From 27ae23e82dfebc2b3cbf7b262725a30717cfdf2d Mon Sep 17 00:00:00 2001 From: partha04patel Date: Thu, 18 Apr 2024 14:56:03 -0400 Subject: [PATCH 09/11] better test MutJson.delete() and support more types of inputs to method --- examples/tests/sdk_tests/std/json.test.w | 22 ++++++++++++++++++++++ libs/tree-sitter-wing/src/grammar.json | 1 + libs/wingsdk/src/std/json.ts | 10 +++++----- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/examples/tests/sdk_tests/std/json.test.w b/examples/tests/sdk_tests/std/json.test.w index 7339077e79e..29ae18d5cc7 100644 --- a/examples/tests/sdk_tests/std/json.test.w +++ b/examples/tests/sdk_tests/std/json.test.w @@ -186,4 +186,26 @@ test "delete() for MutJson" { assert(mutObj.has("x") == false); assert(mutObj.has("y")==true); assert(mutObj.delete("random key that doesn't exist") == true); + let nullObj = MutJson{}; + assert(nullObj.delete("something") == true); + let mutJsonArray = MutJson [1, 2, 3]; + assert(mutJsonArray.delete(1) == true); + let boolMutJson = MutJson[true, 1, 2, 3]; + assert(boolMutJson.delete(true) == true); + assert(boolMutJson.has(true) == false); + assert(boolMutJson.has(1) == true); + let original = MutJson ({ + "string": "wing", + "number": 123, + "array": [1, 2, 3], + "true": true, + "false": false, + "object": { + "key1": "value1", + "key2": 2, + "key3": false, + "key5": [3, 2, 1] + } + }); + assert(original.delete("key5")==true); } \ No newline at end of file diff --git a/libs/tree-sitter-wing/src/grammar.json b/libs/tree-sitter-wing/src/grammar.json index 18ac32e2e77..0051d635a17 100644 --- a/libs/tree-sitter-wing/src/grammar.json +++ b/libs/tree-sitter-wing/src/grammar.json @@ -4450,3 +4450,4 @@ "_literal" ] } + diff --git a/libs/wingsdk/src/std/json.ts b/libs/wingsdk/src/std/json.ts index 26a08e70a5a..50e1807bdb8 100644 --- a/libs/wingsdk/src/std/json.ts +++ b/libs/wingsdk/src/std/json.ts @@ -452,14 +452,14 @@ export class MutJson { /** * Removes the specified element from a map. + * + * @macro delete ($self$)?.[$args$] * - * @macro (delete ($self$)[$args$]) - * - * @param key The key + * @param key Either the key or index to delete * @returns true if the given key is no longer present */ - public delete(key: string): boolean { + public delete(key: string | number): boolean { key; throw new Error("Macro"); } @@ -472,7 +472,7 @@ export class MutJson { * @param key The key to check * @returns Boolean value corresponding to whether the key exists */ - public has(key: string): boolean { + public has(key: string | boolean | number ): boolean { key; throw new Error("Macro"); } From bcfd6e24d54a28bf0cb7f2fa18c7d067295dc47d Mon Sep 17 00:00:00 2001 From: partha04patel Date: Thu, 18 Apr 2024 14:58:24 -0400 Subject: [PATCH 10/11] remove extra line from last commit --- libs/tree-sitter-wing/src/grammar.json | 1 - 1 file changed, 1 deletion(-) diff --git a/libs/tree-sitter-wing/src/grammar.json b/libs/tree-sitter-wing/src/grammar.json index 0051d635a17..18ac32e2e77 100644 --- a/libs/tree-sitter-wing/src/grammar.json +++ b/libs/tree-sitter-wing/src/grammar.json @@ -4450,4 +4450,3 @@ "_literal" ] } - From 41c369f9c8dfe27f38f08bbc67f3b7ad0d568c53 Mon Sep 17 00:00:00 2001 From: "monada-bot[bot]" Date: Fri, 19 Apr 2024 23:10:20 +0000 Subject: [PATCH 11/11] chore: self mutation (build.diff) Signed-off-by: monada-bot[bot] --- docs/docs/04-standard-library/std/json.md | 10 +++++----- .../lsp/snapshots/completions/mut_json_methods.snap | 8 ++++---- libs/wingsdk/src/std/json.ts | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/docs/04-standard-library/std/json.md b/docs/docs/04-standard-library/std/json.md index 72436d5dc3f..aa5e98d4624 100644 --- a/docs/docs/04-standard-library/std/json.md +++ b/docs/docs/04-standard-library/std/json.md @@ -450,16 +450,16 @@ Convert Json element to string if possible. ##### `delete` ```wing -delete(key: str): bool +delete(key: any): bool ``` Removes the specified element from a map. ###### `key`Required -- *Type:* str +- *Type:* any -The key. +Either the key or index to delete. --- @@ -498,14 +498,14 @@ The index of the element in the MutJson Array to return. ##### `has` ```wing -has(key: str): bool +has(key: any): bool ``` Checks if a Json object has a given key. ###### `key`Required -- *Type:* str +- *Type:* any The key to check. diff --git a/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap b/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap index f3a29c0b31e..8f11663039f 100644 --- a/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap +++ b/libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap @@ -27,10 +27,10 @@ source: libs/wingc/src/lsp/completions.rs insertText: asStr() - label: delete kind: 2 - detail: "(key: str): bool" + detail: "(key: any): bool" documentation: kind: markdown - value: "```wing\ndelete: (key: str): bool\n```\n---\nRemoves the specified element from a map.\n### Parameters\n- `key` — `str` — The key.\n\n### Returns\ntrue if the given key is no longer present" + value: "```wing\ndelete: (key: any): bool\n```\n---\nRemoves the specified element from a map.\n### Parameters\n- `key` — `any` — Either the key or index to delete.\n\n### Returns\ntrue if the given key is no longer present" sortText: ff|delete insertText: delete($1) insertTextFormat: 2 @@ -63,10 +63,10 @@ source: libs/wingc/src/lsp/completions.rs command: editor.action.triggerParameterHints - label: has kind: 2 - detail: "(key: str): bool" + detail: "(key: any): bool" documentation: kind: markdown - value: "```wing\nhas: (key: str): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `key` — `str` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" + value: "```wing\nhas: (key: any): bool\n```\n---\nChecks if a Json object has a given key.\n### Parameters\n- `key` — `any` — The key to check.\n\n### Returns\nBoolean value corresponding to whether the key exists" sortText: ff|has insertText: has($1) insertTextFormat: 2 diff --git a/libs/wingsdk/src/std/json.ts b/libs/wingsdk/src/std/json.ts index 50e1807bdb8..26861b52151 100644 --- a/libs/wingsdk/src/std/json.ts +++ b/libs/wingsdk/src/std/json.ts @@ -452,7 +452,7 @@ export class MutJson { /** * Removes the specified element from a map. - * + * * @macro delete ($self$)?.[$args$] * * @param key Either the key or index to delete @@ -472,7 +472,7 @@ export class MutJson { * @param key The key to check * @returns Boolean value corresponding to whether the key exists */ - public has(key: string | boolean | number ): boolean { + public has(key: string | boolean | number): boolean { key; throw new Error("Macro"); }