Skip to content

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.

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.

chrisseto added 2 commits May 26, 2025 10:09
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.
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