All Projects → jinzhu → Copier

jinzhu / Copier

Licence: mit
Copier for golang, copy value from struct to struct and more

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to Copier

goverter
Generate type-safe Go converters by simply defining an interface
Stars: ✭ 100 (-96.05%)
Mutual labels:  copy
Node Fs Extra
Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
Stars: ✭ 8,142 (+221.95%)
Mutual labels:  copy
Fs extra
Expanding opportunities standard library std::fs and std::io
Stars: ✭ 95 (-96.24%)
Mutual labels:  copy
Cpx
A cli tool to watch and copy file globs.
Stars: ✭ 394 (-84.42%)
Mutual labels:  copy
Clipboard Polyfill
📋 Simple copying on the web, with maximum browser compatibility.
Stars: ✭ 748 (-70.42%)
Mutual labels:  copy
Copycat Action
©️ GitHub Action for copying files to other repositories
Stars: ✭ 48 (-98.1%)
Mutual labels:  copy
fs-utils
Generalized file and path utils for Node.js projects.
Stars: ✭ 33 (-98.7%)
Mutual labels:  copy
Discord Guild Copy
A script to copy a discord guild/server
Stars: ✭ 127 (-94.98%)
Mutual labels:  copy
Fcfilemanager
iOS File Manager on top of NSFileManager for simplifying files management. 📂
Stars: ✭ 862 (-65.92%)
Mutual labels:  copy
Usbcopyer
😉 用于在插上U盘后自动按需复制该U盘的文件。”备份&偷U盘文件的神器”(写作USBCopyer,读作USBCopier)
Stars: ✭ 1,296 (-48.75%)
Mutual labels:  copy
Gotem
Copy to clipboard for modern browsers in less than 1kb.
Stars: ✭ 439 (-82.64%)
Mutual labels:  copy
Web Launch App
🔥 launch app from web page(调起app、调用端能力、下载app、环境判断、复制)
Stars: ✭ 598 (-76.35%)
Mutual labels:  copy
Locopy
locopy: Loading/Unloading to Redshift and Snowflake using Python.
Stars: ✭ 73 (-97.11%)
Mutual labels:  copy
Copy
Go copy directory recursively
Stars: ✭ 264 (-89.56%)
Mutual labels:  copy
Redis Tools
my tools working with redis
Stars: ✭ 104 (-95.89%)
Mutual labels:  copy
gdrive-clone
Clone a shared google drive link to your own google drive
Stars: ✭ 48 (-98.1%)
Mutual labels:  copy
Copy To Clipboard
✂️ Modern copy to clipboard. No Flash.
Stars: ✭ 39 (-98.46%)
Mutual labels:  copy
Rollup Plugin Copy
Copy files and folders using Rollup
Stars: ✭ 128 (-94.94%)
Mutual labels:  copy
Vue Clipboard2
A simple vue2 binding to clipboard.js
Stars: ✭ 1,617 (-36.06%)
Mutual labels:  copy
Copy Paths To Clipboard
Copy paths in a variety of formats to the clipboard with Alfred
Stars: ✭ 83 (-96.72%)
Mutual labels:  copy

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
	EmployeCode int64 `copier:"EmployeNum"` // 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
	EmployeId int64 `copier:"EmployeNum"` // 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, EmployeId: 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, EmployeId: 0, SuperRole: "Super Admin"},
	//   {Name: "jinzhu 2", Age: 30, Salary:0, DoubleAge: 60, EmployeId: 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].