Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant