Sanity checking an idea for editing yaml without wanting to throw my laptop out the window

3 years ago by flashgnash to c/technology

Often find myself getting frustrated editing yaml, and it seems to be used everywhere for some reason I cannot fathom

I have an idea to write an editor plugin that will, when opening a yaml file, convert it to json (or some other less painful configuration language), then convert back on save. I don't know enough about yaml syntax to know if that's possible or if there's some quirk that makes them not completely cross compatible

Or alternatively if it exists a better CLI tool for editing yaml than just a normal text editor because I'm getting sick of pasting in a block of yaml and then having to fix the 8 indentation errors that somehow spawn from that

bionicjoey 16 points 3 years ago

What text editor are you using? In theory most should detect a yaml file and adjust their indentation behaviour appropriately. I edit yaml in VSCode and Kate and it's never been a problem.

path: 0 9108825, hotness: undefined, score: 16, children: 5
flashgnash 0 points 3 years ago

I use nvim for it, it works but I'd rather not have to deal with indentation in the first place

path: 0 9108825 9112447, hotness: undefined, score: 0, children: 4
bionicjoey 5 points 3 years ago

Well in that case, I have good news. All valid JSON is also valid yaml. It's a superset by definition. So if you really hate yaml so much you can just write your yaml files as JSON and the parser should handle it fine. Just be aware that yaml does some more agressive typecasting when quotes are omitted, so you may need to figure out which value is actually being used when converting.

path: 0 9108825 9112447 9112528, hotness: undefined, score: 5, children: 3
flashgnash 2 points 3 years ago

Seriously? Chatgpt told me that once and I thought it was just hallucinating

Problem is it's for configuration of other services I didn't write, I would just use JSON in the first place if I were defining the schema

path: 0 9108825 9112447 9112528 9112659, hotness: undefined, score: 2, children: 2
bionicjoey 9 points 3 years ago

You should try looking up the language spec rather than hoping a text generator will generate truthful results. What's even the point of asking for answers if you don't trust that the thing you're asking won't just bullshit you?

path: 0 9108825 9112447 9112528 9112659 9113015, hotness: undefined, score: 9, children: 1
multifredding 6 points 3 years ago

I could see some problems when working with yaml anchors, otherwise the conversion should work fine. But yeah, using a good editor should fix most problems and would be much easier

path: 0 9109184, hotness: undefined, score: 6, children: 2
flashgnash 0 points 3 years ago

What kind of problems? I'm unfamiliar with anchors in yaml

path: 0 9109184 9112804, hotness: undefined, score: 0, children: 1
multifredding 1 point 3 years ago

They are basically constants where you can define reoccurring sections once and reference them multiple times throughout the file. A processor will then resolve these. As gar as I know, there is no comparable concept in JSON so they could get lost in translation

path: 0 9109184 9112804 9226698, hotness: undefined, score: 1, children: 0
Ltcpanic 3 points 3 years ago

Same bruv. I hear your cries of pain, sympathize greatly

path: 0 9110350, hotness: undefined, score: 3, children: 0
eleitl 2 points 3 years ago

Try neovim or something. Codium if you like GUIey things.

path: 0 9110782, hotness: undefined, score: 2, children: 2
flashgnash 2 points 3 years ago

I'm using neovim. It doesn't magically solve my problems with yaml though

path: 0 9110782 9112521, hotness: undefined, score: 2, children: 1
eleitl 1 point 3 years ago

I'm not a power yaml user by any means, but syntax highlighting and linting pipelines do help.

path: 0 9110782 9112521 9117299, hotness: undefined, score: 1, children: 0
bionicjoey 1 point 3 years ago

I have an idea to write an editor plugin that will, when opening a yaml file, convert it to json (or some other less painful configuration language), then convert back on save. I don't know enough about yaml syntax to know if that's possible or if there's some quirk that makes them not completely cross compatible

