From 0ffa03f42d5711bd50ac50027c7d7549b969619a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 11 Jan 2023 01:05:17 -0500 Subject: [PATCH] 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] Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 +- vendor/github.com/TwiN/deepmerge/README.md | 80 ++++++++++++++++++- vendor/github.com/TwiN/deepmerge/config.go | 10 +++ vendor/github.com/TwiN/deepmerge/deepmerge.go | 48 +++++++++++ vendor/github.com/TwiN/deepmerge/json.go | 31 +++++++ vendor/github.com/TwiN/deepmerge/yaml.go | 56 +------------ vendor/modules.txt | 2 +- 8 files changed, 173 insertions(+), 60 deletions(-) create mode 100644 vendor/github.com/TwiN/deepmerge/config.go create mode 100644 vendor/github.com/TwiN/deepmerge/deepmerge.go create mode 100644 vendor/github.com/TwiN/deepmerge/json.go diff --git a/go.mod b/go.mod index 89998b7a..9045d7ee 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index afda1252..88abdd72 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/vendor/github.com/TwiN/deepmerge/README.md b/vendor/github.com/TwiN/deepmerge/README.md index 31e56744..68d7171c 100644 --- a/vendor/github.com/TwiN/deepmerge/README.md +++ b/vendor/github.com/TwiN/deepmerge/README.md @@ -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" + } + ] +} ``` \ No newline at end of file diff --git a/vendor/github.com/TwiN/deepmerge/config.go b/vendor/github.com/TwiN/deepmerge/config.go new file mode 100644 index 00000000..d84c5b61 --- /dev/null +++ b/vendor/github.com/TwiN/deepmerge/config.go @@ -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 +} diff --git a/vendor/github.com/TwiN/deepmerge/deepmerge.go b/vendor/github.com/TwiN/deepmerge/deepmerge.go new file mode 100644 index 00000000..11f82265 --- /dev/null +++ b/vendor/github.com/TwiN/deepmerge/deepmerge.go @@ -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 +} diff --git a/vendor/github.com/TwiN/deepmerge/json.go b/vendor/github.com/TwiN/deepmerge/json.go new file mode 100644 index 00000000..fb8abea7 --- /dev/null +++ b/vendor/github.com/TwiN/deepmerge/json.go @@ -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) +} diff --git a/vendor/github.com/TwiN/deepmerge/yaml.go b/vendor/github.com/TwiN/deepmerge/yaml.go index 2358c395..7575e1e0 100644 --- a/vendor/github.com/TwiN/deepmerge/yaml.go +++ b/vendor/github.com/TwiN/deepmerge/yaml.go @@ -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 -} diff --git a/vendor/modules.txt b/vendor/modules.txt index aaadfe6c..f8349def 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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