Repository: vmware-tanzu/octant
Pull request #647 (2020-02-08 17:05:14)

allow button group to be unmarshaled from JSON
Signed-off-by: Wayne Witzel III <wayne@riotousliving.com>


**What this PR does / why we need it**:
Allows ButtonGroup to be used by Plugins.

**Which issue(s) this PR fixes**
- related #646

**Special notes for your reviewer**: Example plugin https://github.com/wwitzel3/octant-actions-plugin



Patch:
diff --git a/pkg/view/component/button.go b/pkg/view/component/button.go
index 516fd408..133a579b 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 25921fa5..36a8d59e 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/unmarshal.go b/pkg/view/component/unmarshal.go
index dfea1f08..4c19dfce 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),


Test patch:
diff --git a/pkg/view/component/testdata/config_flexlayout.json b/pkg/view/component/testdata/config_flexlayout.json
index 59b43314..ff87f1d0 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_test.go b/pkg/view/component/unmarshal_test.go
index 21047e6c..3f0f0469 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),
 			},