celestiaorg / knuu

Integration Test Framework
Apache License 2.0
38 stars 31 forks source link

Knuu Options Struct #410

Closed MSevey closed 1 week ago

MSevey commented 1 month ago

Current

With the new refactor the following knuu option structure was introduced.

type Option func(*Knuu)

func WithImageBuilder(builder builder.Builder) Option {
    return func(k *Knuu) {
        k.ImageBuilder = builder
    }
}

The goal was to provide flexibility for developers to enable what they wanted.

Two downsides to this approach are:

  1. Every new option requires a new function
  2. Every new option requires a function call when call knuu.New()

Additionally this has the New method applying options and then setting defaults.
This approach is flexible, but could lead to developer errors because the method is making assumptions based on inputs verse handling those inputs more strictly.

Proposed Change

If we use an Options struct we can have the same flexibility for developers with the following benefits:

  1. No new function definitions
  2. No additional function calls on knuu.New()
  3. Ability to define DefaultOptions

To start the Options struct would be the following.

type Options struct {
    system.SystemDependencies
    timeout      time.Duration
    proxyEnabled bool
}

This currently is basically duplicating the Knuu struct, but the benefit here is that it allows us to update the knuu and options structs independently.

This would also allow us to update the New method in a way that still provides flexibility while also being more strict on validating inputs.

func New(ctx context.Context) (*Knuu, error) {
  return NewWithOptions(ctx, DefaultOptions())
}

func NewWithOptions(ctx context.Context, opts Options) (*Knuu, error) {
  // First validate options
  ...  
  // Then apply
  k := &Knuu{opts}
  // Do other things
  return k, nil
}

func DefaultOptions() Options {
  // Initialize defaults
  return
}

This allows the use of New() without needing to worrying about any options, which is create to testing and getting started. Also refactoring out the DefaultOptions makes it easy for developers to get the defaults and just update the options they need to.