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

feat(sdk)!: add delete method for MutJson class #6236

Merged
merged 11 commits into from
Apr 18, 2024
21 changes: 21 additions & 0 deletions examples/tests/sdk_tests/std/json.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
partha04patel marked this conversation as resolved.
Show resolved Hide resolved
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);
}
13 changes: 13 additions & 0 deletions libs/wingsdk/src/std/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Loading