You could probably do this pretty easily with a simple python script. Use the yaml parser to convert into a dictionary, use the JSON renderer to save that dictionary into a pipe file. Launch the visual editor of your choice on that file. When the editor exits, read the file as JSON, parse it back into a dictionary, and use the yaml renderer to save that dictionary back into the original file.

path: 0 9113585, hotness: undefined, score: 1, children: 1
flashgnash 1 point 3 years ago

Yeah that was exactly what I was thinking, and/or doing a similar thing in lua as an nvim plugin

That said I'm not sure if neovim would support something like that

path: 0 9113585 9113625, hotness: undefined, score: 1, children: 0
wisplike_sustainer 0 points 3 years ago

YAML to JSON is probably doable, JSON back to YAML not so much.

There are multiple ways to mark multiline strings in YAML. Then there are anchors, like bionicjoey mentioned. Also comments, YAML has them. You'd have to have some way to retain the extra information, if you want to make the full round trip.

Here's an example:

def-db: &def-db
    # here be dragons
    login: admin
    passwd: nimda
    
prod:
    db: *def-db
    desc: |
        I'm a teapot
        short and stout

dev:
    db: 
        <<: *def-db
        passwd: pass
    desc: "I'm a teapot\nshort and stout\n"

converted to JSON looks like this

{
    "def-db": {
        "login": "admin",
        "passwd": "nimda"
    },
    "prod": {
        "db": {
            "login": "admin",
            "passwd": "nimda"
        },
        "desc": "I'm a teapot\nshort and stout\n"
    },
    "dev": {
        "db": {
            "login": "admin",
            "passwd": "pass"
        },
        "desc": "I'm a teapot\nshort and stout\n"
    }
}
path: 0 9110355, hotness: undefined, score: 0, children: 10
5C5C5C 2 points 3 years ago

All JSON is valid YAML, so after you've converted the file to JSON, just... save it with a YAML file extension and call it a day..?

path: 0 9110355 9113516, hotness: undefined, score: 2, children: 0
flashgnash 0 points 3 years ago

Comments are an issue I'd have to think about. Would prebuilt libraries for importing/exporting data from/to these languages not handle the multiline strings for me?

What do anchors do in yaml I've never heard of them before

path: 0 9110355 9112599, hotness: undefined, score: 0, children: 0
girsaysdoom 0 points 3 years ago

I think the difference is that it sounds they are just looking for something JSON-like, just enough to edit and save a change. It might not need to be valid.

path: 0 9110355 9111048, hotness: undefined, score: 0, children: 7
wisplike_sustainer 3 points 3 years ago

So, a new not-a-markup-language, only human readable and editable, and objectively better than its predecessor? Well, it's all according to tradition. I believe YAML got its start the same way.

path: 0 9110355 9111048 9111262, hotness: undefined, score: 3, children: 1
girsaysdoom 0 points 3 years ago

That's hilarious. I didn't know that

path: 0 9110355 9111048 9111262 9111291, hotness: undefined, score: 0, children: 0
flashgnash 0 points 3 years ago

No, I was thinking of actual JSON because it makes it easier to write a plugin (just use built in libraries to import and export configurations)

That said I suppose I could use the built in library and then re-inject comments after the fact

path: 0 9110355 9111048 9112474, hotness: undefined, score: 0, children: 4
girsaysdoom 1 point 3 years ago

Oh, my mistake. Disregard me then.

path: 0 9110355 9111048 9112474 9112720, hotness: undefined, score: 1, children: 3
flashgnash 0 points 3 years ago

Not a terrible idea to add bits into the JSON but it makes it much harder to program, going for a quick and easy fix here rather than writing my own parser

path: 0 9110355 9111048 9112474 9112720 9112796, hotness: undefined, score: 0, children: 2
technology
technology

@lemmy.world

login for more options
88742
22214
14627

This is a most excellent place for technology news and articles.

Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


go to feed...