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

Generalize run option overriding #429

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading