Skip to content

Commit

Permalink
feat(sdk)!: add delete method for MutJson class (#6236)
Browse files Browse the repository at this point in the history
## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] 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)*.

BREAKING CHANGE: `has()` removed from static `Json`. It is now an instance method on `Json` objects.
  • Loading branch information
partha04patel authored Apr 18, 2024
1 parent b08a84f commit 24178a4
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 92 deletions.
76 changes: 51 additions & 25 deletions docs/docs/04-standard-library/std/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Immutable Json.
| <code><a href="#@winglang/sdk.std.Json.asStr">asStr</a></code> | Convert Json element to string if possible. |
| <code><a href="#@winglang/sdk.std.Json.get">get</a></code> | Returns the value associated with the specified Json key. |
| <code><a href="#@winglang/sdk.std.Json.getAt">getAt</a></code> | Returns a specified element at a given index from Json Array. |
| <code><a href="#@winglang/sdk.std.Json.has">has</a></code> | Checks if a Json object has a given key. |
| <code><a href="#@winglang/sdk.std.Json.tryAsBool">tryAsBool</a></code> | Convert Json element to boolean if possible. |
| <code><a href="#@winglang/sdk.std.Json.tryAsNum">tryAsNum</a></code> | Convert Json element to number if possible. |
| <code><a href="#@winglang/sdk.std.Json.tryAsStr">tryAsStr</a></code> | Convert Json element to string if possible. |
Expand Down Expand Up @@ -85,6 +86,22 @@ The index of the element in the Json Array to return.

---

##### `has` <a name="has" id="@winglang/sdk.std.Json.has"></a>

```wing
has(key: str): bool
```

Checks if a Json object has a given key.

###### `key`<sup>Required</sup> <a name="key" id="@winglang/sdk.std.Json.has.parameter.key"></a>

- *Type:* str

The key to check.

---

##### `tryAsBool` <a name="tryAsBool" id="@winglang/sdk.std.Json.tryAsBool"></a>

```wing
Expand Down Expand Up @@ -149,7 +166,6 @@ The index of the element in the Json Array to return.
| <code><a href="#@winglang/sdk.std.Json.deepCopyMut">deepCopyMut</a></code> | Creates a mutable deep copy of the Json. |
| <code><a href="#@winglang/sdk.std.Json.delete">delete</a></code> | Deletes a key in a given Json. |
| <code><a href="#@winglang/sdk.std.Json.entries">entries</a></code> | Returns the entries from the Json. |
| <code><a href="#@winglang/sdk.std.Json.has">has</a></code> | Checks if a Json object has a given key. |
| <code><a href="#@winglang/sdk.std.Json.keys">keys</a></code> | Returns the keys from the Json. |
| <code><a href="#@winglang/sdk.std.Json.parse">parse</a></code> | Parse a string into a Json. |
| <code><a href="#@winglang/sdk.std.Json.stringify">stringify</a></code> | Formats Json as string. |
Expand Down Expand Up @@ -230,30 +246,6 @@ map to get the entries from.

---

##### `has` <a name="has" id="@winglang/sdk.std.Json.has"></a>

```wing
Json.has(json: Json, key: str);
```

Checks if a Json object has a given key.

###### `json`<sup>Required</sup> <a name="json" id="@winglang/sdk.std.Json.has.parameter.json"></a>

- *Type:* <a href="#@winglang/sdk.std.Json">Json</a>

The json object to inspect.

---

###### `key`<sup>Required</sup> <a name="key" id="@winglang/sdk.std.Json.has.parameter.key"></a>

- *Type:* str

The key to check.

---

##### `keys` <a name="keys" id="@winglang/sdk.std.Json.keys"></a>

```wing
Expand Down Expand Up @@ -417,8 +409,10 @@ Mutable Json.
| <code><a href="#@winglang/sdk.std.MutJson.asBool">asBool</a></code> | Convert Json element to boolean if possible. |
| <code><a href="#@winglang/sdk.std.MutJson.asNum">asNum</a></code> | Convert Json element to number if possible. |
| <code><a href="#@winglang/sdk.std.MutJson.asStr">asStr</a></code> | Convert Json element to string if possible. |
| <code><a href="#@winglang/sdk.std.MutJson.delete">delete</a></code> | Removes the specified element from a map. |
| <code><a href="#@winglang/sdk.std.MutJson.get">get</a></code> | Returns the value associated with the specified Json key. |
| <code><a href="#@winglang/sdk.std.MutJson.getAt">getAt</a></code> | Returns a specified element at a given index from MutJson Array. |
| <code><a href="#@winglang/sdk.std.MutJson.has">has</a></code> | Checks if a Json object has a given key. |
| <code><a href="#@winglang/sdk.std.MutJson.set">set</a></code> | Adds or updates an element in MutJson with a specific key and value. |
| <code><a href="#@winglang/sdk.std.MutJson.setAt">setAt</a></code> | Set element in MutJson Array with a specific key and value. |
| <code><a href="#@winglang/sdk.std.MutJson.tryAsBool">tryAsBool</a></code> | Convert Json element to boolean if possible. |
Expand Down Expand Up @@ -453,6 +447,22 @@ asStr(): str

Convert Json element to string if possible.

##### `delete` <a name="delete" id="@winglang/sdk.std.MutJson.delete"></a>

```wing
delete(key: str): bool
```

Removes the specified element from a map.

###### `key`<sup>Required</sup> <a name="key" id="@winglang/sdk.std.MutJson.delete.parameter.key"></a>

- *Type:* str

The key.

---

##### `get` <a name="get" id="@winglang/sdk.std.MutJson.get"></a>

```wing
Expand Down Expand Up @@ -485,6 +495,22 @@ The index of the element in the MutJson Array to return.

---

##### `has` <a name="has" id="@winglang/sdk.std.MutJson.has"></a>

```wing
has(key: str): bool
```

Checks if a Json object has a given key.

###### `key`<sup>Required</sup> <a name="key" id="@winglang/sdk.std.MutJson.has.parameter.key"></a>

- *Type:* str

The key to check.

---

##### `set` <a name="set" id="@winglang/sdk.std.MutJson.set"></a>

```wing
Expand Down
12 changes: 10 additions & 2 deletions examples/tests/sdk_tests/std/json.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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()" {
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion examples/tests/valid/json.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/valid/json_static.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 0 additions & 12 deletions libs/wingc/src/lsp/snapshots/completions/json_statics.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>"
Expand Down
24 changes: 24 additions & 0 deletions libs/wingc/src/lsp/snapshots/completions/mut_json_methods.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions libs/wingc/src/lsp/snapshots/completions/optional_chaining.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonEntry>` — 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<str>` — 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<Json>` — 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<JsonEntry>` — 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<str>` — 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<Json>` — Returns the values from the Json."
range:
start:
line: 1
Expand Down
Loading

0 comments on commit 24178a4

Please sign in to comment.