Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #647 from wwitzel3/issue-646-button
Browse files Browse the repository at this point in the history
allow button group to be unmarshaled from JSON
  • Loading branch information
Sam Foo committed Feb 9, 2020
2 parents c52a347 + c525ff3 commit f17f454
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
5 changes: 1 addition & 4 deletions pkg/view/component/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package component

import (
"encoding/json"

"github.com/vmware-tanzu/octant/pkg/action"
)

Expand All @@ -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
}
}

Expand Down
33 changes: 32 additions & 1 deletion pkg/view/component/flexlayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
26 changes: 18 additions & 8 deletions pkg/view/component/testdata/config_flexlayout.json
Original file line number Diff line number Diff line change
@@ -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"
}]
}
}
}
6 changes: 5 additions & 1 deletion pkg/view/component/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package component

import (
"encoding/json"

"github.com/pkg/errors"
)

Expand All @@ -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),
Expand Down
8 changes: 8 additions & 0 deletions pkg/view/component/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func Test_unmarshal(t *testing.T) {
},
},
},
ButtonGroup: &ButtonGroup{
base: base{},
Config: ButtonGroupConfig{
Buttons: []Button{{
Name: "test",
}},
},
},
},
base: newBase(typeFlexLayout, nil),
},
Expand Down

0 comments on commit f17f454

Please sign in to comment.