Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Use tag names for decoding nested map[string]interface{} #320

@jet-go

Description

@jet-go

Hello
I've a config of format

        type ListItem struct {
		SemVersion string `mapstructure:"sem_version"`
	}
	type Config struct {
		ListItems map[string]ListItem `mapstructure:"list_items"`
	}

	cfg := Config{
		ListItems: map[string]ListItem{
			"item-1": {SemVersion: "v1"},
			"item2":  {SemVersion: "v2"},
			"item_3": {SemVersion: "v3"},
		},
	}

	out := make(map[string]interface{})
	if err := mapstructure.Decode(cfg, &out); err != nil {
		panic(err)
	}

	jsonBytes, err := json.Marshal(&out)
	if err != nil {
		panic(err)
	}
	fmt.Printf(string(jsonBytes))
Output: {"list_items":{"item-1":{"SemVersion":"v1"},"item2":{"SemVersion":"v2"},"item_3":{"SemVersion":"v3"}}}

Expected: {"list_items":{"item-1":{"sem_version":"v1"},"item2":{"sem_version":"v2"},"item_3":{"sem_version":"v3"}}}

Is there any way to get the tag names in nested items too?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions