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

Add jsonParse to nodejs sdk #11735

Merged
merged 1 commit into from Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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