Skip to content

Commit

Permalink
Add jsonParse to nodejs sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Frassle committed Dec 28, 2022
1 parent e10ad14 commit 9fd6254
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelog/pending/20221226--sdk-nodejs--add-jsonparse.yaml
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: sdk/nodejs
description: Add output jsonParse using JSON.parse.
9 changes: 9 additions & 0 deletions sdk/nodejs/output.ts
Expand Up @@ -1022,3 +1022,12 @@ export function jsonStringify(obj: Input<any>, replacer?: (this: any, key: strin
return JSON.stringify(o, replacer, space);
});
}

/**
* [jsonParse] Uses JSON.parse to deserialize the given Input JSON string into a value.
*/
export function jsonParse(text: Input<string>, reviver?: (this: any, key: string, value: any) => any): Output<any> {
return output(text).apply(t => {
return JSON.parse(t, reviver);
});
}
26 changes: 25 additions & 1 deletion sdk/nodejs/tests/output.spec.ts
Expand Up @@ -15,7 +15,7 @@
/* eslint-disable */

import * as assert from "assert";
import { Output, all, concat, interpolate, output, unknown, secret, unsecret, isSecret, jsonStringify } from "../output";
import { all, concat, interpolate, isSecret, jsonParse, jsonStringify, Output, output, secret, unknown, unsecret } from "../output";
import { Resource } from "../resource";
import * as runtime from "../runtime";

Expand Down Expand Up @@ -924,6 +924,30 @@ describe("output", () => {
});
});

describe("jsonParse", () => {
it ("basic", async () => {
const x = output("[0, 1]")
const result = jsonParse(x)
assert.deepStrictEqual(await result.promise(), [0, 1]);
assert.strictEqual(await result.isKnown, true);
assert.strictEqual(await result.isSecret, false);
});

it ("with reviver", async () => {
const reviver = (key: string, value: any ): any => {
if (key === "bob") {
return "goodbye";
}
return value;
}
const x = output("{\"bob\": \"hello\"}")
const result = jsonParse(x, reviver)
assert.deepStrictEqual(await result.promise(), { bob: "goodbye" })
assert.strictEqual(await result.isKnown, true);
assert.strictEqual(await result.isSecret, false);
});
});

describe("secret operations", () => {
it("ensure secret", async () => {
const sec = secret("foo");
Expand Down

0 comments on commit 9fd6254

Please sign in to comment.