From a1ecfa6a20bd4ef9e9caded262ee1b1b26847675 Mon Sep 17 00:00:00 2001 From: Nick Miyake Date: Fri, 9 Jun 2017 14:46:44 -0700 Subject: [PATCH 1/6] Fix apostrophes in README Fix "it's" -> "its". --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 25181df..577088f 100644 --- a/README.md +++ b/README.md @@ -575,13 +575,13 @@ initialization needed to begin using Viper. Since most applications will want to use a single central repository for their configuration, the viper package provides this. It is similar to a singleton. -In all of the examples above, they demonstrate using viper in it's singleton +In all of the examples above, they demonstrate using viper in its singleton style approach. ### Working with multiple vipers You can also create many different vipers for use in your application. Each will -have it’s own unique set of configurations and values. Each can read from a +have its own unique set of configurations and values. Each can read from a different config file, key value store, etc. All of the functions that viper package supports are mirrored as methods on a viper. From c1de95864d73a5465492829d7cb2dd422b19ac96 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Mon, 19 Jun 2017 12:35:39 +0200 Subject: [PATCH 2/6] Prevent redundant type assertion in Get MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is no need to assert variables which already have appropriate type. name old time/op new time/op delta GetBool-4 554ns ± 3% 493ns ± 8% -10.86% (p=0.000 n=14+15) Get-4 484ns ± 4% 414ns ± 7% -14.37% (p=0.000 n=14+15) GetBoolFromMap-4 8.38ns ± 6% 7.83ns ± 7% -6.59% (p=0.000 n=15+15) name old alloc/op new alloc/op delta GetBool-4 65.0B ± 0% 64.0B ± 0% -1.54% (p=0.000 n=15+15) Get-4 64.0B ± 0% 64.0B ± 0% ~ (all equal) GetBoolFromMap-4 0.00B 0.00B ~ (all equal) name old allocs/op new allocs/op delta GetBool-4 5.00 ± 0% 4.00 ± 0% -20.00% (p=0.000 n=15+15) Get-4 5.00 ± 0% 4.00 ± 0% -20.00% (p=0.000 n=15+15) GetBoolFromMap-4 0.00 0.00 ~ (all equal) --- viper.go | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/viper.go b/viper.go index 31b41a6..39a3c06 100644 --- a/viper.go +++ b/viper.go @@ -53,7 +53,7 @@ func init() { type remoteConfigFactory interface { Get(rp RemoteProvider) (io.Reader, error) Watch(rp RemoteProvider) (io.Reader, error) - WatchChannel(rp RemoteProvider)(<-chan *RemoteResponse, chan bool) + WatchChannel(rp RemoteProvider) (<-chan *RemoteResponse, chan bool) } // RemoteConfig is optional, see the remote package @@ -597,32 +597,33 @@ func (v *Viper) Get(key string) interface{} { return nil } - valType := val if v.typeByDefValue { // TODO(bep) this branch isn't covered by a single test. + valType := val path := strings.Split(lcaseKey, v.keyDelim) defVal := v.searchMap(v.defaults, path) if defVal != nil { valType = defVal } + + switch valType.(type) { + case bool: + return cast.ToBool(val) + case string: + return cast.ToString(val) + case int64, int32, int16, int8, int: + return cast.ToInt(val) + case float64, float32: + return cast.ToFloat64(val) + case time.Time: + return cast.ToTime(val) + case time.Duration: + return cast.ToDuration(val) + case []string: + return cast.ToStringSlice(val) + } } - switch valType.(type) { - case bool: - return cast.ToBool(val) - case string: - return cast.ToString(val) - case int64, int32, int16, int8, int: - return cast.ToInt(val) - case float64, float32: - return cast.ToFloat64(val) - case time.Time: - return cast.ToTime(val) - case time.Duration: - return cast.ToDuration(val) - case []string: - return cast.ToStringSlice(val) - } return val } From df7314a14e26fea5443bb6e696b207d14d9a266d Mon Sep 17 00:00:00 2001 From: Anthony Fok Date: Fri, 21 Jul 2017 04:20:10 -0600 Subject: [PATCH 3/6] Make minor copy-editing changes to README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 577088f..8e723d4 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Many Go projects are built using Viper including: * [Hugo](http://gohugo.io) * [EMC RexRay](http://rexray.readthedocs.org/en/stable/) -* [Imgur's Incus](https://github.com/Imgur/incus) +* [Imgur’s Incus](https://github.com/Imgur/incus) * [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack) * [Docker Notary](https://github.com/docker/Notary) * [BloomApi](https://www.bloomapi.com/) @@ -17,7 +17,7 @@ Many Go projects are built using Viper including: ## What is Viper? -Viper is a complete configuration solution for go applications including 12 factor apps. It is designed +Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats. It supports: @@ -68,7 +68,7 @@ Viper configuration keys are case insensitive. ### Establishing Defaults A good configuration system will support default values. A default value is not -required for a key, but it's useful in the event that a key hasn’t been set via +required for a key, but it’s useful in the event that a key hasn’t been set via config file, environment variable, remote configuration or flag. Examples: @@ -271,7 +271,7 @@ func main() { #### Flag interfaces -Viper provides two Go interfaces to bind other flag systems if you don't use `Pflags`. +Viper provides two Go interfaces to bind other flag systems if you don’t use `Pflags`. `FlagValue` represents a single flag. This is a very simple example on how to implement this interface: @@ -401,7 +401,7 @@ go func(){ ## Getting Values From Viper -In Viper, there are a few ways to get a value depending on the value's type. +In Viper, there are a few ways to get a value depending on the value’s type. The following functions and methods exist: * `Get(key string) : interface{}` @@ -531,7 +531,7 @@ func NewCache(cfg *Viper) *Cache {...} ``` which creates a cache based on config information formatted as `subv`. -Now it's easy to create these 2 caches separately as: +Now it’s easy to create these 2 caches separately as: ```go cfg1 := viper.Sub("app.cache1") From 16a945857303e8ea14318664816932b07a0ed5ce Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Sun, 23 Jul 2017 06:54:24 +0200 Subject: [PATCH 4/6] Fix indentation in README.md See #345 --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8e723d4..e088cbf 100644 --- a/README.md +++ b/README.md @@ -116,10 +116,10 @@ Optionally you can provide a function for Viper to run each time a change occurs **Make sure you add all of the configPaths prior to calling `WatchConfig()`** ```go - viper.WatchConfig() - viper.OnConfigChange(func(e fsnotify.Event) { - fmt.Println("Config file changed:", e.Name) - }) +viper.WatchConfig() +viper.OnConfigChange(func(e fsnotify.Event) { + fmt.Println("Config file changed:", e.Name) +}) ``` ### Reading Config from io.Reader From 9766537120e76571a9a2dcafe4b25a3201dcac22 Mon Sep 17 00:00:00 2001 From: Nikola Kovacs Date: Sun, 23 Jul 2017 07:09:41 +0200 Subject: [PATCH 5/6] Fix grammar/typo in comment for getEnv() See #310 --- viper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 39a3c06..4631e31 100644 --- a/viper.go +++ b/viper.go @@ -315,7 +315,7 @@ func (v *Viper) mergeWithEnvPrefix(in string) string { // (cammel case to snake case for JSON keys perhaps) // getEnv is a wrapper around os.Getenv which replaces characters in the original -// key. This allows env vars which have different keys then the config object +// key. This allows env vars which have different keys than the config object // keys func (v *Viper) getEnv(key string) string { if v.envKeyReplacer != nil { From 04691bc570c10b492d8bf7b66aae05e0c9ecb0db Mon Sep 17 00:00:00 2001 From: Brad Peabody Date: Sat, 22 Jul 2017 22:26:21 -0700 Subject: [PATCH 6/6] Improve documentation for flags See #329 --- README.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e088cbf..5221c14 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,7 @@ Like `BindEnv`, the value is not set when the binding method is called, but when it is accessed. This means you can bind as early as you want, even in an `init()` function. -The `BindPFlag()` method provides this functionality. +For individual flags, the `BindPFlag()` method provides this functionality. Example: @@ -245,6 +245,19 @@ serverCmd.Flags().Int("port", 1138, "Port to run Application server on") viper.BindPFlag("port", serverCmd.Flags().Lookup("port")) ``` +You can also bind an existing set of pflags (pflag.FlagSet): + +Example: + +```go +pflag.Int("flagname", 1234, "help message for flagname") + +pflag.Parse() +viper.BindPFlags(pflag.CommandLine) + +i := viper.GetInt("flagname") // retrieve values from viper instead of pflag +``` + The use of [pflag](https://github.com/spf13/pflag/) in Viper does not preclude the use of other packages that use the [flag](https://golang.org/pkg/flag/) package from the standard library. The pflag package can handle the flags @@ -263,9 +276,17 @@ import ( ) func main() { + + // using standard library "flag" package + flag.Int("flagname", 1234, "help message for flagname") + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() - ... + viper.BindPFlags(pflag.CommandLine) + + i := viper.GetInt("flagname") // retrieve value from viper + + ... } ```