-
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from zerosnake0/issue84
fix embedded struct copy
- Loading branch information
Showing
2 changed files
with
97 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package copier_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jinzhu/copier" | ||
) | ||
|
||
type Embedded struct { | ||
Field1 string | ||
Field2 string | ||
} | ||
|
||
type Embedder struct { | ||
Embedded | ||
PtrField *string | ||
} | ||
|
||
type Timestamps struct { | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} | ||
|
||
type NotWork struct { | ||
ID string `json:"id"` | ||
UserID *string `json:"user_id"` | ||
Name string `json:"name"` | ||
Website *string `json:"website"` | ||
Timestamps | ||
} | ||
|
||
type Work struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
UserID *string `json:"user_id"` | ||
Website *string `json:"website"` | ||
Timestamps | ||
} | ||
|
||
func TestIssue84(t *testing.T) { | ||
t.Run("test1", func(t *testing.T) { | ||
var embedder Embedder | ||
embedded := Embedded{ | ||
Field1: "1", | ||
Field2: "2", | ||
} | ||
err := copier.Copy(&embedder, &embedded) | ||
if err != nil { | ||
t.Errorf("unable to copy: %s", err) | ||
} | ||
if embedder.Field1 != embedded.Field1 { | ||
t.Errorf("field1 value is %s instead of %s", embedder.Field1, embedded.Field1) | ||
} | ||
if embedder.Field2 != embedded.Field2 { | ||
t.Errorf("field2 value is %s instead of %s", embedder.Field2, embedded.Field2) | ||
} | ||
}) | ||
t.Run("from issue", func(t *testing.T) { | ||
notWorkObj := NotWork{ | ||
ID: "123", | ||
Name: "name", | ||
Website: nil, | ||
UserID: nil, | ||
Timestamps: Timestamps{ | ||
UpdatedAt: time.Now(), | ||
}, | ||
} | ||
workObj := Work{ | ||
ID: "123", | ||
Name: "name", | ||
Website: nil, | ||
UserID: nil, | ||
Timestamps: Timestamps{ | ||
UpdatedAt: time.Now(), | ||
}, | ||
} | ||
|
||
destObj1 := Work{} | ||
destObj2 := NotWork{} | ||
|
||
copier.CopyWithOption(&destObj1, &workObj, copier.Option{IgnoreEmpty: true, DeepCopy: false}) | ||
fmt.Println(destObj1) | ||
|
||
copier.CopyWithOption(&destObj2, ¬WorkObj, copier.Option{IgnoreEmpty: true, DeepCopy: false}) | ||
fmt.Println(destObj2) | ||
}) | ||
} |