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

feat: Add ability to ignore interface{} population with SetIgnoreInterface() #123

Merged
merged 4 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1082,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
Expand Down
13 changes: 13 additions & 0 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down