我需要修改yaml文件中一种特定模式的所有实例。有anyOf枚举和字符串的架构,我需要擦除字符串并只保留枚举。因此,从这一点来看:
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怎么做呢?
发布于 2021-07-19 11:09:36
这可以用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的角度来看,这是完全有效的,因为>开始一个折叠的块标量,其中换行将折叠到一个空间中。如果要在描述中保留换行符,请使用文字块标量。
上面的yq命令展示了这个概念,并适用于这个示例,但是您需要根据您的需求对其进行微调。
https://stackoverflow.com/questions/68428084
复制相似问题