【问题标题】:Script (or program) to modify schemas in yaml file修改 yaml 文件中的模式的脚本(或程序)
【发布时间】:2021-07-18 10:13:37
【问题描述】:

我需要修改 yaml 文件中一种特定模式的所有实例。具有任何枚举和字符串的架构,我需要删除字符串并只留下枚举。 所以从这里:

components:
  schemas:
    //a lot of different schemas
    NotificationMethod:
      anyOf:
      - type: string
        enum:
          - PERIODIC
          - ONE_TIME
          - ON_EVENT_DETECTION
      - type: string
        description: >
          This string provides forward-compatibility with future
          extensions to the enumeration but is not used to encode
          content defined in the present version of this API.
      description: >
        Possible values are
        - PERIODIC
        - ONE_TIME
        - ON_EVENT_DETECTION

我需要收到这个:

components:
  schemas:
    //a lot of different schemas
    NotificationMethod:
      type: string
      enum:
        - PERIODIC
        - ONE_TIME
        - ON_EVENT_DETECTION
      description: >
        Possible values are
        - PERIODIC
        - ONE_TIME
        - ON_EVENT_DETECTION
  1. 此类更正仅应在具有 anyOf 且具有 2 个选项的架构中进行:字符串和枚举。
  2. 带有 anyOf 的行被删除。
  3. “-”(破折号和空格)之前留下的“类型”被删除。
  4. 在有叶类型规范的每一行中,两个空格被删除。
  5. 键入“string”,它的描述就会被删除。

怎么做?

【问题讨论】:

    标签: java yaml


    【解决方案1】:

    这可以通过yq来完成:

    yq e '(.. | select(has("anyOf")) | select(.anyOf.[] | has("enum"))) as $i |
        $i.description as $d |
        $i = ($i.anyOf.[] | select(has("enum"))) |
        $i.description = $d' test.yaml
    

    对于您的示例(减去非法的//a lot of different schemas ),您会得到以下输出:

    components:
      schemas:
        NotificationMethod:
          type: string
          enum:
            - PERIODIC
            - ONE_TIME
            - ON_EVENT_DETECTION
          description: >
            Possible values are - PERIODIC - ONE_TIME - ON_EVENT_DETECTION
    

    请注意,从 YAML 的角度来看,这是完全有效的,因为 > 启动了一个 folded 块标量,其中换行符将被折叠到一个空格中。如果您想在描述中保留换行符所在的位置,请改用文字块标量。

    上面的yq 命令显示了概念并适用于示例,但您需要根据自己的要求对其进行微调。

    【讨论】:

      猜你喜欢
      • 2014-07-03
      • 2021-12-18
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 2020-06-26
      • 2011-03-24
      相关资源
      最近更新 更多