From 74585ae5f54b45964d75f16d465fdce5d0e563aa Mon Sep 17 00:00:00 2001 From: Ken Brownfield <61438713+nashikb@users.noreply.github.com> Date: Sun, 24 Jan 2021 18:53:14 -0800 Subject: [PATCH] feat: Add ability to ignore interface{} population with SetIgnoreInterface() (#123) * feat: Add ability to ignore interface{} population with SetIgnoreInterface() * Update mod * Revert "Update mod" This reverts commit e78f28e83b12586e08ddc3f7664c6d83cd10943a. * feat: update random integer boundary handling to accept a 0-span range of numbers. Good for reliable testing of 0 values. --- faker.go | 18 ++++++++++++++++-- faker_test.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/faker.go b/faker.go index d10ccd5..d97bd3a 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 @@ -278,6 +280,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 @@ -407,6 +414,9 @@ func RemoveProvider(tag string) 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() @@ -1098,12 +1108,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 diff --git a/faker_test.go b/faker_test.go index e8dce13..7fff340 100644 --- a/faker_test.go +++ b/faker_test.go @@ -482,6 +482,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{}