elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.75k stars 303 forks source link

Allow parsing YAML documents (and streams) like we do with the "from-json" function #1155

Open seh opened 4 years ago

seh commented 4 years ago

Today it's possible to parse JSON in Elvish using the builtin from-json function. It would be helpful if we could also load YAML documents with an analogous from-yaml function. As people often write multiple YAML documents together in a single YAML stream, it would also be helpful to be able to load a YAML stream that would yield any number of documents (perhaps with a function called from-yaml-stream).

I understand that doing so would require introducing a dependency on a YAML parsing library. Whether we should provide functions to create a YAML document like to-json is debatable. Jsonnet's manifestYamlDoc function in its standard library has been controversial for the way it quotes strings in mapping keys.

krader1961 commented 4 years ago

Note that the project already depends on https://github.com/BurntSushi/toml for generating the blog web pages. While the project doesn't use that package in the elvish program the dependency does set a precedent of sorts.

@seh, Your opening comment doesn't appear to provide a real-world, concrete example showing why an Elvish program would employ this capability. Was there a problem you recently dealt with that would have benefited from a shell (Elvish or otherwise) being able to read YAML and convert it to the shell's native data structures?

seh commented 4 years ago

Was there a problem you recently dealt with that would have benefited from a shell (Elvish or otherwise) being able to read YAML and convert it to the shell's native data structures?

Yes, I frequently write small programs using bash or zsh that inspect Kubernetes manifests and some of the configuration files that go with them, such as the manifests the kops tool consumes. Some of these are static files, and some are the output of tools like helm template, kustomize build, or cue eval --out=yaml.

We have jq to handle our JSON needs for cases when the tool (e.g. kubectl get --output=jsonpath or aws --query) can't_ get us far enough. YAML processing tools are harder to come by; some just convert the YAML to JSON and send it through jq. It's possible to write small Python or Ruby programs do it, approaching one-liners, but then we're edging into just writing the whole program in that language.

When I saw that Elvish offers from-json, I immediately took note, thinking that this is more than just job control language, but could be a "real" programming language. I could use an external tool to convert YAML to JSON and then use Elvish's from-json function, but I'm left to wonder why we have this built-in way to consume JSON. If there were external, optional packages each dedicated to these separate languages (JSON, YAML, TOML, etc.), we wouldn't be propping any one of them higher than another.

rsteube commented 4 years ago

what's the benefit (excluding performance) of working directly on yaml instead of using a streaming yaml2json converter?:

fn from-yaml []{ yaml2json | from-json }
seh commented 4 years ago

As I wrote above:

I could use an external tool to convert YAML to JSON and then use Elvish's from-json function, but I'm left to wonder why we have this built-in way to consume JSON.

I'd rather not introduce any more tools on which to depend, such as yaml2json. Doing so imposes more installation requirements for my team. Rather, I was hoping to reduce the number of tools I'd need—as was once the dream with Perl.

rsteube commented 4 years ago

ah ok, i was wondering if there was another reason. well whether integrating it directly or as plugin would certainly be nice to have such functions out-of-the-box

just stumbled on yj and will settle with this for now:

fn from-yaml []{ yj -yj | from-json }
fn from-toml []{ yj -tj | from-json }
fn from-hcl []{ yj -cj | from-json }
fn to-yaml []{ to-json | yj -jy }
fn to-toml []{ to-json | yj -jt }
fn to-hcl []{ to-json | yj -jc }
aca commented 2 years ago

I think this will actually be great if implemented. We handle quite a lot of yaml. But as far as I know there's no scripting language that natively supports yaml and also yq implementations out there does not seem to be stable like jq. If elvish can handle all kinds of this, I can just install elvish and handle all in elvish scripts without other dependancies.