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 5, 2023
1 parent b16ee29 commit 214842e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Bootstrap struct {
Container *assistant.CobraContainer
optionsInfo ConfigureOptionsInfo
profiles proxy.ConfiguredProfiles
sampler proxy.SamplerInfo
}

type ConfigureOptionsInfo struct {
Expand Down
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
31 changes: 27 additions & 4 deletions src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
return err
}

// validate the scheme
//
_ = inputs.ParamSet.Native.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 +209,8 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
defaultScheme = ""
)

samplers := b.optionsInfo.Config.Viper.Get("samplers")
_ = samplers
sampler := b.optionsInfo.Config.Viper.Get("sampler")
_ = 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,6 +342,8 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
"quality": "q",
}

b.load()

// 📌A note about cobra args validation: cmd.ValidArgs lets you define
// a list of all allowable tokens for positional args. Just define
// ValidArgs, eg:
Expand Down Expand Up @@ -374,3 +384,16 @@ func (b *Bootstrap) getShrinkInputs() *proxy.ShrinkCommandInputs {
).(*assistant.ParamSet[store.FilesFilterParameterSet]),
}
}

func (b *Bootstrap) load() {
// 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)
}
}
8 changes: 4 additions & 4 deletions src/app/proxy/image-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type RootParameterSet struct { // should contain RootCommandInputs

type ConfiguredProfiles 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 ConfiguredProfiles) Validate(name string) error {
if name != "" {
if _, found := ps[name]; !found {
return fmt.Errorf("no such profile: '%v'", name)
}
}

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

import (
"fmt"
)

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

ConfiguredSchemes map[string]ConfiguredSamplingScheme

SamplerSchemes map[string][]ConfiguredSamplingScheme
SamplerInfo struct {
Files int `mapstructure:"files"`
Folders int `mapstructure:"folders"`
Schemes SamplerSchemes `mapstructure:"schemes"`
}
)

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

var (
found bool
scheme []ConfiguredSamplingScheme
)

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

for _, p := range scheme[0].Profiles {
if _, found := (*profiles)[p]; !found {
return fmt.Errorf("profile(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 214842e

Please sign in to comment.