Skip to content

Commit

Permalink
Merge #11735
Browse files Browse the repository at this point in the history
11735: Add jsonParse to nodejs sdk r=Frassle a=Frassle

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

Partner method to jsonStringify.

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [x] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Fraser Waters <fraser@pulumi.com>
  • Loading branch information
bors[bot] and Frassle committed Dec 28, 2022
2 parents 1f220ca + 9fd6254 commit 497ebc5
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 497ebc5

Please sign in to comment.