From 5a5390f34a883a803907f85d4cba348a51a9a982 Mon Sep 17 00:00:00 2001 From: Ken Brownfield Date: Tue, 27 Oct 2020 17:07:59 -0700 Subject: [PATCH 1/4] feat: Add ability to ignore interface{} population with SetIgnoreInterface() --- faker.go | 10 ++++++++++ faker_test.go | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/faker.go b/faker.go index 0080cfe..1c7a47a 100644 --- a/faker.go +++ b/faker.go @@ -30,6 +30,8 @@ var ( randomSize = 100 // Sets the single fake data generator to generate unique values generateUniqueValues = false + // Sets whether interface{}s should be ignored. + ignoreInterface = false // Unique values are kept in memory so the generator retries if the value already exists uniqueValues = map[string][]interface{}{} // Lang is selected language for random string generator @@ -273,6 +275,11 @@ func SetGenerateUniqueValues(unique bool) { generateUniqueValues = unique } +// SetIgnoreInterface allows to set a flag to ignore found interface{}s. +func SetIgnoreInterface(ignore bool) { + ignoreInterface = ignore +} + // SetNilIfLenIsZero allows to set nil for the slice and maps, if size is 0. func SetNilIfLenIsZero(setNil bool) { shouldSetNil = setNil @@ -391,6 +398,9 @@ func AddProvider(tag string, provider TaggedFunction) error { func getValue(a interface{}) (reflect.Value, error) { t := reflect.TypeOf(a) if t == nil { + if ignoreInterface { + return reflect.New(reflect.TypeOf(reflect.Struct)), nil + } return reflect.Value{}, fmt.Errorf("interface{} not allowed") } k := t.Kind() diff --git a/faker_test.go b/faker_test.go index ecefa25..48d5546 100644 --- a/faker_test.go +++ b/faker_test.go @@ -480,6 +480,19 @@ func TestSetNilIfLenIsZero(t *testing.T) { testRandZero = false } +func TestSetIgnoreInterface(t *testing.T) { + SetIgnoreInterface(false) + var someInterface interface{} + if err := FakeData(&someInterface); err == nil { + t.Error("Fake data generation didn't fail on interface{}") + } + SetIgnoreInterface(true) + if err := FakeData(&someInterface); err != nil { + t.Error("Fake data generation fail on interface{} with SetIgnoreInterface(true)") + } + SetIgnoreInterface(false) +} + func TestBoundaryAndLen(t *testing.T) { iterate := 10 someStruct := SomeStructWithLen{} From e78f28e83b12586e08ddc3f7664c6d83cd10943a Mon Sep 17 00:00:00 2001 From: Ken Brownfield Date: Tue, 27 Oct 2020 17:17:13 -0700 Subject: [PATCH 2/4] Update mod --- go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 6901f72..4f7cced 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/bxcodec/faker/v3 +module github.com/irridia/faker -go 1.12 +go 1.15 From 2b2d2a52b6d7fec4fa38dd38789eee0b8dfbddb7 Mon Sep 17 00:00:00 2001 From: Ken Brownfield Date: Tue, 27 Oct 2020 17:17:36 -0700 Subject: [PATCH 3/4] Revert "Update mod" This reverts commit e78f28e83b12586e08ddc3f7664c6d83cd10943a. --- go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 4f7cced..6901f72 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/irridia/faker +module github.com/bxcodec/faker/v3 -go 1.15 +go 1.12 From 533c450976769ac6ed72e0beeb991afe411b3114 Mon Sep 17 00:00:00 2001 From: Ken Brownfield Date: Mon, 9 Nov 2020 19:49:21 -0800 Subject: [PATCH 4/4] feat: update random integer boundary handling to accept a 0-span range of numbers. Good for reliable testing of 0 values. --- faker.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/faker.go b/faker.go index 1c7a47a..27c96cc 100644 --- a/faker.go +++ b/faker.go @@ -1092,12 +1092,16 @@ func randomString(n int, lang *langRuneBoundary) (string, error) { // randomIntegerWithBoundary returns a random integer between input start and end boundary. [start, end) func randomIntegerWithBoundary(boundary numberBoundary) int { - return rand.Intn(boundary.end-boundary.start) + boundary.start + span := boundary.end - boundary.start + if span <= 0 { + return boundary.start + } + return rand.Intn(span) + boundary.start } // randomInteger returns a random integer between start and end boundary. [start, end) func randomInteger() int { - return rand.Intn(nBoundary.end-nBoundary.start) + nBoundary.start + return randomIntegerWithBoundary(nBoundary) } // randomSliceAndMapSize returns a random integer between [0,randomSliceAndMapSize). If the testRandZero is set, returns 0