Skip to content

Commit

Permalink
Squash all commits (#5900)
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Badlato <joseph.badlato@schrodinger.com>
  • Loading branch information
jbadlato committed Aug 4, 2022
1 parent 46b6862 commit cf684e2
Show file tree
Hide file tree
Showing 13 changed files with 1,474 additions and 610 deletions.
8 changes: 8 additions & 0 deletions internal/controllers/core/cmd/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ func (i input) stringValue() string {
}
} else if i.status.Hidden != nil {
return i.status.Hidden.Value
} else if i.status.Choice != nil {
for _, v := range i.spec.Choice.Choices {
if v == i.status.Choice.Value {
return v
}
}
// if value is invalid, we default to the first choice
return i.spec.Choice.Choices[0]
}
return ""
}
Expand Down
32 changes: 32 additions & 0 deletions internal/controllers/core/cmd/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,38 @@ func TestHiddenInput(t *testing.T) {
require.Equal(t, expectedEnv, actualEnv)
}

func TestChoiceInput(t *testing.T) {
for _, tc := range []struct {
name string
input v1alpha1.UIChoiceInputSpec
value string
expectedValue string
}{
{"empty value", v1alpha1.UIChoiceInputSpec{Choices: []string{"choice1", "choice2"}}, "", "choice1"},
{"invalid value", v1alpha1.UIChoiceInputSpec{Choices: []string{"choice1", "choice2"}}, "not in Choices", "choice1"},
{"selected choice1", v1alpha1.UIChoiceInputSpec{Choices: []string{"choice1", "choice2"}}, "choice1", "choice1"},
{"selected choice2", v1alpha1.UIChoiceInputSpec{Choices: []string{"choice1", "choice2"}}, "choice2", "choice2"},
} {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)

setupStartOnTest(t, f)
f.updateButton("b-1", func(button *v1alpha1.UIButton) {
spec := v1alpha1.UIInputSpec{Name: "dry_run", Choice: &tc.input}
button.Spec.Inputs = append(button.Spec.Inputs, spec)
status := v1alpha1.UIInputStatus{Name: "dry_run", Choice: &v1alpha1.UIChoiceInputStatus{Value: tc.value}}
button.Status.Inputs = append(button.Status.Inputs, status)
})
f.triggerButton("b-1", f.clock.Now())
f.reconcileCmd("testcmd")

actualEnv := f.fe.processes["myserver"].env
expectedEnv := []string{fmt.Sprintf("dry_run=%s", tc.expectedValue)}
require.Equal(t, expectedEnv, actualEnv)
})
}
}

