简体   繁体   English

大胆的占位符如何工作?

[英]How does swagger placeholders work?

​I have joined a project which uses Swagger. ``我加入了一个使用Swagger的项目。 I see they have​​ used this kind of placeholders in the Swagger.yaml : 我看到他们在Swagger.yaml使用了这种占位符:

  MYKey: &CONST_MY_KEY ""

How does this work and where do I define CONST_MY_KEY ? 这是如何工作的,以及在哪里定义CONST_MY_KEY

This is a YAML construct known as an anchor and is not specific to swagger. 这是一个称为锚的YAML构造,并非专门针对摇摇欲坠。

The code you wrote actually defines the anchor CONST_MY_KEY as an empty string. 您编写的代码实际上将锚点CONST_MY_KEY定义为空字符串。 The & -prefix indicates that you are defining it. & -prefix表示您正在定义它。 If you wanted to reuse this anchor later in the document you would reference it using a * -prefix instead like *CONST_MY_KEY . 如果您想稍后在文档中重用此锚,则可以使用*前缀而不是*CONST_MY_KEY来引用它。

Below is an explanatory excerpt from https://learnxinyminutes.com/docs/yaml/ : 以下是来自https://learnxinyminutes.com/docs/yaml/的说明性摘录:

# YAML also has a handy feature called 'anchors', which let you easily duplicate
# content across your document. Both of these keys will have the same value:
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name

# Anchors can be used to duplicate/inherit properties
base: &base
    name: Everyone has same name

foo: &foo
  <<: *base
  age: 10

bar: &bar
  <<: *base
  age: 20

You don't define CONST_MY_KEY anywhere, the & introduces this as an anchor for the object "" . 您无需在任何地方定义CONST_MY_KEY&将其引入为对象""的锚点。 You can reuse this in a later stage of a YAML file by using * to specify an alias: *CONST_MY_KEY . 通过使用*指定别名: *CONST_MY_KEY可以在YAML文件的后续阶段中重新使用*CONST_MY_KEY

The primary reason for anchors and aliases is not to duplicate content as @smartcaveman's answer indicates. 锚杆和别名的主要原因重复的内容作为@ smartcaveman的回答表示。 It is to represent a node in multiple locations in the representation graph . 它是要在表示图中的多个位置表示一个节点

Without this feature even the following simple Python construct could not be dumped: 没有此功能,即使以下简单的Python结构也无法转储:

data = dict(a=1)
data['b'] = data

if you dump the above: 如果您将以上内容丢弃:

import sys
import ruamel.yaml

ruamel.yaml.round_trip_dump(data, sys.stdout)

you'll get: 你会得到:

&id001
a: 1
b: *id001

Depending on the language you use and its YAML parser, using an anchor on a scalar, might only be useful for not having to repeat the value later on, without the advantage you get with collections (mappings, sequences) that they actually refer to the same object. 根据您使用的语言及其YAML解析器,在标量上使用锚点可能仅对以后不必重复该值有用,而没有它们实际引用的集合(映射,序列)的好处。同一对象。 Again in Python, the loading of scalars by the more popular parsers is done in separate entities: 再次在Python中,较流行的解析器对标量的加载是在单独的实体中完成的:

import ruamel.yaml

yaml_str = """\
a: &CONST_MY_KEY ""
b: *CONST_MY_KEY
"""

data = ruamel.yaml.round_trip_load(yaml_str)
print('a {a!r}'.format(**data))
print('b {b!r}'.format(**data))
data['a'] = 'hello'
print('a {a!r}'.format(**data))
print('b {b!r}'.format(**data))

gives you: 给你:

a ''
b ''
a 'hello'
b ''

Note that the value of data['b'] doesn't change because (in most parsers) scalars are not constructed as referenced objects. 注意data['b']的值不会改变,因为(在大多数解析器中)标量没有构造为引用对象。

If you do that with the original example: 如果使用原始示例执行此操作:

import ruamel.yaml

yaml_str = """\
&id001
b: *id001
a: 1
"""

data = ruamel.yaml.round_trip_load(yaml_str)
print('a {a!r}'.format(**data))
data['b']['a'] = 2
print('a {a!r}'.format(**data))

Because data['a'] and data['b']['a'] actually are the same object, and changing one changes the other. 因为data['a']data['b']['a']实际上是同一对象,所以更改一个将更改另一个对象。

The usage of <<: *name is a non-standard YAML extension. <<: *name的用法是非标准的YAML扩展名。 It expects &name to be an anchor on a mapping, for which the key-value pairs are added to the mapping the alias is used upon. 它期望&name是映射的锚,将键/值对添加到使用别名的映射上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM