-
-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom fakeable types #230
Conversation
I think I like the idea. It makes sense to me. Looks like all tests are passing so it should effect the current working usages. Were you able to run coverage? Just want to make sure we are fully testing things. Let me pass this along to a few other developers and get everyone's thoughts. |
Thanks for the quick response! I checked the code coverage. It changed a bit because there are a few error handling code paths that have been added but are not tested. I'm happy to add additional tests to cover those cases if you want. Before: After: |
I think as long as the coverage is good around what you did you should be fine |
First of all, I really like the idea! Only one thing that we could talk about. How could the interface work with the faker object? So, for example, if I want to generate a fake first name through the interface, right now the only way I can do it, is by using the global faker object and there is no way I can use a local faker object. type testStruct1 struct {
B string `fake:"{firstname}"`
}
type strTyp string
type testStruct2 struct {
B strTyp
}
func (t strTyp) Fake() interface{} {
return FirstName()
}
func ExampleFake() {
var t1 testStruct1
var t2 testStruct1
var t3 testStruct2
var t4 testStruct2
New(314).Struct(&t1)
New(314).Struct(&t2)
New(314).Struct(&t3)
New(314).Struct(&t4)
fmt.Printf("%#v\n", t1)
fmt.Printf("%#v\n", t2)
fmt.Printf("%#v\n", t3)
fmt.Printf("%#v\n", t4)
// Expected Output:
// gofakeit.testStruct1{B:"Margarette"}
// gofakeit.testStruct1{B:"Margarette"}
// gofakeit.testStruct2{B:"Margarette"}
// gofakeit.testStruct2{B:"Margarette"}
// Actual Output:
// gofakeit.testStruct1{B:"Margarette"}
// gofakeit.testStruct1{B:"Margarette"}
// gofakeit.testStruct2{B:"Two Hearted Ale"}
// gofakeit.testStruct2{B:"Maudite"}
} I don't know if it's possible, but it would be nice to pass a faker object to the type Fakeable interface {
// Fake returns a fake value for the type.
Fake(faker *Faker) interface{}
} |
Interesting idea! To make it work nicely I've refactored a bit |
|
||
type CustomInt int | ||
|
||
func (c CustomInt) Fake(faker *gofakeit.Faker) interface{} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide a couple of different ints, uints and floats to compare them a little more to a representation of what various ints, uints and floats would be?
} | ||
} | ||
|
||
func ExampleFakeable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to help give the best examples of this feature as possible can you throw in a couple of other custom examples to showcase what others can do with simple custom variables as well as structs?
Everything looks great! Nice job! I added just a few comments. Nothing crazy, just somethings to help elevate examples and usages. The only other thing I would ask is just to update the readme with a good example usage. Ill try to get this in soon and will do a change log and small marketing for this release. |
Thanks for the review! I'll get to it. BTW I have a follow up PR after this one is merged. Just mentioning it in case you want to hold the release until you decide if that one is interesting too as it builds on this and the other PR that you just merged. |
ya we can get this merged in once your ready and ill wait for you to submit the other pr and we can go from there. |
Let me know if their is anything else you would like to add before I merge this. |
If you are happy with these last changes, feel free to merge and I will submit the next PR. |
ok sounds good. |
There are occasions in which it is not possible to add field tags in a struct in order to use this library.
This PR defines an single method interface that can be implemented by a type.
When
gofakeit.Struct()
or gofakeit.Slice()` calls the method in the interface, it uses the returned value as the generated value for the type.There are now three ways to control the generation: field tags, Fakeable interface implementation, and gofakeit defaults. That is also the precedence in which they are applied in the case of multiple options being available. The reason why the field tags are always preferred over the rest of the options is because they are explicit instructions at the struct where the generation happens.
Would you be open to merging this?