Skip to content

Commit

Permalink
Generalize run option overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaloney committed Mar 5, 2024
1 parent 4c577e8 commit 50cce8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
23 changes: 2 additions & 21 deletions modes/defaultmode/defaultmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (d *DefaultMode) generateCode() (err error) {
continue
}

options := overrideRunOptions(&model.DefaultParams, &d.commandLineOptions.Options)
options := model.DefaultParams.Override(&d.commandLineOptions.Options)

fileName, err := f.WriteModel(d.settings.TempPath, options)
if err != nil {
Expand All @@ -143,7 +143,7 @@ func (d *DefaultMode) runCode(frameworks framework.List) {
for _, f := range frameworks {
model := f.Model()

options := overrideRunOptions(&model.DefaultParams, &d.commandLineOptions.Options)
options := model.DefaultParams.Override(&d.commandLineOptions.Options)

result, err := f.Run(options)
if err != nil {
Expand All @@ -156,22 +156,3 @@ func (d *DefaultMode) runCode(frameworks framework.List) {
fmt.Println()
}
}

// overrideRunOptions overrides options set in the model with any set on the command line.
func overrideRunOptions(modelOptions, cliOptions *runoptions.Options) *runoptions.Options {
options := *modelOptions

if cliOptions.LogLevel != nil {
options.LogLevel = cliOptions.LogLevel
}

if cliOptions.TraceActivations != nil {
options.TraceActivations = cliOptions.TraceActivations
}

if cliOptions.RandomSeed != nil {
options.RandomSeed = cliOptions.RandomSeed
}

return &options
}
19 changes: 19 additions & 0 deletions util/runoptions/runoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ func New() Options {
}
}

// Override applies any overrides set in another Options struct. It returns a new struct.
func (o Options) Override(cliOptions *Options) *Options {
options := o

if cliOptions.LogLevel != nil {
options.LogLevel = cliOptions.LogLevel
}

if cliOptions.TraceActivations != nil {
options.TraceActivations = cliOptions.TraceActivations
}

if cliOptions.RandomSeed != nil {
options.RandomSeed = cliOptions.RandomSeed
}

return &options
}

// IsValidFramework returns if the framework name is in our list of valid ones or not.
func IsValidFramework(frameworkName string) bool {
return slices.Contains(ValidFrameworks, frameworkName)
Expand Down

0 comments on commit 50cce8e

Please sign in to comment.