YAML.Merge (YAML v0.2.0)

Copy Markdown View Source

Resolves YAML merge keys (<<) in AST documents.

Example

Input YAML:

description: "This is line one\nThis is line two\n"
defaults:
  retries: 3
  timeout: 30
  created_at: "2025-01-01T00:00:00Z"
service_a:
  <<: *defaults
  timeout: 60

Input AST:

[
  %YAML.AST.Document{
    root: %YAML.AST.Mapping{
      pairs: [
        {%YAML.AST.Scalar{value: "description", ...},
         %YAML.AST.Scalar{value: "This is line one\nThis is line two\n", ...}},
        {%YAML.AST.Scalar{value: "defaults", ...},
         %YAML.AST.Mapping{
           pairs: [
             {%YAML.AST.Scalar{value: "retries", ...}, %YAML.AST.Scalar{value: 3, ...}},
             {%YAML.AST.Scalar{value: "timeout", ...}, %YAML.AST.Scalar{value: 30, ...}},
             {%YAML.AST.Scalar{value: "created_at", ...}, %YAML.AST.Scalar{value: "2025-01-01T00:00:00Z", ...}}
           ], ...
         }},
        {%YAML.AST.Scalar{value: "service_a", ...},
         %YAML.AST.Mapping{
           pairs: [
             {%YAML.AST.Scalar{value: "<<", ...}, %YAML.AST.Mapping{pairs: [...], ...}},
             {%YAML.AST.Scalar{value: "timeout", ...}, %YAML.AST.Scalar{value: 60, ...}}
           ], ...
         }}
      ], ...
    }
  }
]

Output AST (after merge):

[
  %YAML.AST.Document{
    root: %YAML.AST.Mapping{
      pairs: [
        {%YAML.AST.Scalar{value: "description", ...},
         %YAML.AST.Scalar{value: "This is line one\nThis is line two\n", ...}},
        {%YAML.AST.Scalar{value: "defaults", ...},
         %YAML.AST.Mapping{
           pairs: [
             {%YAML.AST.Scalar{value: "retries", ...}, %YAML.AST.Scalar{value: 3, ...}},
             {%YAML.AST.Scalar{value: "timeout", ...}, %YAML.AST.Scalar{value: 30, ...}},
             {%YAML.AST.Scalar{value: "created_at", ...}, %YAML.AST.Scalar{value: "2025-01-01T00:00:00Z", ...}}
           ], ...
         }},
        {%YAML.AST.Scalar{value: "service_a", ...},
         %YAML.AST.Mapping{
           pairs: [
             # << key removed, pairs merged:
             {%YAML.AST.Scalar{value: "retries", ...}, %YAML.AST.Scalar{value: 3, ...}},
             {%YAML.AST.Scalar{value: "created_at", ...}, %YAML.AST.Scalar{value: "2025-01-01T00:00:00Z", ...}},
             {%YAML.AST.Scalar{value: "timeout", ...}, %YAML.AST.Scalar{value: 60, ...}}  # overridden value
           ], ...
         }}
      ], ...
    }
  }
]

Notice:

  • The << key is removed from service_a
  • retries and created_at are merged from defaults
  • timeout is overridden to 60 (local value wins)

Summary

Functions

apply(doc)