最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I merge nested mappings in yaml? - Stack Overflow

programmeradmin5浏览0评论

Given the following example:

.a: &a
    sheep: "a"
.b : &b
    cow: "a"

edileMamals: &edible_mamals
  << : *a
  << : *b

edibleAnimals: &edible_animals
    mamals:
        << : *edible_mamals
    fish:
        salmon: "a"
        tuna: "a"

I would like now to create allAnimals that relies on edibleAnimals and extends both mamals and fish. My naive approach of doing it like this:

allAnimals:
    << : *edible_animals
    mamals:
        horse: "a"
    fish:
        dolphin: "a"

got me:

allAnimals:
    mamals:
        horse: "a"
    fish:
        dolphin: "a"

instead of:

allAnimals:
    mamals:
        sheep: "a"
        cow: "a"
        horse: "a"
    fish:
        salmon: "a"
        tuna: "a"
        dolphin: "a"

I understand that the merge function sees that both edibleAnimals and allAnimals have the mamals key, so it takes the later. But is there a way to perform a multilayer merge?

So far I only found an answer here that suggest to add extra code to support the "+" suffix operator as a workaround , but I'm looking for a generic/native solution.

Given the following example:

.a: &a
    sheep: "a"
.b : &b
    cow: "a"

edileMamals: &edible_mamals
  << : *a
  << : *b

edibleAnimals: &edible_animals
    mamals:
        << : *edible_mamals
    fish:
        salmon: "a"
        tuna: "a"

I would like now to create allAnimals that relies on edibleAnimals and extends both mamals and fish. My naive approach of doing it like this:

allAnimals:
    << : *edible_animals
    mamals:
        horse: "a"
    fish:
        dolphin: "a"

got me:

allAnimals:
    mamals:
        horse: "a"
    fish:
        dolphin: "a"

instead of:

allAnimals:
    mamals:
        sheep: "a"
        cow: "a"
        horse: "a"
    fish:
        salmon: "a"
        tuna: "a"
        dolphin: "a"

I understand that the merge function sees that both edibleAnimals and allAnimals have the mamals key, so it takes the later. But is there a way to perform a multilayer merge?

So far I only found an answer here that suggest to add extra code to support the "+" suffix operator as a workaround , but I'm looking for a generic/native solution.

Share Improve this question asked Mar 20 at 12:49 Gleb NebolyubovGleb Nebolyubov 6765 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

After looking through the documentation of yaml I can say that at least by April 2025 "deep merge" or "nested merge" doesn't exist in yaml.

发布评论

评论列表(0)

  1. 暂无评论