Skip to content

For V2: Add default value support #1981

Open
@lfcypo

Description

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Problem Description

I use viper in web development to help me manage and read configuration files. One problem is: I need to connect to the database. When the specific database.type is not indicated in the configuration file, I hope I can point to the sqlite database by default, but viper does not support providing default configuration when the configuration file fails to be obtained, resulting in a lot of redundancy and unsightly appearance in my code. I want viper to provide an optional parameter to the function to provide a default configuration item. Viper is a good library.

Proposed Solution

It is hoped that the default value option can be supported when the configuration item fails to be obtained. Just like when the viper.GetString("database.type") function fails to get the configuration, it allows you to manually configure a return value, just like the viper.GetString("database.type", "sqlite"), where the second parameter represents the default value returned if the get fails.

Alternatives Considered

Currently I am working through the viper.IsSet solve the problem.

func GetString(key string, defaultValue string) string {
	if viper.IsSet(key) {
		return viper.GetString(key)
	} else {
		return defaultValue
	}
}

But I still hope that viper will provide a solution to this problem gracefully in the future.

Additional Information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions