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

feat: allow multiple documents in std.parseYaml #205

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

alexatcanva
Copy link

Heya Team 👋

While trying to give sjsonnet a whirl, we noticed a few of our std.parseYaml calls were failing if they included multi doc YAML. (deliminated by the old ---).

This PR allows the yamlToJson function to parse these files/YAML streams and return the appropriate JSON string.

I'm a first time contributor to this repo, and furthermore it's my first time writing Scala.

Please do let me know if there's a better way to accomplish this, I'm more than happy to work through any of the feedback I receive!

Thanks!

@alexatcanva
Copy link
Author

Heya @lihaoyi-databricks 👋

Is there any chance I could please get a review on this one? ❤️ Thanks!

val yaml: Seq[Object] = new Yaml(new Constructor()).loadAll(yamlString).asScala.toSeq
yaml match {
case l if(l.size > 1) => new JSONArray(yaml.asJava).toString()
case _ => new JSONObject(yaml.iterator.next().asInstanceOf[java.util.LinkedHashMap[String, Object]]).toString()
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no need to call iterator, the seq can access value with seq(1)

This can throw NPE or NoSuchElementException when the yaml is null or empty.

Jsonnet has a ujson, maybe we should use that ?

Test cases?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @He-Pin !

I've performed a small refactor of the function; I did explore using ujson instead of the JSONArray and JSONObject types. However, I ran into a bit of trouble trying to serialize an arbitrary Seq into the ujson.Arr or ujson.Obj? types.

Looking around there wasn't much documentation under "Serializing Scala data types into ujson", and my GitHub-fu of taking a look throughout the repo/other opensource projects let me down a little there 😓

If you have any pointers on as to how I can serialize a Seq[Object] into a ujson.Obj or ujson.Arr elegantly then I'd be more than happy to follow up with some more commits, let me know! ❤️

Thank you again for the review also! 🙏

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.

2 participants