Skip to content

Commit

Permalink
feat(proxy): validate sampler info (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 6, 2023
1 parent b16ee29 commit db5139a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 28 deletions.
16 changes: 15 additions & 1 deletion src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ type ConfigInfo struct {
type Bootstrap struct {
Container *assistant.CobraContainer
optionsInfo ConfigureOptionsInfo
profiles proxy.ConfiguredProfiles
profiles proxy.ProfilesConfig
sampler proxy.SamplerConfig
}

type ConfigureOptionsInfo struct {
Expand Down Expand Up @@ -209,3 +210,16 @@ func handleLangSetting(config configuration.ViperConfig) {
os.Exit(1)
}
}

func (b *Bootstrap) viper() {
// Ideally, the ProfileParameterSet would perform a check against
// the config, but extendio is not aware of config, so it can't
// check. Instead, we can check here.
//
b.profiles = b.optionsInfo.Config.Viper.GetStringMapStringSlice("profiles")
err := b.optionsInfo.Config.Viper.UnmarshalKey("sampler", &b.sampler)

if err != nil {
panic(err)
}
}
10 changes: 0 additions & 10 deletions src/app/command/root-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ func (b *Bootstrap) buildRootCommand(container *assistant.CobraContainer) {
container.MustRegisterParamSet(WorkerPoolFamName, workerPoolFam)
container.MustRegisterParamSet(FoldersFamName, foldersFam)
container.MustRegisterParamSet(ProfileFamName, profileFam)

// This needs to be implemented in extendio, so we can't bind
// here with a validated binder, we just perform a check here
// after the bind as a temporary measure. Actually, this
// needs to be implemented inside cross field validation.

// Ideally, the ProfileParameterSet would perform a check against
// the config, but extendio is not aware of config, so it can't
// check. Instead, we can put this check into
b.profiles = b.optionsInfo.Config.Viper.GetStringMapStringSlice("profiles")
}

func (b *Bootstrap) getRootInputs() *proxy.RootCommandInputs {
Expand Down
20 changes: 15 additions & 5 deletions src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
return err
}

// validate the scheme
//
if err := b.sampler.Validate(
inputs.ParamSet.Native.Scheme,
&b.profiles,
); err != nil {
return err
}

appErr = proxy.EnterShrink(
inputs,
b.optionsInfo.Program,
Expand Down Expand Up @@ -201,8 +210,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
defaultScheme = ""
)

samplers := b.optionsInfo.Config.Viper.Get("samplers")
_ = samplers
sampler := b.optionsInfo.Config.Viper.Get("sampler")

// can we validate that it is present in the config?
paramSet.BindValidatedString(
Expand All @@ -212,9 +220,9 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
),
&paramSet.Native.Scheme,
func(s string, f *pflag.Flag) error {
return lo.TernaryF(samplers != nil,
return lo.TernaryF(sampler != nil,
func() error { return nil },
func() error { return fmt.Errorf("samplers not found (%v)", s) },
func() error { return fmt.Errorf("sampler not found (%v)", s) },
)
},
)
Expand Down Expand Up @@ -334,7 +342,9 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
"quality": "q",
}

// 📌A note about cobra args validation: cmd.ValidArgs lets you define
b.viper()

// 📌 A note about cobra args validation: cmd.ValidArgs lets you define
// a list of all allowable tokens for positional args. Just define
// ValidArgs, eg:
// shrinkCommand.ValidArgs = []string{"foo", "bar", "baz"}
Expand Down
10 changes: 5 additions & 5 deletions src/app/proxy/image-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type RootParameterSet struct { // should contain RootCommandInputs
Language string // TODO: move this to family store
}

type ConfiguredProfiles map[string][]string
type ProfilesConfig map[string][]string

func (ps ConfiguredProfiles) Validate(profile string) error {
if profile != "" {
if _, found := ps[profile]; !found {
return fmt.Errorf("no such profile: '%v'", profile)
func (ps ProfilesConfig) Validate(name string) error {
if name != "" {
if _, found := ps[name]; !found {
return fmt.Errorf("no such profile: '%v'", name)
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/app/proxy/sampler-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package proxy

import (
"fmt"
)

type (
SchemeConfig struct {
Profiles []string `mapstructure:"profiles"`
}

SamplerSchemesConfig map[string]SchemeConfig

SamplerConfig struct {
Files int `mapstructure:"files"`
Folders int `mapstructure:"folders"`
Schemes SamplerSchemesConfig `mapstructure:"schemes"`
}
)

func (cs SamplerConfig) Validate(name string, profiles *ProfilesConfig) error {
if name == "" {
return nil
}

var (
found bool
scheme SchemeConfig
)

if scheme, found = cs.Schemes[name]; !found {
return fmt.Errorf("scheme: '%v' not found in config", name)
}

for _, p := range scheme.Profiles {
if _, found := (*profiles)[p]; !found {
return fmt.Errorf("profile(referenced by scheme: '%v'): '%v' not found in config",
name, p,
)
}
}

return nil
}
16 changes: 13 additions & 3 deletions src/app/proxy/sampler-runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ var _ = Describe("SamplerRunner", Ordered, func() {
// "--sample-folders", "3"
"--dry-run",
"--mode", "tidy",
"--scheme", entry.scheme,
}

bootstrap := command.Bootstrap{}
Expand Down Expand Up @@ -124,13 +123,24 @@ var _ = Describe("SamplerRunner", Ordered, func() {

Entry(nil, &samplerTE{
runnerTE: runnerTE{
given: "scheme",
should: "bar",
given: "profile",
should: "sample using the defined profile",
relative: "nasa/interstellar/Dark Energy Explorers/sessions/scan-01",
args: []string{
"--strip", "--interlace", "plane", "--quality", "85", "--profile", "adaptive",
},
},
}),

Entry(nil, &samplerTE{
runnerTE: runnerTE{
given: "scheme",
should: "sample all profiles in the scheme",
relative: "nasa/interstellar/Dark Energy Explorers/sessions/scan-01",
args: []string{
"--strip", "--interlace", "plane", "--quality", "85", "--scheme", "blur-sf",
},
},
}),
)
})
10 changes: 6 additions & 4 deletions test/data/configuration/pixa-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ profiles:
"--adaptive-resize",
"60",
]
samplers:
sampler:
files: 3
folders: 1
schemes:
- foo:
blur-sf:
profiles: ["blur", "sf"]
- bar:
adaptive-sf:
profiles: ["adaptive", "sf"]
- baz:
adaptive-blur:
profiles: ["adaptive", "blur"]

0 comments on commit db5139a

Please sign in to comment.