func TestCmdOnlyUsesButtonThatStartedIt(t *testing.T) {
f := newFixture(t)

Expand Down
128 changes: 127 additions & 1 deletion internal/tiltfile/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func (p Plugin) registerSymbols(env *starkit.Environment) error {
if err != nil {
return err
}
err = env.AddBuiltin("v1alpha1.ui_choice_input_spec", p.uIChoiceInputSpec)
if err != nil {
return err
}
err = env.AddBuiltin("v1alpha1.ui_component_location", p.uIComponentLocation)
if err != nil {
return err
Expand Down Expand Up @@ -3510,6 +3514,111 @@ func (o *UIBoolInputSpecList) Unpack(v starlark.Value) error {
return nil
}

type UIChoiceInputSpec struct {
*starlark.Dict
Value v1alpha1.UIChoiceInputSpec
isUnpacked bool
t *starlark.Thread // instantiation thread for computing abspath
}

func (p Plugin) uIChoiceInputSpec(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var choices starlark.Value
err := starkit.UnpackArgs(t, fn.Name(), args, kwargs,
"choices?", &choices,
)
if err != nil {
return nil, err
}

dict := starlark.NewDict(1)

if choices != nil {
err := dict.SetKey(starlark.String("choices"), choices)
if err != nil {
return nil, err
}
}
var obj *UIChoiceInputSpec = &UIChoiceInputSpec{t: t}
err = obj.Unpack(dict)
if err != nil {
return nil, err
}
return obj, nil
}

func (o *UIChoiceInputSpec) Unpack(v starlark.Value) error {
obj := v1alpha1.UIChoiceInputSpec{}

starlarkObj, ok := v.(*UIChoiceInputSpec)
if ok {
*o = *starlarkObj
return nil
}

mapObj, ok := v.(*starlark.Dict)
if !ok {
return fmt.Errorf("expected dict, actual: %v", v.Type())
}

for _, item := range mapObj.Items() {
keyV, val := item[0], item[1]
key, ok := starlark.AsString(keyV)
if !ok {
return fmt.Errorf("key must be string. Got: %s", keyV.Type())
}

if key == "choices" {
var v value.StringList
err := v.Unpack(val)
if err != nil {
return fmt.Errorf("unpacking %s: %v", key, err)
}
obj.Choices = v
continue
}
return fmt.Errorf("Unexpected attribute name: %s", key)
}

mapObj.Freeze()
o.Dict = mapObj
o.Value = obj
o.isUnpacked = true

return nil
}

type UIChoiceInputSpecList struct {
*starlark.List
Value []v1alpha1.UIChoiceInputSpec
t *starlark.Thread
}

func (o *UIChoiceInputSpecList) Unpack(v starlark.Value) error {
items := []v1alpha1.UIChoiceInputSpec{}

listObj, ok := v.(*starlark.List)
if !ok {
return fmt.Errorf("expected list, actual: %v", v.Type())
}

for i := 0; i < listObj.Len(); i++ {
v := listObj.Index(i)

item := UIChoiceInputSpec{t: o.t}
err := item.Unpack(v)
if err != nil {
return fmt.Errorf("at index %d: %v", i, err)
}
items = append(items, v1alpha1.UIChoiceInputSpec(item.Value))
}

listObj.Freeze()
o.List = listObj
o.Value = items

return nil
}

type UIComponentLocation struct {
*starlark.Dict
Value v1alpha1.UIComponentLocation
Expand Down Expand Up @@ -3747,18 +3856,20 @@ func (p Plugin) uIInputSpec(t *starlark.Thread, fn *starlark.Builtin, args starl
var text starlark.Value
var bool starlark.Value
var hidden starlark.Value
var choice starlark.Value
err := starkit.UnpackArgs(t, fn.Name(), args, kwargs,
"name?", &name,
"label?", &label,
"text?", &text,
"bool?", &bool,
"hidden?", &hidden,
"choice?", &choice,
)
if err != nil {
return nil, err
}

dict := starlark.NewDict(5)
dict := starlark.NewDict(6)

if name != nil {
err := dict.SetKey(starlark.String("name"), name)
Expand Down Expand Up @@ -3790,6 +3901,12 @@ func (p Plugin) uIInputSpec(t *starlark.Thread, fn *starlark.Builtin, args starl
return nil, err
}
}
if choice != nil {
err := dict.SetKey(starlark.String("choice"), choice)
if err != nil {
return nil, err
}
}
var obj *UIInputSpec = &UIInputSpec{t: t}
err = obj.Unpack(dict)
if err != nil {
Expand Down Expand Up @@ -3862,6 +3979,15 @@ func (o *UIInputSpec) Unpack(v starlark.Value) error {
obj.Hidden = (*v1alpha1.UIHiddenInputSpec)(&v.Value)
continue
}
if key == "choice" {
v := UIChoiceInputSpec{t: o.t}
err := v.Unpack(val)
if err != nil {
return fmt.Errorf("unpacking %s: %v", key, err)
}
obj.Choice = (*v1alpha1.UIChoiceInputSpec)(&v.Value)
continue
}
return fmt.Errorf("Unexpected attribute name: %s", key)
}

Expand Down

0 comments on commit cf684e2

Please sign in to comment.