chore(deps): bump github.com/TwiN/deepmerge from 0.1.0 to 0.2.0 (#401)

Bumps [github.com/TwiN/deepmerge](https://github.com/TwiN/deepmerge) from 0.1.0 to 0.2.0.
- [Release notes](https://github.com/TwiN/deepmerge/releases)
- [Commits](https://github.com/TwiN/deepmerge/compare/v0.1.0...v0.2.0)

---
updated-dependencies:
- dependency-name: github.com/TwiN/deepmerge
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
dependabot[bot] 2023-01-11 01:05:17 -05:00 committed by GitHub
parent e61a42220c
commit 0ffa03f42d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 173 additions and 60 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/TwiN/gatus/v5
go 1.19
require (
github.com/TwiN/deepmerge v0.1.0
github.com/TwiN/deepmerge v0.2.0
github.com/TwiN/g8 v1.4.0
github.com/TwiN/gocache/v2 v2.2.0
github.com/TwiN/health v1.6.0

4
go.sum
View File

@ -57,8 +57,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/TwiN/deepmerge v0.1.0 h1:xVFKkF0WCcIoJANSVf102NZIorVIAldT/G+Jv/uYn8A=
github.com/TwiN/deepmerge v0.1.0/go.mod h1:cR9OWsvI13y+FxrbnPLuF6BX2tbYeOjkiI6JWjEGqAE=
github.com/TwiN/deepmerge v0.2.0 h1:8P1tp2qDXllX6V1Ailg2XA074easAcvLMmW9v1jn0aE=
github.com/TwiN/deepmerge v0.2.0/go.mod h1:cR9OWsvI13y+FxrbnPLuF6BX2tbYeOjkiI6JWjEGqAE=
github.com/TwiN/g8 v1.4.0 h1:RUk5xTtxKCdMo0GGSbBVyjtAAfi2nqVbA9E0C4u5Cxo=
github.com/TwiN/g8 v1.4.0/go.mod h1:ECyGJsoIb99klUfvVQoS1StgRLte9yvvPigGrHdy284=
github.com/TwiN/gocache/v2 v2.2.0 h1:M3B36KyH24BntxLrLaUb2kgTdq8DzCnfod0IekLG57w=

View File

@ -1,10 +1,12 @@
# deepmerge
![test](https://github.com/TwiN/deepmerge/workflows/test/badge.svg?branch=master)
Go library for deep merging YAML files.
Go library for deep merging YAML or JSON files.
## Usage
### YAML
```go
package main
@ -31,7 +33,6 @@ users:
- id: 3
firstName: Bob
lastName: Smith`
output, err := deepmerge.YAML([]byte(dst), []byte(src))
if err != nil {
panic(err)
@ -56,4 +57,79 @@ users:
- firstName: Bob
id: 3
lastName: Smith
```
### JSON
```go
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:
```json
{
"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"
}
]
}
```

10
vendor/github.com/TwiN/deepmerge/config.go generated vendored Normal file
View File

@ -0,0 +1,10 @@
package deepmerge
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
}

48
vendor/github.com/TwiN/deepmerge/deepmerge.go generated vendored Normal file
View File

@ -0,0 +1,48 @@
package deepmerge
import (
"errors"
)
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")
)
func DeepMerge(dst, src map[string]interface{}, config Config) error {
for srcKey, srcValue := range src {
if srcValueAsMap, ok := srcValue.(map[string]interface{}); ok { // handle maps
if dstValue, ok := dst[srcKey]; ok {
if dstValueAsMap, ok := dstValue.(map[string]interface{}); ok {
err := DeepMerge(dstValueAsMap, srcValueAsMap, config)
if err != nil {
return err
}
continue
}
} else {
dst[srcKey] = make(map[string]interface{})
}
err := DeepMerge(dst[srcKey].(map[string]interface{}), srcValueAsMap, config)
if err != nil {
return err
}
} else if srcValueAsSlice, ok := srcValue.([]interface{}); ok { // handle slices
if dstValue, ok := dst[srcKey]; ok {
if dstValueAsSlice, ok := dstValue.([]interface{}); ok {
// If both src and dst are slices, we'll copy the elements from that src slice over to the dst slice
dst[srcKey] = append(dstValueAsSlice, srcValueAsSlice...)
continue
}
}
dst[srcKey] = srcValueAsSlice
} else { // handle primitives
if config.PreventMultipleDefinitionsOfKeysWithPrimitiveValue {
if _, ok := dst[srcKey]; ok {
return ErrKeyWithPrimitiveValueDefinedMoreThanOnce
}
}
dst[srcKey] = srcValue
}
}
return nil
}

31
vendor/github.com/TwiN/deepmerge/json.go generated vendored Normal file
View File

@ -0,0 +1,31 @@
package deepmerge
import (
"encoding/json"
)
// JSON merges the contents of src into dst
func JSON(dst, src []byte, optionalConfig ...Config) ([]byte, error) {
var cfg Config
if len(optionalConfig) > 0 {
cfg = optionalConfig[0]
} else {
cfg = Config{PreventMultipleDefinitionsOfKeysWithPrimitiveValue: true}
}
var dstMap, srcMap map[string]interface{}
err := json.Unmarshal(dst, &dstMap)
if err != nil {
return nil, err
}
err = json.Unmarshal(src, &srcMap)
if err != nil {
return nil, err
}
if dstMap == nil {
dstMap = make(map[string]interface{})
}
if err = DeepMerge(dstMap, srcMap, cfg); err != nil {
return nil, err
}
return json.Marshal(dstMap)
}

View File

@ -1,29 +1,16 @@
package deepmerge
import (
"errors"
"gopkg.in/yaml.v3"
)
var (
ErrDeepMergeDuplicatePrimitiveKey = errors.New("error deep merging YAML files due to duplicate primitive key: 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")
)
type Config struct {
// PreventDuplicateKeysWithPrimitiveValue causes YAML to return an error if dst and src define the same key if
// said key has a value with a primitive type
// This does not apply to slices or maps. Defaults to true
PreventDuplicateKeysWithPrimitiveValue bool
}
// YAML merges the contents of src into dst
func YAML(dst, src []byte, optionalConfig ...Config) ([]byte, error) {
var cfg Config
if len(optionalConfig) > 0 {
cfg = optionalConfig[0]
} else {
cfg = Config{PreventDuplicateKeysWithPrimitiveValue: true}
cfg = Config{PreventMultipleDefinitionsOfKeysWithPrimitiveValue: true}
}
var dstMap, srcMap map[string]interface{}
err := yaml.Unmarshal(dst, &dstMap)
@ -37,47 +24,8 @@ func YAML(dst, src []byte, optionalConfig ...Config) ([]byte, error) {
if dstMap == nil {
dstMap = make(map[string]interface{})
}
if err = deepMerge(dstMap, srcMap, cfg); err != nil {
if err = DeepMerge(dstMap, srcMap, cfg); err != nil {
return nil, err
}
return yaml.Marshal(dstMap)
}
func deepMerge(dst, src map[string]interface{}, config Config) error {
for srcKey, srcValue := range src {
if srcValueAsMap, ok := srcValue.(map[string]interface{}); ok { // handle maps
if dstValue, ok := dst[srcKey]; ok {
if dstValueAsMap, ok := dstValue.(map[string]interface{}); ok {
err := deepMerge(dstValueAsMap, srcValueAsMap, config)
if err != nil {
return err
}
continue
}
} else {
dst[srcKey] = make(map[string]interface{})
}
err := deepMerge(dst[srcKey].(map[string]interface{}), srcValueAsMap, config)
if err != nil {
return err
}
} else if srcValueAsSlice, ok := srcValue.([]interface{}); ok { // handle slices
if dstValue, ok := dst[srcKey]; ok {
if dstValueAsSlice, ok := dstValue.([]interface{}); ok {
// If both src and dst are slices, we'll copy the elements from that src slice over to the dst slice
dst[srcKey] = append(dstValueAsSlice, srcValueAsSlice...)
continue
}
}
dst[srcKey] = srcValueAsSlice
} else { // handle primitives
if config.PreventDuplicateKeysWithPrimitiveValue {
if _, ok := dst[srcKey]; ok {
return ErrDeepMergeDuplicatePrimitiveKey
}
}
dst[srcKey] = srcValue
}
}
return nil
}

2
vendor/modules.txt vendored
View File

@ -1,4 +1,4 @@
# github.com/TwiN/deepmerge v0.1.0
# github.com/TwiN/deepmerge v0.2.0
## explicit; go 1.19
github.com/TwiN/deepmerge
# github.com/TwiN/g8 v1.4.0