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
pull/444/head
Tony-Sol 1 year ago
parent 8ab751ef32
commit 781af68827
No known key found for this signature in database
GPG Key ID: 0B27B02E462B2C67

@ -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{

@ -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)
}

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

@ -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

@ -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())

Loading…
Cancel
Save