func/vendor/github.com/imdario/mergo
knative-automation 6b8cccbbf9
upgrade to latest dependencies (#1269)
bumping github.com/prometheus/common 2af6d03...49b3603:
  > 49b3603 Improve OAuth2 user agent handling (# 391)
  > c5e1b60 config: ignore deprecated warning in tests (# 389)
  > b86ea81 OAuth2: Respect disable keepalives option; Implement close idle connections (# 390)
  > cdc09f0 Merge pull request # 387 from roidelapluie/useragent
  > d75e027 Merge pull request # 388 from simonpasquier/fix-tls-tests-for-go-1.18
  > db0284d Fix comment
  > 26d4974 Add more mimetypes (# 385)
  > aeda642 Update to Go 1.18
  > 2d0de85 Use full roundtripper
  > 627089d Set minimum version for go to 1.16 (# 372)
  > 5ab1c85 config: fix testdata for Go 1.18
  > 316097c Use WithUserAgent
  > 3763a1d TLS config: Enable selection of min TLS version (# 375)
  > 99a1aca add User-Agent header to oauth2 requests
  > 0c7319a Remove comment about PROMETHEUS_COMMON_DISABLE_HTTP2 env var because it is no longer true
  > 840c039 Use path.Clean to clean sigv4 path.
  > ffd0efb Deduplicate slashes for sigv4 signature
  > 902cb39 Merge pull request # 365 from prometheus/superq/bump_sigv4
  > 2c24277 Merge pull request # 362 from prometheus/repo_sync
  > 910a9df Update sigv4 modules
  > f6b0912 Merge pull request # 353 from prometheus/superq/bump_go
  > e457c0a Update common Prometheus files
  > 0e1254b Merge pull request # 359 from prometheus/repo_sync
  > 3c43b4d Update build/test
  > 252ff6f Make HTTP2 user visible
  > 809633a Update common Prometheus files
  > 00591a3 circleci: Test with go 1.17 (# 347)
  > 0762b59 Add proxy_url support for oauth2
  > f57586d circleci: add test-assets and style jobs
  > 1871a70 assets: add file system layer for zipped embed assets
  > ce7006e Update common Prometheus files (# 344)
  > 88ce30c Update common Prometheus files (# 340)
  > 88f1636 Remove github.com/pkg/errors dependency (# 338)
bumping contrib.go.opencensus.io/exporter/prometheus f3a7283...e6b6b80:
  > e6b6b80 Merge pull request # 30 from bogdandrutu/updatedeps
  > aaaf212 Merge pull request # 28 from wincus/update-prometheus
  > 75046ad Upgrade all deps to lastest
  > bf25aba update prometheus golang client
bumping golang.org/x/net 0bcc04d...f2f64eb:
  > f2f64eb http2: Send WindowUpdates when remaining bytes are below a threshold
  > ca03788 dns/dnsmessage: remove unnecessary []byte conversions
  > 0081b4b http2/h2c: propagate HTTP/1 server configuration to HTTP/2
  > f8f703f http2: accept HEAD requests with a body
  > bea034e all: remove redundant type conversion
  > 1e95f45 http/httpproxy: remove comment on https proxy precedance
  > f3363e0 http2: handle server errors after sending GOAWAY
  > 83b083e internal/socket: add missing import to zos-s390x file
  > b0a4917 dns/dnsmessage: use exported MustNewName in example
  > 3211cb9 nettest: fix Unix socket test on macOS
  > 4c34ddd http2: delete multipart form tempfiles after ServeHTTP returns
  > 1d4ff48 http2: add DialTLSContext to Transport
  > 13a9a73 http2: fix conn flow control when stream closes on bad content-length
  > 07c6da5 dns/dnsmessage: add AD and CD bits support
  > f428fae all: add FreeBSD riscv64 support
  > a33c5aa route: import syscall rather than golang.org/x/sys/unix
  > 7431dee lif: import syscall rather than golang.org/x/sys/unix

Signed-off-by: Knative Automation <automation@knative.team>

Signed-off-by: Knative Automation <automation@knative.team>
2022-10-12 05:44:51 +00:00
..
.deepsource.toml Vendor dependencies (#531) 2021-09-16 04:42:39 -07:00
.gitignore Vendor dependencies (#531) 2021-09-16 04:42:39 -07:00
.travis.yml Vendor dependencies (#531) 2021-09-16 04:42:39 -07:00
CODE_OF_CONDUCT.md Vendor dependencies (#531) 2021-09-16 04:42:39 -07:00
LICENSE Vendor dependencies (#531) 2021-09-16 04:42:39 -07:00
README.md upgrade to latest dependencies (#1269) 2022-10-12 05:44:51 +00:00
doc.go Vendor dependencies (#531) 2021-09-16 04:42:39 -07:00
map.go Vendor dependencies (#531) 2021-09-16 04:42:39 -07:00
merge.go upgrade to latest dependencies (#1269) 2022-10-12 05:44:51 +00:00
mergo.go upgrade to latest dependencies (#1269) 2022-10-12 05:44:51 +00:00

README.md

Mergo

GoDoc GitHub release GoCard Build Status Coverage Status Sourcegraph FOSSA Status Become my sponsor

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 (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, Microsoft, etc.

Important note

Please keep in mind that a problematic PR broke 0.3.9. I reverted it in 0.3.10, and I consider it stable but not bug-free. Also, this version adds support for go modules.

Keep in mind that in 0.3.2, Mergo changed Merge()and Map() signatures to support 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. 😍

Buy Me a Coffee at ko-fi.com Donate using Liberapay Become my sponsor

Mergo in the wild

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 too. Also, maps will be merged recursively except for structs inside maps (because they are not addressable using Go reflection).

if err := mergo.Merge(&dst, src); err != nil {
    // ...
}

Also, you can merge overwriting values using the transformer WithOverride.

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.

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:

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.v3

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?

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

About

Written by Dario Castañé.

License

BSD 3-Clause license, as Go language.

FOSSA Status