...

Source file src/dario.cat/mergo/doc.go

Documentation: dario.cat/mergo

     1  // Copyright 2013 Dario Castañé. All rights reserved.
     2  // Copyright 2009 The Go Authors. All rights reserved.
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  /*
     7  A helper to merge structs and maps in Golang. Useful for configuration default values, avoiding messy if-statements.
     8  
     9  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).
    10  
    11  # Status
    12  
    13  It is ready for production use. It is used in several projects by Docker, Google, The Linux Foundation, VMWare, Shopify, etc.
    14  
    15  # Important notes
    16  
    17  1.0.0
    18  
    19  In 1.0.0 Mergo moves to a vanity URL `dario.cat/mergo`.
    20  
    21  0.3.9
    22  
    23  Please keep in mind that a problematic PR broke 0.3.9. We reverted it in 0.3.10. We consider 0.3.10 as stable but not bug-free. . Also, this version adds suppot for go modules.
    24  
    25  Keep in mind that in 0.3.2, Mergo changed Merge() and Map() signatures to support transformers. We added an optional/variadic argument so that it won't break the existing code.
    26  
    27  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 dario.cat/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).
    28  
    29  # Install
    30  
    31  Do your usual installation procedure:
    32  
    33  	go get dario.cat/mergo
    34  
    35  	// use in your .go code
    36  	import (
    37  	    "dario.cat/mergo"
    38  	)
    39  
    40  # Usage
    41  
    42  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).
    43  
    44  	if err := mergo.Merge(&dst, src); err != nil {
    45  		// ...
    46  	}
    47  
    48  Also, you can merge overwriting values using the transformer WithOverride.
    49  
    50  	if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil {
    51  		// ...
    52  	}
    53  
    54  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.
    55  
    56  	if err := mergo.Map(&dst, srcMap); err != nil {
    57  		// ...
    58  	}
    59  
    60  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.
    61  
    62  Here is a nice example:
    63  
    64  	package main
    65  
    66  	import (
    67  		"fmt"
    68  		"dario.cat/mergo"
    69  	)
    70  
    71  	type Foo struct {
    72  		A string
    73  		B int64
    74  	}
    75  
    76  	func main() {
    77  		src := Foo{
    78  			A: "one",
    79  			B: 2,
    80  		}
    81  		dest := Foo{
    82  			A: "two",
    83  		}
    84  		mergo.Merge(&dest, src)
    85  		fmt.Println(dest)
    86  		// Will print
    87  		// {two 2}
    88  	}
    89  
    90  # Transformers
    91  
    92  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?
    93  
    94  	package main
    95  
    96  	import (
    97  		"fmt"
    98  		"dario.cat/mergo"
    99  		"reflect"
   100  		"time"
   101  	)
   102  
   103  	type timeTransformer struct {
   104  	}
   105  
   106  	func (t timeTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
   107  		if typ == reflect.TypeOf(time.Time{}) {
   108  			return func(dst, src reflect.Value) error {
   109  				if dst.CanSet() {
   110  					isZero := dst.MethodByName("IsZero")
   111  					result := isZero.Call([]reflect.Value{})
   112  					if result[0].Bool() {
   113  						dst.Set(src)
   114  					}
   115  				}
   116  				return nil
   117  			}
   118  		}
   119  		return nil
   120  	}
   121  
   122  	type Snapshot struct {
   123  		Time time.Time
   124  		// ...
   125  	}
   126  
   127  	func main() {
   128  		src := Snapshot{time.Now()}
   129  		dest := Snapshot{}
   130  		mergo.Merge(&dest, src, mergo.WithTransformers(timeTransformer{}))
   131  		fmt.Println(dest)
   132  		// Will print
   133  		// { 2018-01-12 01:15:00 +0000 UTC m=+0.000000001 }
   134  	}
   135  
   136  # Contact me
   137  
   138  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): https://twitter.com/im_dario
   139  
   140  # About
   141  
   142  Written by Dario Castañé: https://da.rio.hn
   143  
   144  # License
   145  
   146  BSD 3-Clause license, as Go language.
   147  */
   148  package mergo
   149  

View as plain text