Skip to content

Support Make customization #64

Open
@matthchr

Description

@matthchr

I'd like to use rapid.Make to generate structs. It already does this great for simple structures, but in my case the structure I'd like to generate is quite complex:

  1. Deeply nested.
  2. Some fields are recursive.
  3. Some fields are built out of types that have private fields

These facts mean that by default Make doesn't work for me.

One possible solution is that I use Custom instead and build my struct that way. I'm very new to rapid but as far as I can tell that requires me explicitly build each section of this structure. This is possible but a lot of work.

Instead, it would be nice if I could customize Make by overriding the default generators, but only for certain types or certain fields. gopter arbitraries has a similar capability.

Naïvely, something like Make[V any](overrides ...*Generator[any]) *Generator[V] might work, where in my test I could first set up the overrides I know that I need, and then call Make and supply them, and it would prefer the overrides but fall back to the defaults. Obviously if there's a desire to avoid changing the signature of Make, a variant could be used instead.

The disadvantage of this approach is that it allows type overrides but doesn't achieve field-specific overrides. To achieve a field specific override you'd need to override the type the field resided on and use Custom to generate the whole struct. I suppose a MakeVariant (ignore the name) could take (overrides []*Generator[any], fieldOverrides []*FieldGenerator[any]) where

type FieldGenerator[V any] struct {
    typ reflect.Type // Type of the containing struct
    field string
    gen *Generator[V]
}

I'm not sure that the field-specific override really fits the interfaces that you have defined well, but at least the type-specific one feels like it would slot in quite nicely.

Thoughts? Is there some way to do this already that I just haven't noticed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions