mirror of
https://github.com/Threnklyn/jira.git
synced 2026-06-03 19:48:28 +02:00
udpate to use latest dep, update figtree
This commit is contained in:
-3
@@ -1,3 +0,0 @@
|
||||
guard 'gotest' do
|
||||
watch(%r{\.go$})
|
||||
end
|
||||
-98
@@ -1,98 +0,0 @@
|
||||
# Copier
|
||||
|
||||
I am a copier, I copy everything from one to another
|
||||
|
||||
## 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
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Name string
|
||||
Role string
|
||||
Age int32
|
||||
}
|
||||
|
||||
func (user *User) DoubleAge() int32 {
|
||||
return 2 * user.Age
|
||||
}
|
||||
|
||||
type Employee struct {
|
||||
Name string
|
||||
Age int32
|
||||
DoubleAge int32
|
||||
EmployeId int64
|
||||
SuperRule string
|
||||
}
|
||||
|
||||
func (employee *Employee) Role(role string) {
|
||||
employee.SuperRule = "Super " + role
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
user = User{Name: "Jinzhu", Age: 18, Role: "Admin"}
|
||||
users = []User{{Name: "Jinzhu", Age: 18, Role: "Admin"}, {Name: "jinzhu 2", Age: 30, Role: "Dev"}}
|
||||
employee = Employee{}
|
||||
employees = []Employee{}
|
||||
)
|
||||
|
||||
copier.Copy(&employee, &user)
|
||||
|
||||
fmt.Printf("%#v \n", employee)
|
||||
// Employee{
|
||||
// Name: "Jinzhu", // Copy from field
|
||||
// Age: 18, // Copy from field
|
||||
// DoubleAge: 36, // Copy from method
|
||||
// EmployeeId: 0, // Ignored
|
||||
// SuperRule: "Super Admin", // Copy to method
|
||||
// }
|
||||
|
||||
// Copy struct to slice
|
||||
copier.Copy(&employees, &user)
|
||||
|
||||
fmt.Printf("%#v \n", employees)
|
||||
// []Employee{
|
||||
// {Name: "Jinzhu", Age: 18, DoubleAge: 36, EmployeId: 0, SuperRule: "Super Admin"}
|
||||
// }
|
||||
|
||||
// Copy slice to slice
|
||||
employees = []Employee{}
|
||||
copier.Copy(&employees, &users)
|
||||
|
||||
fmt.Printf("%#v \n", employees)
|
||||
// []Employee{
|
||||
// {Name: "Jinzhu", Age: 18, DoubleAge: 36, EmployeId: 0, SuperRule: "Super Admin"},
|
||||
// {Name: "jinzhu 2", Age: 30, DoubleAge: 60, EmployeId: 0, SuperRule: "Super Dev"},
|
||||
// }
|
||||
}
|
||||
```
|
||||
|
||||
# Supporting the project
|
||||
|
||||
[](http://patreon.com/jinzhu)
|
||||
|
||||
# Author
|
||||
|
||||
**jinzhu**
|
||||
|
||||
* <http://github.com/jinzhu>
|
||||
* <wosmvp@gmail.com>
|
||||
* <http://twitter.com/zhangjinzhu>
|
||||
|
||||
## License
|
||||
|
||||
Released under the [MIT License](https://github.com/jinzhu/copier/blob/master/License).
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package copier_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
func BenchmarkCopyStruct(b *testing.B) {
|
||||
var fakeAge int32 = 12
|
||||
user := User{Name: "Jinzhu", Nickname: "jinzhu", Age: 18, FakeAge: &fakeAge, Role: "Admin", Notes: []string{"hello world", "welcome"}, flags: []byte{'x'}}
|
||||
for x := 0; x < b.N; x++ {
|
||||
copier.Copy(&Employee{}, &user)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNamaCopy(b *testing.B) {
|
||||
var fakeAge int32 = 12
|
||||
user := User{Name: "Jinzhu", Nickname: "jinzhu", Age: 18, FakeAge: &fakeAge, Role: "Admin", Notes: []string{"hello world", "welcome"}, flags: []byte{'x'}}
|
||||
for x := 0; x < b.N; x++ {
|
||||
employee := &Employee{
|
||||
Name: user.Name,
|
||||
Nickname: &user.Nickname,
|
||||
Age: int64(user.Age),
|
||||
FakeAge: int(*user.FakeAge),
|
||||
DoubleAge: user.DoubleAge(),
|
||||
Notes: user.Notes,
|
||||
}
|
||||
employee.Role(user.Role)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJsonMarshalCopy(b *testing.B) {
|
||||
var fakeAge int32 = 12
|
||||
user := User{Name: "Jinzhu", Nickname: "jinzhu", Age: 18, FakeAge: &fakeAge, Role: "Admin", Notes: []string{"hello world", "welcome"}, flags: []byte{'x'}}
|
||||
for x := 0; x < b.N; x++ {
|
||||
data, _ := json.Marshal(user)
|
||||
var employee Employee
|
||||
json.Unmarshal(data, &employee)
|
||||
|
||||
employee.DoubleAge = user.DoubleAge()
|
||||
employee.Role(user.Role)
|
||||
}
|
||||
}
|
||||
-143
@@ -1,143 +0,0 @@
|
||||
package copier_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
type TypeStruct1 struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
Field3 TypeStruct2
|
||||
Field4 *TypeStruct2
|
||||
Field5 []*TypeStruct2
|
||||
Field6 []TypeStruct2
|
||||
Field7 []*TypeStruct2
|
||||
Field8 []TypeStruct2
|
||||
}
|
||||
|
||||
type TypeStruct2 struct {
|
||||
Field1 int
|
||||
Field2 string
|
||||
}
|
||||
|
||||
type TypeStruct3 struct {
|
||||
Field1 interface{}
|
||||
Field2 string
|
||||
Field3 TypeStruct4
|
||||
Field4 *TypeStruct4
|
||||
Field5 []*TypeStruct4
|
||||
Field6 []*TypeStruct4
|
||||
Field7 []TypeStruct4
|
||||
Field8 []TypeStruct4
|
||||
}
|
||||
|
||||
type TypeStruct4 struct {
|
||||
field1 int
|
||||
Field2 string
|
||||
}
|
||||
|
||||
func (t *TypeStruct4) Field1(i int) {
|
||||
t.field1 = i
|
||||
}
|
||||
|
||||
func TestCopyDifferentFieldType(t *testing.T) {
|
||||
ts := &TypeStruct1{
|
||||
Field1: "str1",
|
||||
Field2: "str2",
|
||||
}
|
||||
ts2 := &TypeStruct2{}
|
||||
|
||||
copier.Copy(ts2, ts)
|
||||
|
||||
if ts2.Field2 != ts.Field2 || ts2.Field1 != 0 {
|
||||
t.Errorf("Should be able to copy from ts to ts2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyDifferentTypeMethod(t *testing.T) {
|
||||
ts := &TypeStruct1{
|
||||
Field1: "str1",
|
||||
Field2: "str2",
|
||||
}
|
||||
ts4 := &TypeStruct4{}
|
||||
|
||||
copier.Copy(ts4, ts)
|
||||
|
||||
if ts4.Field2 != ts.Field2 || ts4.field1 != 0 {
|
||||
t.Errorf("Should be able to copy from ts to ts4")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignableType(t *testing.T) {
|
||||
ts := &TypeStruct1{
|
||||
Field1: "str1",
|
||||
Field2: "str2",
|
||||
Field3: TypeStruct2{
|
||||
Field1: 666,
|
||||
Field2: "str2",
|
||||
},
|
||||
Field4: &TypeStruct2{
|
||||
Field1: 666,
|
||||
Field2: "str2",
|
||||
},
|
||||
Field5: []*TypeStruct2{
|
||||
{
|
||||
Field1: 666,
|
||||
Field2: "str2",
|
||||
},
|
||||
},
|
||||
Field6: []TypeStruct2{
|
||||
{
|
||||
Field1: 666,
|
||||
Field2: "str2",
|
||||
},
|
||||
},
|
||||
Field7: []*TypeStruct2{
|
||||
{
|
||||
Field1: 666,
|
||||
Field2: "str2",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ts3 := &TypeStruct3{}
|
||||
|
||||
copier.Copy(&ts3, &ts)
|
||||
|
||||
if v, ok := ts3.Field1.(string); !ok {
|
||||
t.Error("Assign to interface{} type did not succeed")
|
||||
} else if v != "str1" {
|
||||
t.Error("String haven't been copied correctly")
|
||||
}
|
||||
|
||||
if ts3.Field2 != ts.Field2 {
|
||||
t.Errorf("Field2 should be copied")
|
||||
}
|
||||
|
||||
checkType2WithType4(ts.Field3, ts3.Field3, t, "Field3")
|
||||
checkType2WithType4(*ts.Field4, *ts3.Field4, t, "Field4")
|
||||
|
||||
for idx, f := range ts.Field5 {
|
||||
checkType2WithType4(*f, *(ts3.Field5[idx]), t, "Field5")
|
||||
}
|
||||
|
||||
for idx, f := range ts.Field6 {
|
||||
checkType2WithType4(f, *(ts3.Field6[idx]), t, "Field6")
|
||||
}
|
||||
|
||||
for idx, f := range ts.Field7 {
|
||||
checkType2WithType4(*f, ts3.Field7[idx], t, "Field7")
|
||||
}
|
||||
|
||||
for idx, f := range ts.Field8 {
|
||||
checkType2WithType4(f, ts3.Field8[idx], t, "Field8")
|
||||
}
|
||||
}
|
||||
|
||||
func checkType2WithType4(t2 TypeStruct2, t4 TypeStruct4, t *testing.T, testCase string) {
|
||||
if t2.Field1 != t4.field1 || t2.Field2 != t4.Field2 {
|
||||
t.Errorf("%v: type struct 4 and type struct 2 is not equal", testCase)
|
||||
}
|
||||
}
|
||||
-212
@@ -1,212 +0,0 @@
|
||||
package copier_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Name string
|
||||
Nickname string
|
||||
Role string
|
||||
Age int32
|
||||
FakeAge *int32
|
||||
Notes []string
|
||||
flags []byte
|
||||
}
|
||||
|
||||
func (user User) DoubleAge() int32 {
|
||||
return 2 * user.Age
|
||||
}
|
||||
|
||||
type Employee struct {
|
||||
Name string
|
||||
Nickname *string
|
||||
Age int64
|
||||
FakeAge int
|
||||
EmployeID int64
|
||||
DoubleAge int32
|
||||
SuperRule string
|
||||
Notes []string
|
||||
flags []byte
|
||||
}
|
||||
|
||||
func (employee *Employee) Role(role string) {
|
||||
employee.SuperRule = "Super " + role
|
||||
}
|
||||
|
||||
func checkEmployee(employee Employee, user User, t *testing.T, testCase string) {
|
||||
if employee.Name != user.Name {
|
||||
t.Errorf("%v: Name haven't been copied correctly.", testCase)
|
||||
}
|
||||
if employee.Nickname == nil || *employee.Nickname != user.Nickname {
|
||||
t.Errorf("%v: NickName haven't been copied correctly.", testCase)
|
||||
}
|
||||
if employee.Age != int64(user.Age) {
|
||||
t.Errorf("%v: Age haven't been copied correctly.", testCase)
|
||||
}
|
||||
if user.FakeAge != nil && employee.FakeAge != int(*user.FakeAge) {
|
||||
t.Errorf("%v: FakeAge haven't been copied correctly.", testCase)
|
||||
}
|
||||
if employee.DoubleAge != user.DoubleAge() {
|
||||
t.Errorf("%v: Copy from method doesn't work", testCase)
|
||||
}
|
||||
if employee.SuperRule != "Super "+user.Role {
|
||||
t.Errorf("%v: Copy to method doesn't work", testCase)
|
||||
}
|
||||
if !reflect.DeepEqual(employee.Notes, user.Notes) {
|
||||
t.Errorf("%v: Copy from slice doen't work", testCase)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyStruct(t *testing.T) {
|
||||
var fakeAge int32 = 12
|
||||
user := User{Name: "Jinzhu", Nickname: "jinzhu", Age: 18, FakeAge: &fakeAge, Role: "Admin", Notes: []string{"hello world", "welcome"}, flags: []byte{'x'}}
|
||||
employee := Employee{}
|
||||
|
||||
if err := copier.Copy(employee, &user); err == nil {
|
||||
t.Errorf("Copy to unaddressable value should get error")
|
||||
}
|
||||
|
||||
copier.Copy(&employee, &user)
|
||||
checkEmployee(employee, user, t, "Copy From Ptr To Ptr")
|
||||
|
||||
employee2 := Employee{}
|
||||
copier.Copy(&employee2, user)
|
||||
checkEmployee(employee2, user, t, "Copy From Struct To Ptr")
|
||||
|
||||
employee3 := Employee{}
|
||||
ptrToUser := &user
|
||||
copier.Copy(&employee3, &ptrToUser)
|
||||
checkEmployee(employee3, user, t, "Copy From Double Ptr To Ptr")
|
||||
|
||||
employee4 := &Employee{}
|
||||
copier.Copy(&employee4, user)
|
||||
checkEmployee(*employee4, user, t, "Copy From Ptr To Double Ptr")
|
||||
}
|
||||
|
||||
func TestCopyFromStructToSlice(t *testing.T) {
|
||||
user := User{Name: "Jinzhu", Age: 18, Role: "Admin", Notes: []string{"hello world"}}
|
||||
employees := []Employee{}
|
||||
|
||||
if err := copier.Copy(employees, &user); err != nil && len(employees) != 0 {
|
||||
t.Errorf("Copy to unaddressable value should get error")
|
||||
}
|
||||
|
||||
if copier.Copy(&employees, &user); len(employees) != 1 {
|
||||
t.Errorf("Should only have one elem when copy struct to slice")
|
||||
} else {
|
||||
checkEmployee(employees[0], user, t, "Copy From Struct To Slice Ptr")
|
||||
}
|
||||
|
||||
employees2 := &[]Employee{}
|
||||
if copier.Copy(&employees2, user); len(*employees2) != 1 {
|
||||
t.Errorf("Should only have one elem when copy struct to slice")
|
||||
} else {
|
||||
checkEmployee((*employees2)[0], user, t, "Copy From Struct To Double Slice Ptr")
|
||||
}
|
||||
|
||||
employees3 := []*Employee{}
|
||||
if copier.Copy(&employees3, user); len(employees3) != 1 {
|
||||
t.Errorf("Should only have one elem when copy struct to slice")
|
||||
} else {
|
||||
checkEmployee(*(employees3[0]), user, t, "Copy From Struct To Ptr Slice Ptr")
|
||||
}
|
||||
|
||||
employees4 := &[]*Employee{}
|
||||
if copier.Copy(&employees4, user); len(*employees4) != 1 {
|
||||
t.Errorf("Should only have one elem when copy struct to slice")
|
||||
} else {
|
||||
checkEmployee(*((*employees4)[0]), user, t, "Copy From Struct To Double Ptr Slice Ptr")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFromSliceToSlice(t *testing.T) {
|
||||
users := []User{User{Name: "Jinzhu", Age: 18, Role: "Admin", Notes: []string{"hello world"}}, User{Name: "Jinzhu2", Age: 22, Role: "Dev", Notes: []string{"hello world", "hello"}}}
|
||||
employees := []Employee{}
|
||||
|
||||
if copier.Copy(&employees, users); len(employees) != 2 {
|
||||
t.Errorf("Should have two elems when copy slice to slice")
|
||||
} else {
|
||||
checkEmployee(employees[0], users[0], t, "Copy From Slice To Slice Ptr @ 1")
|
||||
checkEmployee(employees[1], users[1], t, "Copy From Slice To Slice Ptr @ 2")
|
||||
}
|
||||
|
||||
employees2 := &[]Employee{}
|
||||
if copier.Copy(&employees2, &users); len(*employees2) != 2 {
|
||||
t.Errorf("Should have two elems when copy slice to slice")
|
||||
} else {
|
||||
checkEmployee((*employees2)[0], users[0], t, "Copy From Slice Ptr To Double Slice Ptr @ 1")
|
||||
checkEmployee((*employees2)[1], users[1], t, "Copy From Slice Ptr To Double Slice Ptr @ 2")
|
||||
}
|
||||
|
||||
employees3 := []*Employee{}
|
||||
if copier.Copy(&employees3, users); len(employees3) != 2 {
|
||||
t.Errorf("Should have two elems when copy slice to slice")
|
||||
} else {
|
||||
checkEmployee(*(employees3[0]), users[0], t, "Copy From Slice To Ptr Slice Ptr @ 1")
|
||||
checkEmployee(*(employees3[1]), users[1], t, "Copy From Slice To Ptr Slice Ptr @ 2")
|
||||
}
|
||||
|
||||
employees4 := &[]*Employee{}
|
||||
if copier.Copy(&employees4, users); len(*employees4) != 2 {
|
||||
t.Errorf("Should have two elems when copy slice to slice")
|
||||
} else {
|
||||
checkEmployee(*((*employees4)[0]), users[0], t, "Copy From Slice Ptr To Double Ptr Slice Ptr @ 1")
|
||||
checkEmployee(*((*employees4)[1]), users[1], t, "Copy From Slice Ptr To Double Ptr Slice Ptr @ 2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmbedded(t *testing.T) {
|
||||
type Base struct {
|
||||
BaseField1 int
|
||||
BaseField2 int
|
||||
}
|
||||
|
||||
type Embed struct {
|
||||
EmbedField1 int
|
||||
EmbedField2 int
|
||||
Base
|
||||
}
|
||||
|
||||
base := Base{}
|
||||
embeded := Embed{}
|
||||
embeded.BaseField1 = 1
|
||||
embeded.BaseField2 = 2
|
||||
embeded.EmbedField1 = 3
|
||||
embeded.EmbedField2 = 4
|
||||
|
||||
copier.Copy(&base, &embeded)
|
||||
|
||||
if base.BaseField1 != 1 {
|
||||
t.Error("Embedded fields not copied")
|
||||
}
|
||||
}
|
||||
|
||||
type structSameName1 struct {
|
||||
A string
|
||||
B int64
|
||||
C time.Time
|
||||
}
|
||||
|
||||
type structSameName2 struct {
|
||||
A string
|
||||
B time.Time
|
||||
C int64
|
||||
}
|
||||
|
||||
func TestCopyFieldsWithSameNameButDifferentTypes(t *testing.T) {
|
||||
obj1 := structSameName1{A: "123", B: 2, C: time.Now()}
|
||||
obj2 := &structSameName2{}
|
||||
err := copier.Copy(obj2, &obj1)
|
||||
if err != nil {
|
||||
t.Error("Should not raise error")
|
||||
}
|
||||
|
||||
if obj2.A != obj1.A {
|
||||
t.Errorf("Field A should be copied")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user