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

Commit 9d03be6

Browse files
authored
fix(skip-tag): skip tag does not really skip the value but reset it. (#67)
* fix(skip-tag): skip tag not really skip the value but reset it. * chore(travis): change the go version
1 parent 342a24f commit 9d03be6

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: go
22
go:
3-
- "1.11"
3+
- "1.12"
44

55
env:
66
- env GO111MODULE=on

faker.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,18 @@ func getValue(a interface{}) (reflect.Value, error) {
345345
v.Elem().Set(val.Convert(t.Elem()))
346346
return v, nil
347347
case reflect.Struct:
348-
349348
switch t.String() {
350349
case "time.Time":
351350
ft := time.Now().Add(time.Duration(rand.Int63()))
352351
return reflect.ValueOf(ft), nil
353352
default:
353+
originalDataVal := reflect.ValueOf(a)
354354
v := reflect.New(t).Elem()
355355
for i := 0; i < v.NumField(); i++ {
356356
if !v.Field(i).CanSet() {
357357
continue // to avoid panic to set on unexported field in struct
358358
}
359359
tags := decodeTags(t, i)
360-
361360
switch {
362361
case tags.keepOriginal:
363362
zero, err := isZero(reflect.ValueOf(a).Field(i))
@@ -380,7 +379,10 @@ func getValue(a interface{}) (reflect.Value, error) {
380379
val = val.Convert(v.Field(i).Type())
381380
v.Field(i).Set(val)
382381
case tags.fieldType == SKIP:
383-
continue
382+
item := originalDataVal.Field(i).Interface()
383+
if v.CanSet() && item != nil {
384+
v.Field(i).Set(reflect.ValueOf(item))
385+
}
384386
default:
385387
err := setDataWithTag(v.Field(i).Addr(), tags.fieldType)
386388
if err != nil {

faker_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,13 @@ func TestSkipField(t *testing.T) {
685685
a := struct {
686686
ID int
687687
ShouldBeSkipped int `faker:"-"`
688+
ShouldBeSkippedFilled int `faker:"-"`
689+
688690
}{}
689691

692+
693+
a.ShouldBeSkippedFilled = 10
694+
690695
err := FakeData(&a)
691696

692697
if err != nil {
@@ -696,6 +701,10 @@ func TestSkipField(t *testing.T) {
696701
if a.ShouldBeSkipped != 0 {
697702
t.Error("Expected that field will be skipped")
698703
}
704+
705+
if a.ShouldBeSkippedFilled != 10 {
706+
t.Error("Expected that field will be skipped")
707+
}
699708

700709
}
701710

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
module github.com/bxcodec/faker/v3
2+
3+
go 1.12

0 commit comments

Comments
 (0)