Tiago
Tiago

Reputation: 733

Override YAML node

I have an YAML file with some project configurations and I wish to reuse part of the code to keep it simple to maintain. So, I try to use anchors to do that but I'd like to override the previous nodes. Is that possible?

Below is my example:

default: &default
    output: &default_output
       make_video: true
       take_screenshot: true
    browser: &default_browser
       type: :chrome
       width: 1024
       height: 1280
    logger: &default_logger
       level: TRACE
       output_type: :file

chrome_browser: &chrome_browser
    <<: *default
    browser:
       type: :chrome
       user_agent: user_agent_string_for_chrome

firefox_browser: &firefox_browser
     <<: *default
     browser: 
         type: :firefox
         user_agent: user_agent_string_for_firefox

Ok, so this is the first part: is this possible to do? will the firefox_browser override the "type"?

Now the second part:

profile:
    <<: *default
    staging:
        europe:
            url: www.staging-europe-site.com
            chrome:
                <<: *browser_chrome
            firefox:
                <<: *browser_firefox
        america:
            url: www.staging-america-site.com
            chrome:
                <<: &browser_chrome
            firefox:
                <<: &browser_firefox                    
    live:
        europe:
            url: www.europe-site.com
            chrome:
                <<: &browser_chrome
            firefox:
                <<: &browser_firefox
        america:
            url: www.america-site.com
            chrome:
                <<: &browser_chrome
            firefox:
                <<: &browser_firefox

Can I do such a thing in order that, after reading the yaml, I could do:

profile_yaml['staging.europe.chrome'] 

and I get all the configurations?

Upvotes: 3

Views: 540

Answers (1)

Anthon
Anthon

Reputation: 76872

As for your first question, that doesn't do what you want as the value for key browser (from default) is replaced with the one define in chrome_browser. There is no tree merging or similar going on.

So for your second example, there you end up with:

output: 
   make_video: true
   take_screenshot: true
logger: 
   level: TRACE
   output_type: :file
browser:
   type: :chrome
   user_agent: user_agent_string_for_chrome

if you dump that back to YAML, which is probably also lacking. Of course there are other ways of achieving such goals, but you would have to help the parser to do so.

Upvotes: 2

Related Questions