Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chrisseto
Copy link

👋 I was utilizing rapid for a test involving Kubernetes objects (corev1.Pod) and wanted to leverage Make 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.

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
}
Copy link
Owner

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?

Copy link
Author

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()

Copy link
Owner

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.

Copy link
Author

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.

@flyingmutant flyingmutant linked an issue Aug 18, 2024 that may be closed by this pull request
@flyingmutant
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Make customization
2 participants