Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk): improve delete() method for MutJson class #6278

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/docs/04-standard-library/std/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,16 @@ Convert Json element to string if possible.
##### `delete` <a name="delete" id="@winglang/sdk.std.MutJson.delete"></a>

```wing
delete(key: str): bool
delete(key: any): 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
- *Type:* any

The key.
Either the key or index to delete.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure we should allow deletion at an index. While it may seem like order is preserved in most JSON objects, this is not a guarantee, according to the json specification "An object is an unordered set of name/value pairs"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense for Json Arrays, since their order is preserved but perhaps in these cases its better to just convert types


---

Expand Down Expand Up @@ -498,14 +498,14 @@ 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
has(key: any): 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
- *Type:* any

The key to check.

Expand Down
22 changes: 22 additions & 0 deletions examples/tests/sdk_tests/std/json.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returning true feels off. Since "key5" is not a top level key of original it should be original["object"].delete("key5")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see why it feels off, technically the returning true makes sense since "key5" was never present as a top level key of original, but original["object"].delete("key5") should also return true as well with it actually deleting the present key

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions libs/wingsdk/src/std/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,13 @@ 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");
}
Expand All @@ -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");
}
Expand Down
Loading