From 9fd6254f499431a83f218c59265520437967e140 Mon Sep 17 00:00:00 2001 From: Fraser Waters Date: Mon, 26 Dec 2022 22:09:32 +0000 Subject: [PATCH] Add jsonParse to nodejs sdk --- .../20221226--sdk-nodejs--add-jsonparse.yaml | 4 +++ sdk/nodejs/output.ts | 9 +++++++ sdk/nodejs/tests/output.spec.ts | 26 ++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 changelog/pending/20221226--sdk-nodejs--add-jsonparse.yaml diff --git a/changelog/pending/20221226--sdk-nodejs--add-jsonparse.yaml b/changelog/pending/20221226--sdk-nodejs--add-jsonparse.yaml new file mode 100644 index 000000000000..6a0cbd729c8e --- /dev/null +++ b/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. diff --git a/sdk/nodejs/output.ts b/sdk/nodejs/output.ts index 622beac80d44..0a152913146d 100644 --- a/sdk/nodejs/output.ts +++ b/sdk/nodejs/output.ts @@ -1022,3 +1022,12 @@ export function jsonStringify(obj: Input, 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, reviver?: (this: any, key: string, value: any) => any): Output { + return output(text).apply(t => { + return JSON.parse(t, reviver); + }); +} diff --git a/sdk/nodejs/tests/output.spec.ts b/sdk/nodejs/tests/output.spec.ts index a4f32f35e3f1..fca7b4f9ae64 100644 --- a/sdk/nodejs/tests/output.spec.ts +++ b/sdk/nodejs/tests/output.spec.ts @@ -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"; @@ -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");