diff --git a/docs/docs/04-standard-library/std/json.md b/docs/docs/04-standard-library/std/json.md index da8d8a1e80c..72436d5dc3f 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 @@ -417,8 +409,10 @@ 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. | +| 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. | @@ -453,6 +447,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 @@ -485,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/examples/tests/sdk_tests/std/json.test.w b/examples/tests/sdk_tests/std/json.test.w index d8f3b72d474..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()" { @@ -178,4 +178,12 @@ 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"); + 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/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 c26469afc94..f3a29c0b31e 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" @@ -49,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 diff --git a/libs/wingsdk/src/std/json.ts b/libs/wingsdk/src/std/json.ts index 2a7673107d5..26a08e70a5a 100644 --- a/libs/wingsdk/src/std/json.ts +++ b/libs/wingsdk/src/std/json.ts @@ -155,24 +155,20 @@ export class Json { str; throw new Error("Macro"); } - + private constructor() {} /** * Checks if a Json object has a given key * - * @macro ((json, key) => { return json.hasOwnProperty(key); })($args$) + * @macro ((obj, key) => { return obj.hasOwnProperty(key); })($self$,$args$) * - * @param json The json object to inspect * @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"); } - private constructor() {} - /** * Returns the value associated with the specified Json key * @@ -453,4 +449,31 @@ 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"); + } + + /** + * 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"); + } } 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 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": []})}); 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"))}, }) `; }