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

Unmarshal and WriteConfigAs work with different tag #1951

Closed
3 tasks done
Martzki opened this issue Nov 19, 2024 · 1 comment
Closed
3 tasks done

Unmarshal and WriteConfigAs work with different tag #1951

Martzki opened this issue Nov 19, 2024 · 1 comment
Labels
kind/bug Something isn't working

Comments

@Martzki
Copy link

Martzki commented Nov 19, 2024

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.
  • I am not looking for support or already pursued the available support channels without success.
  • I have checked the troubleshooting guide for my problem, without success.

Viper Version

1.19.0

Go Version

1.22.5

Config Source

Files

Format

YAML

Repl.it link

No response

Code reproducing the issue

package main

import (
	"fmt"
	"github.com/spf13/viper"
)

type Data struct {
	MemberA string `yaml:"member_a"`
	MemberB string `yaml:"member_b"`
}

func main() {
	// Write
	v := viper.New()
	v.SetConfigType("yaml")
	v.Set("test", Data{MemberA: "a", MemberB: "b"})
	_ = v.WriteConfigAs("1.yaml")

	// Read
	v = viper.New()
	v.SetConfigFile("1.yaml")
	_ = v.ReadInConfig()
	data := make(map[string]Data)
	_ = v.Unmarshal(&data)
	fmt.Println("data from 1.yaml")
	fmt.Println(data)
}

Expected Behavior

The data read from 1.yaml is correct.

Actual Behavior

$ go run viper.go 
data from 1.yaml
map[test:{ }]
$ cat 1.yaml
test:
    member_a: a
    member_b: b

Steps To Reproduce

No response

Additional Information

Unmarshal works with tag mapstructure but WriteConfigAs works with tag yaml when the yaml key is snake case like member_a.
Even the member has both two tags, the problem is still existed:

type Data struct {
        MemberA string `yaml:"member_a" ,mapstructure: "member_a"`
        MemberB string `yaml:"member_b" ,mapstructure: "member_b"`
}
@Martzki Martzki added the kind/bug Something isn't working label Nov 19, 2024
@Martzki
Copy link
Author

Martzki commented Nov 19, 2024

Wrong tag format, use delimiter will work.

type Data struct {
        MemberA string `yaml:"member_a" mapstructure: "member_a"`
        MemberB string `yaml:"member_b" mapstructure: "member_b"`
}

@Martzki Martzki closed this as completed Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant