From 781af688275bfc0452a032ecf58db0cfe6af4936 Mon Sep 17 00:00:00 2001 From: Tony-Sol Date: Sat, 20 May 2023 17:56:54 +0300 Subject: [PATCH] Cleanup json-related code with additions `detailsFormat` key removed from config `MarshalIntoFormat` renamed into `marshalIntoFormat` with better description `MarshalIntoYaml` introduced as `marshalIntoFormat` wrapper for common usage --- pkg/config/app_config.go | 5 ----- pkg/gui/containers_panel.go | 2 +- pkg/gui/presentation/container_stats.go | 2 +- pkg/utils/utils.go | 11 +++++++++-- pkg/utils/utils_test.go | 4 ++-- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 6565101e..c3a0994a 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -130,10 +130,6 @@ type GuiConfig struct { // When true, increases vertical space used by focused side panel, // creating an accordion effect ExpandFocusedSidePanel bool `yaml:"expandFocusedSidePanel"` - - // DetailsFormat determines in which format will be printed container stats and configs - // By default "yaml", available values "json", "yaml" - DetailsFormat string `yaml:"detailsFormat,omitempty"` } // CommandTemplatesConfig determines what commands actually get called when we @@ -366,7 +362,6 @@ func GetDefaultConfig() UserConfig { SidePanelWidth: 0.3333, ShowBottomLine: true, ExpandFocusedSidePanel: false, - DetailsFormat: "yaml", }, ConfirmOnQuit: false, Logs: LogsConfig{ diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index b525b5cf..98499df5 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -199,7 +199,7 @@ func (gui *Gui) containerConfigStr(container *commands.Container) string { output += "none\n" } - data, err := utils.MarshalIntoFormat(&container.Details, gui.Config.UserConfig.Gui.DetailsFormat) + data, err := utils.MarshalIntoYaml(&container.Details) if err != nil { return fmt.Sprintf("Error marshalling container details: %v", err) } diff --git a/pkg/gui/presentation/container_stats.go b/pkg/gui/presentation/container_stats.go index cd8009ee..e3a9b3dc 100644 --- a/pkg/gui/presentation/container_stats.go +++ b/pkg/gui/presentation/container_stats.go @@ -37,7 +37,7 @@ func RenderStats(userConfig *config.UserConfig, container *commands.Container, v dataReceived := fmt.Sprintf("Traffic received: %s", utils.FormatDecimalBytes(stats.ClientStats.Networks.Eth0.RxBytes)) dataSent := fmt.Sprintf("Traffic sent: %s", utils.FormatDecimalBytes(stats.ClientStats.Networks.Eth0.TxBytes)) - originalStats, err := utils.MarshalIntoFormat(stats, userConfig.Gui.DetailsFormat) + originalStats, err := utils.MarshalIntoYaml(stats) if err != nil { return "", err } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index eae9e268..b9eb801f 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -385,7 +385,14 @@ func OpensMenuStyle(str string) string { return ColoredString(fmt.Sprintf("%s...", str), color.FgMagenta) } -func MarshalIntoFormat(data interface{}, format string) ([]byte, error) { +// MarshalIntoYaml gets any json-tagged data and marshal it into yaml saving original json structure. +// Useful for structs from 3rd-party libs without yaml tags. +func MarshalIntoYaml(data interface{}) ([]byte, error) { + return marshalIntoFormat(data, "yaml") +} + +func marshalIntoFormat(data interface{}, format string) ([]byte, error) { + // First marshal struct->json to get the resulting structure declared by json tags dataJSON, err := json.MarshalIndent(data, "", " ") if err != nil { return nil, err @@ -394,7 +401,7 @@ func MarshalIntoFormat(data interface{}, format string) ([]byte, error) { case "json": return dataJSON, err case "yaml": - // Use Unmarshal->Marshal hack to convert vendor-locked objects to YAML with same structure as JSON + // Use Unmarshal->Marshal hack to convert json into yaml with the original structure preserved var dataMirror yaml.MapSlice if err := yaml.Unmarshal(dataJSON, &dataMirror); err != nil { return nil, err diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 65e12cf9..0e9f9c54 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -270,7 +270,7 @@ func TestMarshalIntoFormat(t *testing.T) { } type scenario struct { - input data + input interface{} format string expected []byte expectedErr error @@ -310,7 +310,7 @@ quux: } for _, s := range scenarios { - output, err := MarshalIntoFormat(s.input, s.format) + output, err := marshalIntoFormat(s.input, s.format) assert.EqualValues(t, s.expected, output) if s.expectedErr != nil { assert.EqualError(t, err, s.expectedErr.Error())