-
Notifications
You must be signed in to change notification settings - Fork 25
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
Make[T]()
: add support for field and type overrides
#72
base: master
Are you sure you want to change the base?
Conversation
This commit introduces the `MakeCustom[T](MakeConfig)` function which allows overriding the generation of specific types or the generation of specific fields on specific types. This ability allows users to leverage the convenience of `Make` on highly nested types that may have, for example, a string type that should be treated as an enum. Make now ignores private fields instead of panicking due to reflect's inability to set private fields.
@@ -54,10 +72,19 @@ func (g *castGen) String() string { | |||
|
|||
func (g *castGen) value(t *T) any { | |||
v := g.gen.value(t) | |||
if v == nil { | |||
return nil | |||
} |
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.
Why is this check here?
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.
I was seeing nil pointer exceptions in cases where v
was nil. After double checking reflect.ValueOf
I suspect the issue stems from reflect.Value.Convert
as reflect.ValueOf(nil)
returns a zero reflect.Value
.
I'll add a comment and/or I could change this to:
v := reflect.ValueOf(g.gen.value(t))
if v.IsNil() {
return nil
}
return v.Convert(g.type).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.
In what cases .value()
is nil
? It should not be, ever.
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.
Ah! I figured it out. I was attempting to skip over a pointer field but missed the *
so I had overridden a struct generator with rapid.Just[any](nil)
. I'll see if I can make the error message a bit more friendly and add a regression test.
Can you please separate this PR in two parts, support for private fields and type/field customization? Also, please make sure the CI tests all pass. |
👋 I was utilizing rapid for a test involving Kubernetes objects (
corev1.Pod
) and wanted to leverageMake
instead of hand writing all the generation. I was transitioning the tests away from go-fuzz which provides the abilities to skip various fields and override "fuzzing" of others. This PR extends rapid to support a similar feature set.I'm already using this change in my fork and it works quite well. I'm not overjoyed with the API though but it does get the job done. If you have any suggestions, I'm all ears.