4

Say I have a YML file original.yml with an array of objects

array_in_yml:
- start: 1
- middle: 2
- end: 3

I included it in modified.yml

!include "original.yml" 
array_in_yml: []

I am expecting this array to be empty when I load the modified.yml, but it seems to have 3 values as original.yml. How do I force/override the array to be empty?

mkporkodi
  • 81
  • 1
  • 5
  • Are you actually using `yaml-cpp`? I don't think it supports the `!include` feature, so I'd be surprised that it loads 3 values. If you are, please post working source code that shows the issue. – Jesse Beder Dec 18 '16 at 00:26
  • `modified.yml` is a not a valid YAML file, you cannot have both a scalar string `"original.yml"` tagged `!include` at the toplevel of the file, as well as a key-value pair indicating a mapping (`array_in_yml: []`). You should include a minimal working program that shows what you are seeing. – Anthon Dec 18 '16 at 05:18
  • You `original.yml` file has no array of objects. At the toplevel it has a mapping, with one key-value pair. The value is a sequence of which all the elements are mappings, each with one key-value pair. **There is no array and there are no objects**. – Anthon Dec 18 '16 at 05:21
  • @Anthon, there is a top-level object with a property called `array_in_yml`. The `-` hyphen character begins a new array element. So the value of the property `array_in_yml` is an array of three objects, each having a single property. – Ted Epstein Dec 18 '16 at 15:36
  • @TedEpstein A YAML document consists of [*collections*](http://yaml.org/spec/1.2/spec.html#id2759963) (*mappings* and *sequences*), [*scalars*](http://yaml.org/spec/1.2/spec.html#id2760844) and [*tags*](http://yaml.org/spec/1.2/spec.html#id2761292). I don't see any objects, arrays, etc. there. You seem to be mixing in the terminology of the representation of a YAML file loaded in your programming language. That is like talking about `` in an HTML file as being an Aardvark (instead of a tag), just because your programming language loads it into such a beast. Confusing, if not worse. – Anthon Dec 18 '16 at 17:52
  • @Anthon, I'm using terminology from JSON (object, array) and JSON Schema (property). YAML has a well-defined relationship with JSON. Many of us use YAML as shorthand for JSON, and use JSON Schema to validate or describe YAML documents. Standards like Swagger-OpenAPI have made this pretty common. Since the the original question used JSON, I didn't think it was confusing to stick with that. But if you think the JSON abstraction is getting in the way of understanding and asnwering the question, then you're right to steer us back to YAML's native terminology and concepts. – Ted Epstein Dec 18 '16 at 19:35
  • Thanks for the thoughts. I just realized the override mechanism was a method provided by our internal API which a wrapper around yaml-cpp. I was able to resolve my issue. – mkporkodi Dec 19 '16 at 22:06

1 Answers1

2

The discussion about !include seems to lead a bit away from the actual question. Let's assume that in some unknown way, the !include line gets replaced with the content in original.yml. We would have:

array_in_yml:
- start: 1
- middle: 2
- end: 3
array_in_yml: []

This is not valid YAML, since every key in a dictionary must be unique, but you use the key array_in_yml twice. Your YAML processor might ignore this and simply assign the first value (which is a sequence of three items) to the key array_in_yml.

Now the important part: There is no way in YAML to modify previously given values. You cannot override a value given previously with a different one. What you want to do is outside the YAML spec and you would need some merging tool that does such replacements for you.

flyx
  • 35,506
  • 7
  • 89
  • 126
  • Thanks for the thoughts. I just realized the override mechanism was a method provided by our internal API which a wrapper around yaml-cpp. – mkporkodi Dec 19 '16 at 22:05