TwiN / deepmerge

Go library for deep merging YAML or JSON
MIT License
6 stars 1 forks source link
deep-merge deepmerge go golang json merge yaml

deepmerge

test Go Report Card Go version Go Reference

Go library for deep merging YAML or JSON files.

Usage

YAML

package main

import (
    "github.com/TwiN/deepmerge"
)

func main() {
    dst := `
debug: true
client:
  insecure: true
users:
  - id: 1
    firstName: John
    lastName: Doe
  - id: 2
    firstName: Jane
    lastName: Doe`
    src := `
client:
  timeout: 5s
users:
  - id: 3
    firstName: Bob
    lastName: Smith`
    output, err := deepmerge.YAML([]byte(dst), []byte(src))
    if err != nil {
        panic(err)
    }
    println(string(output))
}

Output:

client:
    insecure: true
    timeout: 5s
debug: true
users:
    - firstName: John
      id: 1
      lastName: Doe
    - firstName: Jane
      id: 2
      lastName: Doe
    - firstName: Bob
      id: 3
      lastName: Smith

JSON

package main

import (
    "github.com/TwiN/deepmerge"
)

func main() {
    dst := `{
  "debug": true,
  "client": {
    "insecure": true
  },
  "users": [
    {
      "id": 1,
      "firstName": "John",
      "lastName": "Doe"
    },
    {
      "id": 2,
      "firstName": "Jane",
      "lastName": "Doe"
    }
  ]
}`
    src := `{
  "client": {
    "timeout": "5s"
  },
  "users": [
    {
      "id": 3,
      "firstName": "Bob",
      "lastName": "Smith"
    }
  ]
}`
    output, err := deepmerge.JSON([]byte(dst), []byte(src))
    if err != nil {
        panic(err)
    }
    println(string(output))
}

Output:

{
  "client": {
    "insecure": true,
    "timeout": "5s"
  },
  "debug": true,
  "users": [
    {
      "firstName": "John",
      "id": 1,
      "lastName": "Doe"
    },
    {
      "firstName": "Jane",
      "id": 2,
      "lastName": "Doe"
    },
    {
      "firstName": "Bob",
      "id": 3,
      "lastName": "Smith"
    }
  ]
}