• Stars
    star
    5,034
  • Rank 7,882 (Top 0.2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Copier for golang, copy value from struct to struct and more

Copier

I am a copier, I copy everything from one to another

test status

Features

  • Copy from field to field with same name
  • Copy from method to field with same name
  • Copy from field to method with same name
  • Copy from slice to slice
  • Copy from struct to slice
  • Copy from map to map
  • Enforce copying a field with a tag
  • Ignore a field with a tag
  • Deep Copy

Usage

package main

import (
	"fmt"
	"github.com/jinzhu/copier"
)

type User struct {
	Name        string
	Role        string
	Age         int32
	EmployeeCode int64 `copier:"EmployeeNum"` // specify field name

	// Explicitly ignored in the destination struct.
	Salary   int
}

func (user *User) DoubleAge() int32 {
	return 2 * user.Age
}

// Tags in the destination Struct provide instructions to copier.Copy to ignore
// or enforce copying and to panic or return an error if a field was not copied.
type Employee struct {
	// Tell copier.Copy to panic if this field is not copied.
	Name      string `copier:"must"`

	// Tell copier.Copy to return an error if this field is not copied.
	Age       int32  `copier:"must,nopanic"`

	// Tell copier.Copy to explicitly ignore copying this field.
	Salary    int    `copier:"-"`

	DoubleAge int32
	EmployeeId int64 `copier:"EmployeeNum"` // specify field name
	SuperRole string
}

func (employee *Employee) Role(role string) {
	employee.SuperRole = "Super " + role
}

func main() {
	var (
		user      = User{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 200000}
		users     = []User{{Name: "Jinzhu", Age: 18, Role: "Admin", Salary: 100000}, {Name: "jinzhu 2", Age: 30, Role: "Dev", Salary: 60000}}
		employee  = Employee{Salary: 150000}
		employees = []Employee{}
	)

	copier.Copy(&employee, &user)

	fmt.Printf("%#v \n", employee)
	// Employee{
	//    Name: "Jinzhu",           // Copy from field
	//    Age: 18,                  // Copy from field
	//    Salary:150000,            // Copying explicitly ignored
	//    DoubleAge: 36,            // Copy from method
	//    EmployeeId: 0,            // Ignored
	//    SuperRole: "Super Admin", // Copy to method
	// }

	// Copy struct to slice
	copier.Copy(&employees, &user)

	fmt.Printf("%#v \n", employees)
	// []Employee{
	//   {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeeId: 0, SuperRole: "Super Admin"}
	// }

	// Copy slice to slice
	employees = []Employee{}
	copier.Copy(&employees, &users)

	fmt.Printf("%#v \n", employees)
	// []Employee{
	//   {Name: "Jinzhu", Age: 18, Salary:0, DoubleAge: 36, EmployeeId: 0, SuperRole: "Super Admin"},
	//   {Name: "jinzhu 2", Age: 30, Salary:0, DoubleAge: 60, EmployeeId: 0, SuperRole: "Super Dev"},
	// }

 	// Copy map to map
	map1 := map[int]int{3: 6, 4: 8}
	map2 := map[int32]int8{}
	copier.Copy(&map2, map1)

	fmt.Printf("%#v \n", map2)
	// map[int32]int8{3:6, 4:8}
}

Copy with Option

copier.CopyWithOption(&to, &from, copier.Option{IgnoreEmpty: true, DeepCopy: true})

Contributing

You can help to make the project better, check out http://gorm.io/contribute.html for things you can do.

Author

jinzhu

License

Released under the MIT License.

More Repositories

1

now

Now is a time toolkit for golang
Go
4,349
star
2

configor

Golang Configuration tool that support YAML, JSON, TOML, Shell Environment
Go
1,697
star
3

vrome

Vrome: Bringing Vim elegance to Chrome
CoffeeScript
617
star
4

inflection

Pluralizes and singularizes English nouns
Go
475
star
5

zeal-at-point

Search the word at point with Zeal (Emacs)
Emacs Lisp
166
star
6

configure

My dot files for Emacs, Openbox, XMonad, VIM, Golang, Zsh/Bash, tmux, URXVT, ArchLinux, Git, Ruby/Rails, Xbindkey, Vrome...
Emacs Lisp
89
star
7

grb

A tool to simplify working with remote git branches
Go
42
star
8

ipparse

IP Parse,Parse IP
Ruby
20
star
9

capistrano-confirm

Confirm before deploy
Ruby
16
star
10

mail

A Go Email Utility
Go
16
star
11

cacheAjax

jquery plugin to cache ajax request and more.
JavaScript
13
star
12

paygent

Ruby wrapper for paygent
Ruby
9
star
13

backupit

BackupIt: A tool to setup your backup server (backup multi servers once).
Ruby
8
star
14

yac

Yet Another Cheat
Ruby
7
star
15

mycheat

My personal cheat sheet repository for yac
6
star
16

super_list

Ruby
5
star
17

cheat

Public cheat sheet repository for yac
5
star
18

bitpay

Bitpay is a wrapper for chinese payment gateways. (unfinished, but alipay is ready to use...)
Ruby
5
star
19

cookieless

Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application!
Ruby
4
star
20

javascript_localize

javascript localize solution
Ruby
3
star
21

acts_as_tagtree

Tag? Tree? Tags like Tree
Ruby
3
star
22

redgreen

Ruby
2
star
23

jinzhu.github.com

My Github Page
JavaScript
2
star
24

eo

Eo_oE : EasyOperate
Ruby
2
star
25

hz_on_fly

convert hankaku to zenkaku/zenkaku to hankaku on fly
Ruby
2
star
26

acts_as_param

acts as param
Ruby
2
star
27

shanghaionrails

http://jinzhu.github.com/shanghaionrails/
2
star
28

phase_diagram

η›Έε›Ύεˆ†ζž
Ruby
2
star
29

metric_fu

A fist full of code metrics
Ruby
1
star
30

testingmachine

Testing Machine is a automated testing tool, make keyword driven testing even easier.
Ruby
1
star
31

jquery-tab-slider

jQuery tab slider
JavaScript
1
star
32

.github

my .github
1
star
33

qLayout

JavaScript
1
star
34

omniauth-t163

omniauth for t163 (working)
Ruby
1
star
35

acts_as_node

Ruby
1
star
36

devise

Ruby
1
star
37

newplugin

newPlugin - New Plugin Generator, make it dead easy to create a new rails plugin.
Ruby
1
star
38

enconverter

Encoding Converter, rack middleware used to convert encoding.
Ruby
1
star
39

jslib

a collection of javascript utils
JavaScript
1
star