Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
fix(skip-tag): skip tag does not really skip the value but reset it. (#…
Browse files Browse the repository at this point in the history
…67)

* fix(skip-tag): skip tag not really skip the value but reset it.

* chore(travis): change the go version
  • Loading branch information
bxcodec authored May 7, 2019
1 parent 342a24f commit 9d03be6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- "1.11"
- "1.12"

env:
- env GO111MODULE=on
Expand Down
8 changes: 5 additions & 3 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,18 @@ func getValue(a interface{}) (reflect.Value, error) {
v.Elem().Set(val.Convert(t.Elem()))
return v, nil
case reflect.Struct:

switch t.String() {
case "time.Time":
ft := time.Now().Add(time.Duration(rand.Int63()))
return reflect.ValueOf(ft), nil
default:
originalDataVal := reflect.ValueOf(a)
v := reflect.New(t).Elem()
for i := 0; i < v.NumField(); i++ {
if !v.Field(i).CanSet() {
continue // to avoid panic to set on unexported field in struct
}
tags := decodeTags(t, i)

switch {
case tags.keepOriginal:
zero, err := isZero(reflect.ValueOf(a).Field(i))
Expand All @@ -380,7 +379,10 @@ func getValue(a interface{}) (reflect.Value, error) {
val = val.Convert(v.Field(i).Type())
v.Field(i).Set(val)
case tags.fieldType == SKIP:
continue
item := originalDataVal.Field(i).Interface()
if v.CanSet() && item != nil {
v.Field(i).Set(reflect.ValueOf(item))
}
default:
err := setDataWithTag(v.Field(i).Addr(), tags.fieldType)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,13 @@ func TestSkipField(t *testing.T) {
a := struct {
ID int
ShouldBeSkipped int `faker:"-"`
ShouldBeSkippedFilled int `faker:"-"`

}{}


a.ShouldBeSkippedFilled = 10

err := FakeData(&a)

if err != nil {
Expand All @@ -696,6 +701,10 @@ func TestSkipField(t *testing.T) {
if a.ShouldBeSkipped != 0 {
t.Error("Expected that field will be skipped")
}

if a.ShouldBeSkippedFilled != 10 {
t.Error("Expected that field will be skipped")
}

}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/bxcodec/faker/v3

go 1.12

0 comments on commit 9d03be6

Please sign in to comment.