Skip to content

PR #673 broke my code. The nested structures don't get the default values correctly #764

@flw-cn

Description

@flw-cn

I wrote a short example to illustrate the problem.

package main

import (
	"fmt"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

type Config struct {
	Foo struct {
		Bar string
	}
}

func main() {
	config := &Config{}
	cmdRoot := &cobra.Command{
		Run: func(*cobra.Command, []string) {
			viper.Unmarshal(config)
		},
	}
	flags := cmdRoot.PersistentFlags()
	flags.String("foo.bar", "hello-world", "test default value fo foo.bar")
	viper.BindPFlags(flags)
	cmdRoot.Execute()
	fmt.Printf("foo.bar: [%v]\n", config.Foo.Bar)
}

The above code I expected to get the output is:

foo.bar: [hello-world]

This has worked very well in the past. But since PR #673, it can't work properly, it will output:

foo.bar: []

I checked the contents of the PR #673 , I can't fully understand the changes. But I believe the changes blocked my previous usage.

How should I modify my code to fit the new version of viper?

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