Skip to content

Commit d380b4d

Browse files
committed
Ignore unexported fields
1 parent c72a821 commit d380b4d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

copier.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,12 @@ func deepFields(reflectType reflect.Type) []reflect.StructField {
348348

349349
for i := 0; i < reflectType.NumField(); i++ {
350350
v := reflectType.Field(i)
351-
if v.Anonymous {
352-
fields = append(fields, deepFields(v.Type)...)
353-
} else {
354-
fields = append(fields, v)
351+
if v.IsExported() {
352+
if v.Anonymous {
353+
fields = append(fields, deepFields(v.Type)...)
354+
} else {
355+
fields = append(fields, v)
356+
}
355357
}
356358
}
357359

copier_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (user User) DoubleAge() int32 {
2626
}
2727

2828
type Employee struct {
29+
_User *User
2930
Name string
3031
Birthday *time.Time
3132
Nickname *string
@@ -134,6 +135,10 @@ func TestCopyStruct(t *testing.T) {
134135
employee4 := &Employee{}
135136
copier.Copy(&employee4, user)
136137
checkEmployee(*employee4, user, t, "Copy From Ptr To Double Ptr")
138+
139+
employee5 := &Employee{}
140+
copier.Copy(&employee5, &employee)
141+
checkEmployee(*employee5, user, t, "Copy From Employee To Employee")
137142
}
138143

139144
func TestCopyFromStructToSlice(t *testing.T) {

0 commit comments

Comments
 (0)