What do the &,<<, * mean in this database.yml file?

谁说胖子不能爱 提交于 2019-11-28 13:41:54

问题


Up until now I have only used database.yml with each parameter called out explicitly, in the file below it uses some characters I do not understand. What does each line and symbol(&,*,<<) mean, how do i read this file?

development: &default
  adapter: postgresql
  database: dev_development

test: &test
  <<: *default
  database: test_test


cucumber:
  <<: *test

production:
  <<: *default
  database: test_production

回答1:


The & marks an alias for the node (in your example &default aliases the development node as "default") and the * references the aliased node with the name "default". The <<: inserts the content of that node.

Allow me to quote the YAML spec here:

Repeated nodes (objects) are first identified by an anchor (marked with the ampersand - “&”), and are then aliased (referenced with an asterisk - “*”) thereafter.

so parts of your example

test: &test
  <<: *default

actually expand to

test: &test
  adapter: postgresql       # from the "default" alias
  database: dev_development # from the "default" alias

and at the same time make the "test" node as well available under the alias "test". have a look at the YAML specification - 2.2 Structures for further details (or if you need even moar docs++: 3.2.2.2. Anchors and Aliases)




回答2:


&default means you're labeling this set of attributes with some name for later use

<<: *default means you're including all attributes from group labeled as default




回答3:


These represent node references (*) and associative array merges (<<) that refer to a node labeled with an anchor (&) tag -- wikipedia

Try it out yourself online.




回答4:


They are a way to reference environments without having to repeat the same settings over and over (DRY it up).

test: &test
  <<: *default

&test creates a reference to those specific settings.

<<: *default says use the default settings for the test

cucumber:
  <<: *test

So now we know that for cucumber we want to use the settings from test.




回答5:


In simple words, this notion resembles with the base and derived class.

In base class template, you mention all the common details with '&', which means it can be used to expand the other yaml section that needs these fields. Now when you create another section that is superset of config values of this 'base class' type structure, you use the '*' along with the base class anchor (i.e. the one started with '&'). You use '<<:' as yaml notion for actually placing the 'base class' section, that you can override later.

vsm:
  stub_nsx_mgr: &MGR_CTRL_STUB
    username: ADMIN
    password: $DEFAULT_PASSWORD
    deployment: ovf
    build: $PR_BUILD
    vmnics:
      - network: $MANAGEMENT_NETWORK_0
    vc: vc_0
    ovf_options:
      - --diskMode=$DISKMODE
      - --deploymentOption=$DEPLOYMENT_OPTION
$MGR_0:
    <<: *MGR_CTRL_STUB
    ovf_path_regex: 'appliance.*\.ovf'
    ovf_options:
      - --diskMode=$DISKMODE
      - --deploymentOption=$DEPLOYMENT_OPTION
$CTRL_0:
    <<: *MGR_CTRL_STUB
    ovf_options:
      - --diskMode=$DISKMODE
      - --allowExtraConfig
$CTRL_1:
    *MGR_CTRL_STUB

But, if you do not want to override the extended fields, you can skip '<<:'



来源:https://stackoverflow.com/questions/6651275/what-do-the-mean-in-this-database-yml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!