Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an inherit property from yaml config?

I have a yaml file like this:

local: &local
  image: xxx
  # *tons of config*

ci:
  <<: *local
  image: # delete
  build: .

I want ci to inherit all values from local, except the image.

Is there a way to "delete" this value?

like image 457
BrunoLM Avatar asked Dec 03 '16 12:12

BrunoLM


People also ask

How do I use inheritance in YAML?

On the first line of your YAML definition, use the ! inherit directive followed by a colon and the identifier of the definition you want to inherit from.

How do I edit a YAML file?

You can open a YAML file in any text editor, such as Microsoft Notepad (Windows) or Apple TextEdit (Mac). However, if you intend to edit a YAML file, you should open it using a source code editor, such as NotePad++ (Windows) or GitHub Atom (cross-platform).

Can I include a YAML file inside another?

No, standard YAML does not include any kind of "import" or "include" statement. You could create a ! include <filename> handler.


2 Answers

For properties that accept a list of values, you can send [] as value.

For example in docker-compose you don't want to inherit ports:

service_1:  &service_1
    # some other properties.
    ports:
      - "49281:22"
      - "8876:8000"
    # some other properties

service_2:
    <<: *service_1
    ports: []  # it removes ports values.

like image 68
msln Avatar answered Oct 23 '22 07:10

msln


No there isn't a way to mark a key for deletion in a YAML file. You can only overwrite existing values.

And the latter is what you do, you associate the empty scalar as value to the key image as if you would have written:

  image: null   # delete

There are two things you can do: post-process or make a base mapping in your YAML file.

If you want to post-process, you associate a special unique value to image, or a specially tagged object, and after loading recursively walk over the tree to remove key-value pairs with this special value. Whether you can already do this during parsing, using hooks or overwriting some of its methods, depends on the parser.

Using a base mapping requires less work, but is more intrusive to the YAML file:

localbase: &lb
  # *tons of config*

local: &local
  image: xxx

ci:
  <<: *lb
  build: .

If you do the former you should note that if you use a parsers that preserve the "merge-hierarchy" on round-tripping (like my ruamel.yaml parser can do) it is not enough to delete the key-value pair, in that case the original from local would come back. Other parsers that simply resolve this at load time don't have this issue.

like image 35
Anthon Avatar answered Oct 23 '22 07:10

Anthon