Skip to content

feat: add JSON functions that work with dynamic values#5259

Merged
wolffcm merged 4 commits intomasterfrom
feat/dynamic-json-parse
Oct 3, 2022
Merged

feat: add JSON functions that work with dynamic values#5259
wolffcm merged 4 commits intomasterfrom
feat/dynamic-json-parse

Conversation

@wolffcm
Copy link

@wolffcm wolffcm commented Oct 3, 2022

This PR adds two new functions to the experimental/dynamic stdlib package:

  • jsonParse which produces dynamic values.
  • jsonEncode which encodes JSON bytes into dynamic values.

I had to cherry-pick a commit from #5239 to fix type inference to allow calling functions that accept dynamic arguments. I also found an issue where map does not deal with dynamic types, but this was an easy one-line fix.

Fixes #5141.

Checklist

Dear Author 👋, the following checks should be completed (or explicitly dismissed) before merging.

  • ✏️ Write a PR description, regardless of triviality, to include the value of this PR
  • 🔗 Reference related issues
  • 🏃 Test cases are included to exercise the new code
  • 🧪 If new packages are being introduced to stdlib, link to Working Group discussion notes and ensure it lands under experimental/
  • 📖 If language features are changing, ensure docs/Spec.md has been updated (N/A)

Dear Reviewer(s) 👋, you are responsible (among others) for ensuring the completeness and quality of the above before approval.

@wolffcm wolffcm requested review from a team as code owners October 3, 2022 21:15
@wolffcm wolffcm requested review from lwandzura and nathanielc and removed request for a team October 3, 2022 21:15
@wolffcm wolffcm changed the title Feat/dynamic json parse feat: add JSON functions that work with dynamic values Oct 3, 2022
@wolffcm wolffcm requested a review from onelson October 3, 2022 21:16
@sanderson sanderson requested review from sanderson and removed request for lwandzura October 3, 2022 21:17
Copy link
Contributor

@nathanielc nathanielc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with two small fixes

Copy link
Contributor

@sanderson sanderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor doc suggestions.

@wolffcm wolffcm force-pushed the feat/dynamic-json-parse branch from a00c57b to 2948d80 Compare October 3, 2022 21:35
Copy link
Contributor

@onelson onelson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Love to see these good tests!

arr := v.Array()
a := make([]interface{}, arr.Len())
var rangeErr error
arr.Range(func(i int, v values.Value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered whether I should use Array.Range or just use a plain loop while I was looking at asArray. I opted for "plain loop" just because it seemed more minimal. What made you prefer it?

My guess: in this function we're iterating over different collection types (array, dict, object) and the Range() method might make this work more uniform.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code I mostly copied from the non-dynamic implementation of encode. I only had to add the case to semantic.Dynamic above. So it was like this already. Looking at the alternative, I can't say that I have a strong preference.

I considered just changing the regular version of json.encode to be able to handle dynamic, since it's an additive change that shouldn't break with existing behavior, but decided it would be more prudent to copy it.

Copy link
Contributor

@sanderson sanderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌🏻

@wolffcm wolffcm merged commit cd90646 into master Oct 3, 2022
@wolffcm wolffcm deleted the feat/dynamic-json-parse branch October 3, 2022 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Dynamic version of json functions

4 participants