mirror of
https://gitea.com/Sirherobrine23/tea.git
synced 2024-08-22 03:17:24 +00:00
update go min version Update Vendors: * code.gitea.io/gitea-vet v0.2.0 -> v0.2.1 * code.gitea.io/sdk/gitea v0.13.0 -> v0.13.1 * github.com/AlecAivazis/survey v2.1.1 -> v2.2.2 * github.com/adrg/xdg v0.2.1 -> v0.2.2 * github.com/araddon/dateparse d820a6159ab1 -> 8aadafed4dc4 * github.com/go-git/go-git v5.1.0 -> v5.2.0 * github.com/muesli/termenv v0.7.2 -> v0.7.4 * github.com/stretchr/testify v1.5.1 -> v1.6.1 * github.com/urfave/cli v2.2.0 -> v2.3.0 Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/250 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: mrsdizzie <info@mrsdizzie.com> Co-Authored-By: 6543 <6543@noreply.gitea.io> Co-Committed-By: 6543 <6543@noreply.gitea.io>
248 lines
10 KiB
Markdown
248 lines
10 KiB
Markdown
# Mergo
|
|
|
|
|
|
[![GoDoc][3]][4]
|
|
[![GitHub release][5]][6]
|
|
[![GoCard][7]][8]
|
|
[![Build Status][1]][2]
|
|
[![Coverage Status][9]][10]
|
|
[![Sourcegraph][11]][12]
|
|
[![FOSSA Status][13]][14]
|
|
|
|
[![GoCenter Kudos][15]][16]
|
|
|
|
[1]: https://travis-ci.org/imdario/mergo.png
|
|
[2]: https://travis-ci.org/imdario/mergo
|
|
[3]: https://godoc.org/github.com/imdario/mergo?status.svg
|
|
[4]: https://godoc.org/github.com/imdario/mergo
|
|
[5]: https://img.shields.io/github/release/imdario/mergo.svg
|
|
[6]: https://github.com/imdario/mergo/releases
|
|
[7]: https://goreportcard.com/badge/imdario/mergo
|
|
[8]: https://goreportcard.com/report/github.com/imdario/mergo
|
|
[9]: https://coveralls.io/repos/github/imdario/mergo/badge.svg?branch=master
|
|
[10]: https://coveralls.io/github/imdario/mergo?branch=master
|
|
[11]: https://sourcegraph.com/github.com/imdario/mergo/-/badge.svg
|
|
[12]: https://sourcegraph.com/github.com/imdario/mergo?badge
|
|
[13]: https://app.fossa.io/api/projects/git%2Bgithub.com%2Fimdario%2Fmergo.svg?type=shield
|
|
[14]: https://app.fossa.io/projects/git%2Bgithub.com%2Fimdario%2Fmergo?ref=badge_shield
|
|
[15]: https://search.gocenter.io/api/ui/badge/github.com%2Fimdario%2Fmergo
|
|
[16]: https://search.gocenter.io/github.com/imdario/mergo
|
|
|
|
A helper to merge structs and maps in Golang. Useful for configuration default values, avoiding messy if-statements.
|
|
|
|
Mergo merges same-type structs and maps by setting default values in zero-value fields. Mergo won't merge unexported (private) fields. It will do recursively any exported one. It also won't merge structs inside maps (because they are not addressable using Go reflection).
|
|
|
|
Also a lovely [comune](http://en.wikipedia.org/wiki/Mergo) (municipality) in the Province of Ancona in the Italian region of Marche.
|
|
|
|
## Status
|
|
|
|
It is ready for production use. [It is used in several projects by Docker, Google, The Linux Foundation, VMWare, Shopify, etc](https://github.com/imdario/mergo#mergo-in-the-wild).
|
|
|
|
### Important note
|
|
|
|
Please keep in mind that a problematic PR broke [0.3.9](//github.com/imdario/mergo/releases/tag/0.3.9). I reverted it in [0.3.10](//github.com/imdario/mergo/releases/tag/0.3.10), and I consider it stable but not bug-free. Also, this version adds suppot for go modules.
|
|
|
|
Keep in mind that in [0.3.2](//github.com/imdario/mergo/releases/tag/0.3.2), Mergo changed `Merge()`and `Map()` signatures to support [transformers](#transformers). I added an optional/variadic argument so that it won't break the existing code.
|
|
|
|
If you were using Mergo before April 6th, 2015, please check your project works as intended after updating your local copy with ```go get -u github.com/imdario/mergo```. I apologize for any issue caused by its previous behavior and any future bug that Mergo could cause in existing projects after the change (release 0.2.0).
|
|
|
|
### Donations
|
|
|
|
If Mergo is useful to you, consider buying me a coffee, a beer, or making a monthly donation to allow me to keep building great free software. :heart_eyes:
|
|
|
|
<a href='https://ko-fi.com/B0B58839' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=0' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
|
|
[![Beerpay](https://beerpay.io/imdario/mergo/badge.svg)](https://beerpay.io/imdario/mergo)
|
|
[![Beerpay](https://beerpay.io/imdario/mergo/make-wish.svg)](https://beerpay.io/imdario/mergo)
|
|
<a href="https://liberapay.com/dario/donate"><img alt="Donate using Liberapay" src="https://liberapay.com/assets/widgets/donate.svg"></a>
|
|
|
|
### Mergo in the wild
|
|
|
|
- [moby/moby](https://github.com/moby/moby)
|
|
- [kubernetes/kubernetes](https://github.com/kubernetes/kubernetes)
|
|
- [vmware/dispatch](https://github.com/vmware/dispatch)
|
|
- [Shopify/themekit](https://github.com/Shopify/themekit)
|
|
- [imdario/zas](https://github.com/imdario/zas)
|
|
- [matcornic/hermes](https://github.com/matcornic/hermes)
|
|
- [OpenBazaar/openbazaar-go](https://github.com/OpenBazaar/openbazaar-go)
|
|
- [kataras/iris](https://github.com/kataras/iris)
|
|
- [michaelsauter/crane](https://github.com/michaelsauter/crane)
|
|
- [go-task/task](https://github.com/go-task/task)
|
|
- [sensu/uchiwa](https://github.com/sensu/uchiwa)
|
|
- [ory/hydra](https://github.com/ory/hydra)
|
|
- [sisatech/vcli](https://github.com/sisatech/vcli)
|
|
- [dairycart/dairycart](https://github.com/dairycart/dairycart)
|
|
- [projectcalico/felix](https://github.com/projectcalico/felix)
|
|
- [resin-os/balena](https://github.com/resin-os/balena)
|
|
- [go-kivik/kivik](https://github.com/go-kivik/kivik)
|
|
- [Telefonica/govice](https://github.com/Telefonica/govice)
|
|
- [supergiant/supergiant](supergiant/supergiant)
|
|
- [SergeyTsalkov/brooce](https://github.com/SergeyTsalkov/brooce)
|
|
- [soniah/dnsmadeeasy](https://github.com/soniah/dnsmadeeasy)
|
|
- [ohsu-comp-bio/funnel](https://github.com/ohsu-comp-bio/funnel)
|
|
- [EagerIO/Stout](https://github.com/EagerIO/Stout)
|
|
- [lynndylanhurley/defsynth-api](https://github.com/lynndylanhurley/defsynth-api)
|
|
- [russross/canvasassignments](https://github.com/russross/canvasassignments)
|
|
- [rdegges/cryptly-api](https://github.com/rdegges/cryptly-api)
|
|
- [casualjim/exeggutor](https://github.com/casualjim/exeggutor)
|
|
- [divshot/gitling](https://github.com/divshot/gitling)
|
|
- [RWJMurphy/gorl](https://github.com/RWJMurphy/gorl)
|
|
- [andrerocker/deploy42](https://github.com/andrerocker/deploy42)
|
|
- [elwinar/rambler](https://github.com/elwinar/rambler)
|
|
- [tmaiaroto/gopartman](https://github.com/tmaiaroto/gopartman)
|
|
- [jfbus/impressionist](https://github.com/jfbus/impressionist)
|
|
- [Jmeyering/zealot](https://github.com/Jmeyering/zealot)
|
|
- [godep-migrator/rigger-host](https://github.com/godep-migrator/rigger-host)
|
|
- [Dronevery/MultiwaySwitch-Go](https://github.com/Dronevery/MultiwaySwitch-Go)
|
|
- [thoas/picfit](https://github.com/thoas/picfit)
|
|
- [mantasmatelis/whooplist-server](https://github.com/mantasmatelis/whooplist-server)
|
|
- [jnuthong/item_search](https://github.com/jnuthong/item_search)
|
|
- [bukalapak/snowboard](https://github.com/bukalapak/snowboard)
|
|
- [janoszen/containerssh](https://github.com/janoszen/containerssh)
|
|
|
|
## Install
|
|
|
|
go get github.com/imdario/mergo
|
|
|
|
// use in your .go code
|
|
import (
|
|
"github.com/imdario/mergo"
|
|
)
|
|
|
|
## Usage
|
|
|
|
You can only merge same-type structs with exported fields initialized as zero value of their type and same-types maps. Mergo won't merge unexported (private) fields but will do recursively any exported one. It won't merge empty structs value as [they are zero values](https://golang.org/ref/spec#The_zero_value) too. Also, maps will be merged recursively except for structs inside maps (because they are not addressable using Go reflection).
|
|
|
|
```go
|
|
if err := mergo.Merge(&dst, src); err != nil {
|
|
// ...
|
|
}
|
|
```
|
|
|
|
Also, you can merge overwriting values using the transformer `WithOverride`.
|
|
|
|
```go
|
|
if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil {
|
|
// ...
|
|
}
|
|
```
|
|
|
|
Additionally, you can map a `map[string]interface{}` to a struct (and otherwise, from struct to map), following the same restrictions as in `Merge()`. Keys are capitalized to find each corresponding exported field.
|
|
|
|
```go
|
|
if err := mergo.Map(&dst, srcMap); err != nil {
|
|
// ...
|
|
}
|
|
```
|
|
|
|
Warning: if you map a struct to map, it won't do it recursively. Don't expect Mergo to map struct members of your struct as `map[string]interface{}`. They will be just assigned as values.
|
|
|
|
Here is a nice example:
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/imdario/mergo"
|
|
)
|
|
|
|
type Foo struct {
|
|
A string
|
|
B int64
|
|
}
|
|
|
|
func main() {
|
|
src := Foo{
|
|
A: "one",
|
|
B: 2,
|
|
}
|
|
dest := Foo{
|
|
A: "two",
|
|
}
|
|
mergo.Merge(&dest, src)
|
|
fmt.Println(dest)
|
|
// Will print
|
|
// {two 2}
|
|
}
|
|
```
|
|
|
|
Note: if test are failing due missing package, please execute:
|
|
|
|
go get gopkg.in/yaml.v2
|
|
|
|
### Transformers
|
|
|
|
Transformers allow to merge specific types differently than in the default behavior. In other words, now you can customize how some types are merged. For example, `time.Time` is a struct; it doesn't have zero value but IsZero can return true because it has fields with zero value. How can we merge a non-zero `time.Time`?
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/imdario/mergo"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
type timeTransformer struct {
|
|
}
|
|
|
|
func (t timeTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
|
|
if typ == reflect.TypeOf(time.Time{}) {
|
|
return func(dst, src reflect.Value) error {
|
|
if dst.CanSet() {
|
|
isZero := dst.MethodByName("IsZero")
|
|
result := isZero.Call([]reflect.Value{})
|
|
if result[0].Bool() {
|
|
dst.Set(src)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Snapshot struct {
|
|
Time time.Time
|
|
// ...
|
|
}
|
|
|
|
func main() {
|
|
src := Snapshot{time.Now()}
|
|
dest := Snapshot{}
|
|
mergo.Merge(&dest, src, mergo.WithTransformers(timeTransformer{}))
|
|
fmt.Println(dest)
|
|
// Will print
|
|
// { 2018-01-12 01:15:00 +0000 UTC m=+0.000000001 }
|
|
}
|
|
```
|
|
|
|
|
|
## Contact me
|
|
|
|
If I can help you, you have an idea or you are using Mergo in your projects, don't hesitate to drop me a line (or a pull request): [@im_dario](https://twitter.com/im_dario)
|
|
|
|
## About
|
|
|
|
Written by [Dario Castañé](http://dario.im).
|
|
|
|
## Top Contributors
|
|
|
|
[![0](https://sourcerer.io/fame/imdario/imdario/mergo/images/0)](https://sourcerer.io/fame/imdario/imdario/mergo/links/0)
|
|
[![1](https://sourcerer.io/fame/imdario/imdario/mergo/images/1)](https://sourcerer.io/fame/imdario/imdario/mergo/links/1)
|
|
[![2](https://sourcerer.io/fame/imdario/imdario/mergo/images/2)](https://sourcerer.io/fame/imdario/imdario/mergo/links/2)
|
|
[![3](https://sourcerer.io/fame/imdario/imdario/mergo/images/3)](https://sourcerer.io/fame/imdario/imdario/mergo/links/3)
|
|
[![4](https://sourcerer.io/fame/imdario/imdario/mergo/images/4)](https://sourcerer.io/fame/imdario/imdario/mergo/links/4)
|
|
[![5](https://sourcerer.io/fame/imdario/imdario/mergo/images/5)](https://sourcerer.io/fame/imdario/imdario/mergo/links/5)
|
|
[![6](https://sourcerer.io/fame/imdario/imdario/mergo/images/6)](https://sourcerer.io/fame/imdario/imdario/mergo/links/6)
|
|
[![7](https://sourcerer.io/fame/imdario/imdario/mergo/images/7)](https://sourcerer.io/fame/imdario/imdario/mergo/links/7)
|
|
|
|
|
|
## License
|
|
|
|
[BSD 3-Clause](http://opensource.org/licenses/BSD-3-Clause) license, as [Go language](http://golang.org/LICENSE).
|
|
|
|
|
|
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fimdario%2Fmergo.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fimdario%2Fmergo?ref=badge_large)
|