deepmerge

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2024 License: MIT Imports: 3 Imported by: 7

README

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"
    }
  ]
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyWithPrimitiveValueDefinedMoreThanOnce = errors.New("error due to parameter with value of primitive type: only maps and slices/arrays can be merged, which means you cannot have define the same key twice for parameters that are not maps or slices/arrays")
)

Functions

func DeepMerge added in v0.2.0

func DeepMerge(dst, src map[string]interface{}, config Config) error

func JSON added in v0.2.0

func JSON(dst, src []byte, optionalConfig ...Config) ([]byte, error)

JSON merges the contents of src into dst

func YAML

func YAML(dst, src []byte, optionalConfig ...Config) ([]byte, error)

YAML merges the contents of src into dst

Types

type Config

type Config struct {
	// PreventMultipleDefinitionsOfKeysWithPrimitiveValue causes the return of an error if dst and src define
	// the same key and if said key has a value with a primitive type
	// This does not apply to slices or maps.
	//
	// Defaults to true
	PreventMultipleDefinitionsOfKeysWithPrimitiveValue bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL