yaml / yaml-spec

YAML Specification
http://yaml.org/spec/
348 stars 53 forks source link

Dotted keys #302

Closed ghost closed 1 year ago

ghost commented 1 year ago

TOML supports dotted keys:

physical.color = "orange"
physical.shape = "round"

JSON result:

{
  "physical": {
    "color": "orange",
    "shape": "round"
  }
}

https://toml.io/en/v1.0.0#keys

I tried this YAML:

physical.color: orange
physical.shape: round

but the resultant JSON is not desirable:

{
  "physical.color": "orange",
  "physical.shape": "round"
}

instead requiring explicit indentation:

physical:
   color: orange
   shape: round
Thom1729 commented 1 year ago

Not sure if this is a question or a feature request.

In YAML, physical.color is a plain scalar. It's no different from e.g. "physical.color", aside from a difference in tag resolution. As in JSON, there is no special handling for mapping keys that have dots in them. If a future version of YAML did treat dotted keys specially, that would be a breaking change, and I strongly suspect that there would be no appetite for such a change. (Also, because YAML's semantics are richer than JSON's or TOML's, such a change might be surprisingly complicated to specify and would certainly add additional failure points.)

However, if you're writing some application that consumes YAML documents, and it makes sense for your application to interpret dotted keys in a TOML-like way, then there's nothing stopping you from doing this at the application level. That is, you can load in a YAML document, and then transform it in your application to treat dotted keys however you like.

mhanuszh commented 1 year ago

While I was using and was aware of YAML for a while, I just started to dig into it, and oh boy, I fell in love instantly. As I started to use it for "real" projects, I came across things like this:

scope1:
  scope2:
    scope3:
      setting1: value

While this could have been described like this:

scope1.scope2.scope3.setting1: value

As @Thom1729 said, it would be a breaking change, so could this be considered as an opt-in feature? Maybe a new directive could be used for this?

Sometimes it's not possible to change the program that will consume our files, but would be beneficial to express such cases in a more compact way, while conforming the standard. (And without resulting to preprocessing our config files)

Is there any possibility that this feature request will be implemented? Or at least considered?


Alternative idea: It could be legal to have multiple Maps in a single line:

scope1: scope2: scope3: setting1: value 
ghost commented 1 year ago

closing this because I dont care anymore.

UnePierre commented 1 year ago

Also, you can always write with curly braces, if you really need a one-liner:

scope1: { scope2: { scope3: { setting1: value } } }