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

Made JSON encoder a public function #443

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

eastside
Copy link

@eastside eastside commented Dec 8, 2022

This one is pretty simple. I found myself starting to write a bunch of code to encode a Starlark value as JSON, when I realized that oh hey, there's a JSON encoder already in the project. It's just hidden.

@google-cla
Copy link

google-cla bot commented Dec 8, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

lib/json/json.go Outdated Show resolved Hide resolved
lib/json/json.go Outdated
return nil, err
}

// Encodes the given Starlark value as a string in JSON format.
Copy link
Collaborator

Choose a reason for hiding this comment

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

// Encode returns the JSON encoding of a Starlark value.

lib/json/json.go Outdated Show resolved Hide resolved
lib/json/json.go Outdated
if err != nil {
return nil, fmt.Errorf("%s: %v", b.Name(), err)
}
return starlark.String(s), nil
return starlark.String(string(bytes[:])), nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

String(bytes)

@eastside
Copy link
Author

Two questions:

First, I wasn't sure if Decode should also accept []byte, to have some symmetry with Encode.

Second, I wasn't sure if I should add some tests. I started to add some, but realized I was testing basically everything that's already being tested.

@adonovan
Copy link
Collaborator

First, I wasn't sure if Decode should also accept []byte, to have some symmetry with Encode.

Yes, I think it should.

Second, I wasn't sure if I should add some tests. I started to add some, but realized I was testing basically everything that's already being tested.

You could add one test to ensure that a value can be encoded and then decoded to ensure there are no gross errors, but I wouldn't bother testing all the cases of JSON.

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.

None yet

2 participants