Skip to content

Commit

Permalink
fix(sdk): MutJson methods .set(), .setAt() accept any, not `M…
Browse files Browse the repository at this point in the history
…utJson` (#2901)

Changed `MutJson` methods `.set()`, `.setAt()` to accept `MutJson`  and not
Fixes issue #2767  

## Checklist

- [ ] Title matches [Winglang's style guide](https://docs.winglang.io/contributors/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] 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 [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
  • Loading branch information
revitalbarletz committed Jun 18, 2023
1 parent 917a4fa commit dbd363e
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 13 deletions.
8 changes: 4 additions & 4 deletions docs/04-standard-library/04-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4530,7 +4530,7 @@ The index of the element in the MutJson Array to return.
##### `set` <a name="set" id="@winglang/sdk.std.MutJson.set"></a>

```wing
set(key: str, value: any): void
set(key: str, value: MutJson): void
```

Adds or updates an element in MutJson with a specific key and value.
Expand All @@ -4545,7 +4545,7 @@ The key of the element to add.

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

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

The value of the element to add.

Expand All @@ -4554,7 +4554,7 @@ The value of the element to add.
##### `setAt` <a name="setAt" id="@winglang/sdk.std.MutJson.setAt"></a>

```wing
setAt(index: num, value: any): void
setAt(index: num, value: MutJson): void
```

Set element in MutJson Array with a specific key and value.
Expand All @@ -4567,7 +4567,7 @@ Set element in MutJson Array with a specific key and value.

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

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

The value of the element to set.

Expand Down
50 changes: 49 additions & 1 deletion examples/tests/sdk_tests/std/json.w
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
//-----------------------------------------------------------------------------
// TODO: https://github.com/winglang/wing/issues/2785
// TODO: https://github.com/winglang/wing/issues/2785
//-----------------------------------------------------------------------------
bring cloud;

// set() & get()
let a = MutJson { a: 1 };
let b = MutJson { b: 2 };
a.set("c", b);

let c = a.get("c");
assert(c.get("b") == 2);

test "set()" {
let x = MutJson { a: 1 };
x.set("b", 2);
let y = x.get("b");
assert(y == 2);
}

//-----------------------------------------------------------------------------
// setAt() & getAt()
let d = MutJson { d: 3 };
a.setAt(2, d);
let e = a.getAt(2);
assert(e.get("d") == 3);

test "setAt()" {
let x = MutJson { a: 1 };
let a = MutJson { c: 3 };
x.setAt(2, a);
let d = x.getAt(2);
assert(d.get("c") == 3);
}

/*
Will add test later:
test "setWithNonMutJsonObject()" {
let var error = "";
try {
let f = MutJson { e: 4 };
f.set("f", 9);
} catch e {
error = e;
}

log(error);
assert(error == "");
}
*/
8 changes: 4 additions & 4 deletions libs/wingsdk/API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libs/wingsdk/src/std/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class MutJson {
* @param key The key of the element to add
* @param value The value of the element to add
*/
public set(key: string, value: any): void {
public set(key: string, value: MutJson): void {
key;
value;
throw new Error("Macro");
Expand All @@ -219,7 +219,7 @@ export class MutJson {
*
* @param value The value of the element to set
*/
public setAt(index: number, value: any): void {
public setAt(index: number, value: MutJson): void {
index;
value;
throw new Error("Macro");
Expand Down
Loading

0 comments on commit dbd363e

Please sign in to comment.