faker icon indicating copy to clipboard operation
faker copied to clipboard

Make possible to ignore some fields or provide custom generators

Open unkeep opened this issue 2 years ago • 1 comments

For the cases when you cant use struct tags (eg struct is from 3d-party) it would be nice to have an option to ignore some fields by it's name or provide a custom generator.

type SomeType struct {
  Field1 error
  Field2 context.Context
  Field3 string
}

a.

var v SomeType
faker.Fake(&v, faker.IgnoreFields("Field1", "Field2"))

b.

var v SomeType
faker.Fake(&v,
   faker.FieldGenerator("Field1", func() (interface{}, error) {
      return context.TODO(), nil
   }),
   faker.FieldGenerator("Field2", func() (interface{}, error){
      return errors.New("test"), nil
   },
)

c.

faker.SetGeneratorForType("context.Context", func() (interface{}, error) {
  return context.TODO(), nil
})
faker.SetGeneratorForType("error", func() (interface{}, error) {
  return errors.New("test"), nil
})

b and c are more flexible as they allow custom generators

unkeep avatar Jun 20 '22 11:06 unkeep

Hi @unkeep Can we link this feature with the current custom tag generator here?

https://github.com/bxcodec/faker/blob/72e522332c49399d4c52a9ad8086244df95f38a6/faker.go#L405-L456

bxcodec avatar Aug 11 '22 15:08 bxcodec

fixed in #160

Let me know if it still not enough

bxcodec avatar Aug 19 '22 15:08 bxcodec