“>-" 是什么作用?

huangapple go评论60阅读模式
英文:

What does ">-" do?

问题

>- in this case is a YAML indicator for a folded block scalar style. It is used to indicate that the string should be folded, meaning that leading white spaces (like newlines and indentation) will be removed, but the line breaks within the string will be preserved. This is typically used for multiline strings in YAML.

So, in your example, the string "Some description" is formatted as a folded block scalar, and when parsed, it will be a single string with preserved line breaks. The > is an HTML entity code that represents the > character, which is used to start the folded block scalar, and - indicates the chomping method, which is to strip trailing newlines.

If you were to read the content of the "description" field, it would be "Some description" with line breaks preserved as they were in the YAML code.

英文:

I have found some internal async-api schema that looks like the following example:

  schemas:
    Example:
      type: object
      allOf:
        - $ref: '#/components/schemas/Foo'
        - type: object
          properties:
            bar:
              type: string
              description: >-
                Some description

What does the >- mean in this case? I could not find any documentation about this. The official specification only mentions the | character, which indicates a multiline string.

答案1

得分: 4

|> 都用于多行字符串。

块样式指示符:块样式指示了块内部的换行应该如何处理。如果希望它们保留为换行符,请使用文字样式,由竖线(|)表示。如果相反地,您想要它们被替换为空格,请使用折叠样式,由右尖括号(>)表示。(要使用折叠样式获取换行符,请通过插入两个换行符留出一个空行。具有额外缩进的行也不会被折叠。)

块吞噬指示符:吞噬指示符控制了字符串末尾的换行应该如何处理。默认情况下,是剪切,在字符串末尾放置一个换行符。要移除所有换行符,请在样式指示符后加一个减号(-)。剪切和移除都会忽略块末尾实际有多少个换行符;要保留它们所有,请在样式指示符后加一个加号(+)。

具有交互性的源,因此您可以轻松看到这些语法之间的差异:https://yaml-multiline.info/

还可以查看如何在YAML中将字符串拆分成多行?以获得详细解释。

英文:

| and > are both used for multiline strings.

> Block Style Indicator: The block style indicates how newlines inside the block should behave. If you would like them to be kept as
> newlines, use the literal style, indicated by a pipe (|). If instead
> you want them to be replaced by spaces, use the folded style,
> indicated by a right angle bracket (>). (To get a newline using the
> folded style, leave a blank line by putting two newlines in. Lines
> with extra indentation are also not folded.)
>
> Block Chomping Indicator: The chomping indicator controls what should happen with newlines at the end of the string. The default,
> clip, puts a single newline at the end of the string. To remove all
> newlines, strip them by putting a minus sign (-) after the style
> indicator. Both clip and strip ignore how many newlines are actually
> at the end of the block; to keep them all put a plus sign (+) after
> the style indicator.

Source with interactivity, so you can easily see the differences between these syntaxes: https://yaml-multiline.info/

See also How do I break a string in YAML over multiple lines? for an in-depth explanation.

huangapple
  • 本文由 发表于 2023年3月9日 17:56:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定