Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/djboris9/merger
Go package that merges different datatypes together
https://github.com/djboris9/merger
data-structures golang golang-library golang-package map merge slice struct
Last synced: 25 days ago
JSON representation
Go package that merges different datatypes together
- Host: GitHub
- URL: https://github.com/djboris9/merger
- Owner: djboris9
- License: mit
- Created: 2017-01-01T15:45:16.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-17T16:11:36.000Z (about 7 years ago)
- Last Synced: 2024-06-20T09:19:01.503Z (7 months ago)
- Topics: data-structures, golang, golang-library, golang-package, map, merge, slice, struct
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Merger
This Go package merges different types together. Nested types will be merged too.
This can be useful for:* Merging configurations from different sources
* Deduplicate items before serialization
* Setting field preferencesIt works with Go 1.7 and greater
## Usage
Documentation with examples is available on https://godoc.org/github.com/djboris9/merger
This readme is only an introduction.### Merge algorithm
Let A be the first arbitrary value and B be the second arbitrary value with precedence.
boolean, numeric and string types will be overwritten by the argument with precedence (B).
slice and array types will be concatenated (A ∥ B).
struct and map types will be merged together giving a union of all fields, where the values of them are merged too (A ∪ B)### Example
Full usage example:
```Go
package mainimport (
"encoding/json"
"fmt"
"log""github.com/djboris9/merger"
)func main() {
A := struct {
FieldA string
FieldB string
FieldC []int
}{
"aAaA",
"bBbB",
[]int{1, 2},
}B := struct {
FieldA string
FieldC []int
}{
"NewVal",
[]int{3, 4},
}// Merge struct A and B together
V, err := merger.Merge(A, B)
if err != nil {
log.Fatal(err)
}// Print it
ser, _ := json.Marshal(V)
fmt.Println(string(ser))
// Output: {"FieldA":"NewVal","FieldB":"bBbB","FieldC":[1,2,3,4]}
}
```Merging maps:
```Go
A := map[string]int{
"x": 1,
"y": 2,
}
B := map[string]int{
"a": 1,
"b": 2,
}
V, _ := merger.Merge(A, B)
// V: map[string]int{"a": 1, "b": 2, "x": 1, "y": 2}
```Merging slices:
```Go
A := []int{1, 2, 3}
B := []int{4, 5, 6}
V, _ := merger.Merge(A, B)
// V: []int{1, 2, 3, 4, 5}
```Other examples are in the godoc.