Skip to content

Commit

Permalink
Updated README and improved .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Jan 12, 2018
1 parent 161c606 commit f1ac598
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
33 changes: 33 additions & 0 deletions .gitignore
@@ -0,0 +1,33 @@
#### joe made this: http://goel.io/joe

#### go ####
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

#### vim ####
# Swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]

# Session
Session.vim

# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
58 changes: 51 additions & 7 deletions README.md
Expand Up @@ -71,10 +71,10 @@ if err := mergo.Merge(&dst, src); err != nil {
}
```

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

```go
if err := mergo.MergeWithOverwrite(&dst, src); err != nil {
if err := mergo.Merge(&dst, src, WithOverride); err != nil {
// ...
}
```
Expand All @@ -87,7 +87,7 @@ 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.
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.

More information and examples in [godoc documentation](http://godoc.org/github.com/imdario/mergo).

Expand All @@ -111,13 +111,10 @@ func main() {
A: "one",
B: 2,
}

dest := Foo{
A: "two",
}

mergo.Merge(&dest, src)

fmt.Println(dest)
// Will print
// {two 2}
Expand All @@ -126,7 +123,54 @@ func main() {

Note: if test are failing due missing package, please execute:

go get gopkg.in/yaml.v1
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"
"reflect"
"time"
)

type timeTransfomer struct {
}

func (t timeTransfomer) 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
}

type Snapshot struct {
Time time.Time
// ...
}

func main() {
src := Snapshot{time.Now()}
dest := Snapshot{}
mergo.Merge(&dest, src, WithTransformers(timeTransfomer{}))
fmt.Println(dest)
// Will print
// { 2018-01-12 01:15:00 +0000 UTC m=+0.000000001 }
}
```


## Contact me

Expand Down

0 comments on commit f1ac598

Please sign in to comment.