In this article, php editor Xigua will introduce how to merge two mapping hierarchies from YAML files and preserve all keys. The process is very simple and only requires the use of appropriate functions and methods. By merging two mapping hierarchies, we can combine their keys and values to create a larger, more comprehensive mapping hierarchy. Whether you are working with configuration files or data processing, merging mapping hierarchies is a very useful technique. Next, we will walk you through the process step by step so that you can easily master this technique.
Question content
I am working on a solution involving nested keys in a yaml file. The software will read the files passed in the parameters and load them in the order in which keys are updated/added.
I have 2 yaml files and I want to merge them without losing any keys. I want to stack all profiles to generate a single map without removing any keys.
So I have yaml 1
env: test1 template: app: database: name: oracle
yaml2
env: test2 template: app: database: version : 12
The result I want is (the order is yaml1 - yaml2)
env: test2 template: app: database: name: oracle version: 12
I tried copying using a map but since the keys have the same name I ended up with
env: test2 template: app: database: version: 12
I am using
gopkg.in/yaml.v3 for reading yaml provides me with the map[string] interface{}
and map using copy
package main import ( "fmt" "log" "maps" "os" "path/filepath" "gopkg.in/yaml.v3" ) type configuration struct { c m fl []string } type m = map[string]interface{} func (c *configuration) Update(nc m) { if c.c == nil { c.c = nc } else { maps.Copy(c.c, nc) } } func (c configuration) Print() { d, err := yaml.Marshal(&c.c) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("---:\n%s\n\n", string(d)) } func (c configuration) ParseDir(path string) { } func (c *configuration) LoadFromFile(filename string) { // YAML string stored in a variable yf, yfErr := os.ReadFile(filename) if yfErr != nil { log.Fatal("Error reading the file ", yfErr) } // Map to store the parsed YAML data var data m // Unmarshal the YAML string into the data map err := yaml.Unmarshal(yf, &data) if err != nil { log.Fatal(err) } c.Update(data) } func listFiles(path string) []string { var returnLf []string err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.Mode().IsRegular() { returnLf = append(returnLf, path) } return nil }) if err != nil { log.Println(err) } return returnLf }
Workaround
Suppose you want to merge YAML maps with "template" as the key from two YAML documents, a fairly simple implementation would look like this:
package main import ( "fmt" "gopkg.in/yaml.v3" ) const data1 = `--- env: test1 template: app: database: name: oracle foo: whatever ` const data2 = `--- env: test2 template: app: some_stuff: [1, 2, 3, 4] database: version : 12 foo: 42 ` type T struct { Env string `yaml:"env"` Tmpl map[string]any `yaml:"template"` } func mergeMapsRecursively(dst, src map[string]any) map[string]any { res := make(map[string]any) for dstKey, dstVal := range dst { srcVal, exists := src[dstKey] if !exists { res[dstKey] = dstVal continue } dstValMap, dstValIsMap := dstVal.(map[string]any) srcValMap, srcValIsMap := srcVal.(map[string]any) if dstValIsMap && srcValIsMap { res[dstKey] = mergeMapsRecursively(dstValMap, srcValMap) } else { res[dstKey] = srcVal } } for srcKey, srcVal := range src { if _, exists := dst[srcKey]; !exists { res[srcKey] = srcVal } } return res } func main() { var a, b T if err := yaml.Unmarshal([]byte(data1), &a); err != nil { panic(err) } if err := yaml.Unmarshal([]byte(data2), &b); err != nil { panic(err) } fmt.Printf("%#v\n%#v\n%#v\n", a.Tmpl, b.Tmpl, mergeMapsRecursively(a.Tmpl, b.Tmpl)) }
mergeMapsRecursively
Function recursively merges fields that exist in two maps (if they are both maps), or replaces the value in dst
with src
value in , otherwise — just like maps.Copy
does.
If this isn't exactly what you asked for, I hope my example will get you on the right track anyway.
The above is the detailed content of Merge two mapping hierarchies from YAML files, preserving all keys. For more information, please follow other related articles on the PHP Chinese website!

This article explains Go's package import mechanisms: named imports (e.g., import "fmt") and blank imports (e.g., import _ "fmt"). Named imports make package contents accessible, while blank imports only execute t

This article details efficient conversion of MySQL query results into Go struct slices. It emphasizes using database/sql's Scan method for optimal performance, avoiding manual parsing. Best practices for struct field mapping using db tags and robus

This article explains Beego's NewFlash() function for inter-page data transfer in web applications. It focuses on using NewFlash() to display temporary messages (success, error, warning) between controllers, leveraging the session mechanism. Limita

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

This article details efficient file writing in Go, comparing os.WriteFile (suitable for small files) with os.OpenFile and buffered writes (optimal for large files). It emphasizes robust error handling, using defer, and checking for specific errors.

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
