这个database.yml 文件中的&、<<、* 是什么意思?

发布于 2024-11-19 20:05:22 字数 408 浏览 4 评论 0 原文

到目前为止,我只使用了 database.yml 并明确调用了每个参数,在下面的文件中使用了一些我不理解的字符。每行和符号(&*<<)的含义是什么?我如何读取这个文件?

development: &default
  adapter: postgresql
  database: dev_development

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


cucumber:
  <<: *test

production:
  <<: *default
  database: test_production

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ま柒月 2024-11-26 20:05:22

& 标记节点的别名(在示例中 &default 将开发节点别名为“default”)和 * 引用名为“default”的别名节点。 <<: 插入该节点的内容。

请允许我在这里引用 YAML 规范:

重复的节点(对象)首先由锚点(用与号标记 - “&”)标识,然后使用别名(用星号 - “*”引用)。

因此,示例的部分内容

development: &default
  adapter: postgresql
  database: dev_development

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

实际上扩展为

development: &default
  adapter: postgresql
  database: dev_development

test: &test
  adapter: postgresql       # from the "default" alias
  database: test_test       # overridden by the duplicate key

“test”节点,同时也使“test”节点在别名“test”下可用。

查看 YAML 规范 - 2.2 结构 了解更多详细信息(或如果您甚至需要摩尔文档++: 3.2.2.2. 锚点和别名)

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

development: &default
  adapter: postgresql
  database: dev_development

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

actually expand to

development: &default
  adapter: postgresql
  database: dev_development

test: &test
  adapter: postgresql       # from the "default" alias
  database: test_test       # overridden by the duplicate key

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)

明媚殇 2024-11-26 20:05:22

&default 表示您使用某个名称来标记这组属性以供以后使用

<<: *default 表示您包含标记为的组中的所有属性默认

&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

猫瑾少女 2024-11-26 20:05:22

这些表示节点引用 (*) 和关联数组合并 (<<),它们引用标有锚点 (&) 标签的节点 - 维基百科

在线亲自尝试一下。

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

Try it out yourself online.

海未深 2024-11-26 20:05:22

它们是一种参考环境的方法,而无需一遍又一遍地重复相同的设置(干燥)。

test: &test
  <<: *default

&test 创建对这些特定设置的引用。

<<: *default 表示使用默认设置进行测试

cucumber:
  <<: *test

现在我们知道对于 cucumber 我们想要使用 test 中的设置>。

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.

北方的巷 2024-11-26 20:05:22

简而言之,这个概念类似于基类和派生类。

在基类模板中,您使用“&”提及所有常见详细信息,这意味着它可用于扩展需要这些字段的其他 yaml 部分。
现在,当您创建另一个部分(该部分是此“基类”类型结构的配置值的超集)时,您可以使用“*”和基类锚点(即以“&”开头的锚点)。您使用“<<:”作为 yaml 概念来实际放置“基类”部分,您可以稍后覆盖。

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

但是,如果您不想覆盖扩展字段,则可以跳过 '<<:'

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 '<<:'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文