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

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

Closed
jet-go opened this issue Feb 27, 2023 · 1 comment
Closed

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

jet-go opened this issue Feb 27, 2023 · 1 comment

Comments

@jet-go
Copy link

jet-go commented Feb 27, 2023

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?

@jet-go
Copy link
Author

jet-go commented Feb 27, 2023

My bad.
Another issue already exists with an open PR.
Let me close this.

@jet-go jet-go closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant