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

FakeData on interface that is a slice #116

Closed
pioz opened this issue Jun 24, 2020 · 1 comment
Closed

FakeData on interface that is a slice #116

pioz opened this issue Jun 24, 2020 · 1 comment

Comments

@pioz
Copy link
Contributor

pioz commented Jun 24, 2020

Is possible with faker do something like this:

func BulkCreate(db *gorm.DB, records interface{}) {
	switch reflect.TypeOf(records).Kind() {
	case reflect.Slice:
		slice := reflect.ValueOf(records)
		for i := 0; i < slice.Len(); i++ {
			faker.FakeData(slice.Index(i).Interface())
			fmt.Println(slice.Index(i).Interface())
		}
		db.Create(slice.Interface())
	}
}

I've tried, but this function does not modify the slice.Index(i).Interface() with fake data.

Here a playground.

Any idea to achieve this behavior?

@pioz pioz changed the title FakeData on interface that is a FakeData on interface that is a slice Jun 24, 2020
@bxcodec
Copy link
Owner

bxcodec commented Jun 24, 2020

Hi, maybe you can try this.
https://play.golang.org/p/3MW4hrlE7vM

  • I've changed the faker version, you're using the old one.
  • I use reflect.Set to set the value back

I'm closing this issue now, feel free to re-open if you have questions

@bxcodec bxcodec closed this as completed Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants