Live data from Hacker News

Why are we templating YAML?

leebriggs.co.uk

41–50 of 351 posts

Re: Why are we templating YAML?

#41
post #10

Earlier quoted context omitted.

It's a lot easier for end-users to read and write by hand. If that is not a requirement, a simpler machine-friendly format is likely better.

Writing YAML is not easy for end-users. The indentation--especially in large files--is really difficult to keep track of, and some really basic stuff like when to quote strings is not consistent at all.

It definitely is. You have no idea how intimidating an equivalent JSON file would be for a new user creating a pretty simple k8s setup.

YAML hides most of the scary brackets and quotes so that new users can focus on copy-and-pasting semantic tidbits.

That said... I don't like YAML...

Re: Why are we templating YAML?

#42
post #38
post #8

Earlier quoted context omitted.

White space matters with Python but I rarely run into issues with indentation in Python. But YAML on the other hand, I have nothing but nightmares.

I think its because python has very strict and very simple indentation rules (a nested block must be indented, and the file requires consistent indentation. Is there anything else?) YAML gives you options, or varies necessity somewhat arbitrarily on the structures, which is (marginally) good for reading, but a lot of headache in writing

Also because the longer the YAML file, the bigger your indentation gets. It gets exponentially worse as you go.

Re: Why are we templating YAML?

#43
post #19

Earlier quoted context omitted.

The best thing that ever happened to Cloud Formation was the new YAML syntax. Writing YAML is easy with a good editor like VSCode. Install the YAML code outline extension for sugar; works great for OpenAPI specs. YAML flow style offers some good options for keeping the file compact.

I'm all for good editor support, but you shouldn't need it to write a simple document. Complicated editors should be supporting tools at best, because you won't always be in a position to use one. The things we write should be simple enough to be written and understood by hand with minimal mistakes (then we can make it even easier in some cases using nice editors and tooling).

I don't think I've ever used anything under than vanilla vim for editing YAML files... with next to zero issues.

The problem for me is that the multiple ways to do the same thing result in something that's pretty opaque to clear specifications that aren't just very simple examples / structures.

Re: Why are we templating YAML?

#44
post #34

Earlier quoted context omitted.

A misplaced comma is also not a problem in an editor with syntax highlighting, but between the two, indentation could take longer to fix.

Syntax highlighting doesn't count commas. The problem is JSON doesn't allow trailing commas, so adding or reordering frequently results in a difficult to diagnose error.

Finding out if a set ends in a comma or not still makes it easier to debug than YAML.

Re: Why are we templating YAML?

#45

The real question is why are we using yaml at all?

Easier to read and write than JSON.

JSON requires constant quoting, can't support multiline strings, has no comments, has no/little typing (e.g., no datetime type). It's not good if a human needs to encode data.

For configs, I think TOML beats YAML hands down; I think YAML's spot is at encoding data structures that humans need to read/write.

I do agree that YAML, the spec, is fairly complicated. But YAML, as used in most projects, by most people, is not, and can be picked up fairly quickly. It is easier to visually read as it removes much of the clutter that would exist in the comparable JSON. It isn't typically necessary to know the entirety of the YAML spec to be useful with YAML, and most of the parts you won't know will get introduced by an obvious-looking sigil, which can be used to figure out what you're dealing with.

When I've actually sat with folks struggling with YAML, it's almost always in configuration tools, and it's also always around the templating bits. Ansible, in particular, has a bizarre templating: it happens after YAML parsing, which is not the mental model most people use when approaching it. I've also found that most of the people I've spoken to intertwine YAML and Ansible's templating functions, thinking they're one in the same.

I do not think Ansible makes good use of YAML: I would rather write task files in an actual programming language, since they are — at their core — a program. (The tasks do have some metadata attached to them, but the core task itself is a program. A function in some real language can get metadata attached to it in a number of ways, and that would be a better solution.)

Re: Why are we templating YAML?

#46
post #12

Earlier quoted context omitted.

But the simpler format of JSON should be easier to write and read.

No comments, constant need to quote every string, inane comma requirements, no multiline string, little typing support.

Many language require strings to be quoted, and calling the comma requirements inane is an opinion, not an objective fact.

You're not saying anything about the readability of one versus another, you're just listing gripes you have about JSON.

Re: Why are we templating YAML?

#47
post #6

I think they are missing the real selling point of JSON. It's basically interoperable with JavaScript objects. That means you write it, send it, store it, operate on it, etc. with little or no modification. The author says "converting between the two is trivial" which may be true, but the developer overhead is less trivial. And it will always be JSON in the client - JS doesn't support YAML objects.

Except that you cannot encode pretty valid Double.NaN or Date(), which is showstopper for many. I remember mongodb started with json, but switched to in-house bson pretty early because of the json limitations.

new Date().toJSON()

Re: Why are we templating YAML?

#48

The real question is why are we using yaml at all?

Because json is poorly suited for config files, and it is very convenient to have config files that closely mirror apis, like kubernetes does. I've seen too many teams who try to use json for config end up developing yet another custom superset of json to allow comments or multiline strings or such. If you are going to use a superset of json might as well use an existing one like yaml or json5.

That said I agree with all the criticisms of templating yaml. We have to do the same (with helm and other tools), and I have pushed hard to adopt conventions that we only use flow-style and not block-style to avoid all the white space problems when splicing together chucks of yaml. And on the plus side we get trailing commas and other such niceties which don't exist in json and make it harder template.

Re: Why are we templating YAML?

#49
post #44

Earlier quoted context omitted.

Syntax highlighting doesn't count commas. The problem is JSON doesn't allow trailing commas, so adding or reordering frequently results in a difficult to diagnose error.

Finding out if a set ends in a comma or not still makes it easier to debug than YAML.

Not my experience, 10x more trouble with long JSON.

Re: Why are we templating YAML?

#50
If you have been around long enough you still remember the world that was excited about XML and templating it using XSLT. As a hindsight it was a horrible world.

Even though YAML is not optimal, it is a human friendly compromise between too verbose XML and machine only JSON. It lacks native templating, leading to funny constructs e.g. with Ansible files. However human kind has made progress and will make progress further, so it is just a matter of time until someone comes up with sane "native templated YAML" and all projects will adopt it.

Post reply on HN