diff --git a/pkg/view/component/button.go b/pkg/view/component/button.go index 516fd40801..133a579b65 100644 --- a/pkg/view/component/button.go +++ b/pkg/view/component/button.go @@ -2,7 +2,6 @@ package component import ( "encoding/json" - "github.com/vmware-tanzu/octant/pkg/action" ) @@ -18,12 +17,10 @@ type ButtonOption func(button *Button) // WithButtonConfirmation configured a button with a confirmation. func WithButtonConfirmation(title, body string) ButtonOption { return func(button *Button) { - confirmation := Confirmation{ + button.Confirmation = &Confirmation{ Title: title, Body: body, } - - button.Confirmation = &confirmation } } diff --git a/pkg/view/component/flexlayout.go b/pkg/view/component/flexlayout.go index 25921fa5a8..36a8d59ed7 100644 --- a/pkg/view/component/flexlayout.go +++ b/pkg/view/component/flexlayout.go @@ -5,7 +5,10 @@ SPDX-License-Identifier: Apache-2.0 package component -import "encoding/json" +import ( + "encoding/json" + "errors" +) const ( // WidthFull is a full width section. @@ -55,6 +58,34 @@ type FlexLayoutConfig struct { ButtonGroup *ButtonGroup `json:"buttonGroup,omitempty"` } +func (f *FlexLayoutConfig) UnmarshalJSON(data []byte) error { + x := struct { + Sections []FlexLayoutSection `json:"sections,omitempty"` + ButtonGroup *TypedObject `json:"buttonGroup,omitempty"` + }{} + + if err := json.Unmarshal(data, &x); err != nil { + return err + } + + if x.ButtonGroup != nil { + component, err := x.ButtonGroup.ToComponent() + if err != nil { + return err + } + + buttonGroup, ok := component.(*ButtonGroup) + if !ok { + return errors.New("item was not a buttonGroup") + } + f.ButtonGroup = buttonGroup + } + + f.Sections = x.Sections + + return nil +} + // FlexLayout is a flex layout view. type FlexLayout struct { base diff --git a/pkg/view/component/testdata/config_flexlayout.json b/pkg/view/component/testdata/config_flexlayout.json index 59b433143b..ff87f1d0ab 100644 --- a/pkg/view/component/testdata/config_flexlayout.json +++ b/pkg/view/component/testdata/config_flexlayout.json @@ -1,17 +1,27 @@ { - "sections":[ + "sections": [ [ { - "width":24, - "view":{ - "metadata":{ - "type":"text" + "width": 24, + "view": { + "metadata": { + "type": "text" }, - "config":{ - "value":"text" + "config": { + "value": "text" } } } ] - ] + ], + "buttonGroup": { + "metadata": { + "type": "buttonGroup" + }, + "config": { + "buttons": [{ + "name": "test" + }] + } + } } diff --git a/pkg/view/component/unmarshal.go b/pkg/view/component/unmarshal.go index dfea1f080b..4c19dfcedd 100644 --- a/pkg/view/component/unmarshal.go +++ b/pkg/view/component/unmarshal.go @@ -7,7 +7,6 @@ package component import ( "encoding/json" - "github.com/pkg/errors" ) @@ -21,6 +20,11 @@ func unmarshal(to TypedObject) (Component, error) { err = errors.Wrapf(json.Unmarshal(to.Config, &t.Config), "unmarshal annotations config") o = t + case typeButtonGroup: + t := &ButtonGroup{base: base{Metadata: to.Metadata}} + err = errors.Wrapf(json.Unmarshal(to.Config, &t.Config), + "unmarshal buttonGroup config") + o = t case typeCard: t := &Card{base: base{Metadata: to.Metadata}} err = errors.Wrapf(json.Unmarshal(to.Config, &t.Config), diff --git a/pkg/view/component/unmarshal_test.go b/pkg/view/component/unmarshal_test.go index 21047e6c5b..3f0f04697a 100644 --- a/pkg/view/component/unmarshal_test.go +++ b/pkg/view/component/unmarshal_test.go @@ -94,6 +94,14 @@ func Test_unmarshal(t *testing.T) { }, }, }, + ButtonGroup: &ButtonGroup{ + base: base{}, + Config: ButtonGroupConfig{ + Buttons: []Button{{ + Name: "test", + }}, + }, + }, }, base: newBase(typeFlexLayout, nil), },