Commit graph

199 commits

Author SHA1 Message Date
Kamil Wargula
4557438166 Added new method for getting Defaults values from config.
In some cases, app needs to print default values for user.
This PR add new `GetDefaults` method.
This feature was mentioned in #376
2017-10-20 13:55:12 +02:00
Kamil Wargula
8ef37cbca7 Fix incorrect name of function in README.md
Change `SetEnvReplacer` to `SetEnvKeyReplacer`
2017-10-20 12:40:09 +02:00
Bjørn Erik Pedersen
d9cca5ef33 Go fmt viper.go 2017-09-29 23:06:42 +02:00
Alexander Krasnukhin
266e588e9e Add string slice support to defaultDecoderConfig
This way it correctly decodes string slices as well.
2017-09-29 21:44:38 +02:00
JG²
25b30aa063 Add Clairctl as a Go project that uses Viper
See #354
2017-07-22 23:52:07 -06:00
Nick Miyake
8ac2e2e20f Run gofmt on all existing code
Also:

* Add check to .travis.yml that verifies that all code is gofmt-compliant
* Touch up some newlines

See #351
2017-07-22 23:47:47 -06:00
Nick Miyake
f257d19100 Fix a few minor typos/formatting issues with comments
See #350
2017-07-22 23:39:01 -06:00
Brad Peabody
04691bc570 Improve documentation for flags
See #329
2017-07-22 23:26:21 -06:00
Nikola Kovacs
9766537120 Fix grammar/typo in comment for getEnv()
See #310
2017-07-22 23:09:41 -06:00
Albert Nigmatzianov
16a9458573 Fix indentation in README.md
See #345
2017-07-22 22:54:24 -06:00
Anthony Fok
df7314a14e Make minor copy-editing changes to README.md 2017-07-21 04:20:10 -06:00
Albert Nigmatzianov
c1de95864d Prevent redundant type assertion in Get
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)
2017-06-19 14:43:13 +02:00
Nick Miyake
a1ecfa6a20 Fix apostrophes in README
Fix "it's" -> "its".
2017-06-10 01:09:38 +02:00
Paweł Szczur
0967fc9ace Properly handle string slice values 2017-04-17 10:08:15 +02:00
Miguel Eduardo Gil Biraud
5d46e70da8 Fix UnmarshalKey handling of time.Duration
* Failing test for key unmarshaling with nested structs containing time.Duration

* Fix UnmarshalKey to use of defaultDecoderConfig
2017-04-10 11:26:50 +02:00
Wolfgang Friedl
84f94806c6 Avoid the start of go-routines which are never get stopped 2017-03-15 14:43:09 +01:00
Bjørn Erik Pedersen
0b5690fd87 Revert "remote: Avoid the start of go-routines which are never get stopped"
This reverts commit 11ca61e888.
2017-03-15 08:10:26 +01:00
Wolfgang Friedl
11ca61e888 remote: Avoid the start of go-routines which are never get stopped 2017-03-15 08:08:46 +01:00
Bjørn Erik Pedersen
7538d73b4e travis: Bump to Go 1.7.5 and 1.8 (Yay!) 2017-02-17 17:38:17 +01:00
Cameron Moore
d90f2bb139 Only save config on success in ReadInConfig
If the user creates a invalid config file while watching for config
changes, the previous, valid config is not retained.  This commit only
overwrites the running config if unmarshalling was successful.
2017-02-17 17:31:47 +01:00
Kevin GEORGES
5ed0fc31f7 Fix MergeInConfig error return
UnsupportedConfigError was returned if config file not found

* Swap getConfigFile and getConfigType call
* Add a unit test
2016-12-13 10:38:49 +01:00
Albert Nigmatzianov
651d9d916a Document case-insensitivity for key taking methods 2016-10-29 23:33:52 +02:00
Bjørn Erik Pedersen
80ab6657f9 Copy and insensitivise maps in Set
Fixes #261
Closes #265
2016-10-24 21:20:41 +02:00
Benoît Masson
285f151019 Fixed AllKeys() to include env values added with BindEnv()
* Fixed: values bound with BindEnv added to AllKeys()

Cast was not working, and v.env wasn't used when merging keys.

Rewrote explicit and specific casts for maps storing strings or FlagValues.

* Added: test for BindEnv() and AllKeys()

To make sure AllSettings() and Unmarshal() will consider environment
variables added with BindEnv().
2016-10-23 23:04:21 +02:00
Benoît Masson
50515b700e Increase performance of nested keys search
* Fixed: insensitiviseMaps and tests

All keys (even nested ones) are now lower-cased recursively.

On the way, map[interface{}]interface{} are cast to map[string]interface{}

* Changed: simplified find() fast path and increase performance

Removed searchMapForKey(), fast path directly integrated into searchMap() and
searchMapWithPathPrefixes()
=> more generic (searchMapForKey() wasn't called everywhere it should have)

At the same time, significantly speed up searchMap() and searchMapWithPathPrefixes(),
which are still used for nested keys: the assumption that map keys are all
lower-cased allows to perform
    val = m[key]
instead of
    for k, v := range m {
      if strings.ToLower(k) == strings.ToLower(key) {
        val = v
      }
    }
=> i.e., directly access the map instead of enumerate the keys
2016-10-14 11:24:45 +02:00
Jonathan Anderson
44208030b3 Allow errors to propagate from getConfigFile(). (#161)
- propagate ConfigFileNotFoundError instead of using unsupported config type error when config file is not found
2016-10-13 13:33:30 +02:00
Nils Landt
c14ce6d43c Correct FlagValue interface examples in Readme (#254)
`IsChanged` is actually named `HasChanged`, and the examples were missing function return declarations, so you couldn't just copy/paste it.
2016-10-12 11:40:13 +02:00
Bjørn Erik Pedersen
51f23d1f1c Restore performance for the simple case
```
BenchmarkGetBool-4            1021          481           -52.89%
BenchmarkGet-4                879           403           -54.15%
BenchmarkGetBoolFromMap-4     6.56          6.40          -2.44%

benchmark                     old allocs     new allocs     delta
BenchmarkGetBool-4            6              4              -33.33%
BenchmarkGet-4                6              4              -33.33%
BenchmarkGetBoolFromMap-4     0              0              +0.00%

benchmark                     old bytes     new bytes     delta
BenchmarkGetBool-4            113           49            -56.64%
BenchmarkGet-4                112           48            -57.14%
BenchmarkGetBoolFromMap-4     0             0             +0.00%
```

Fixes #249
Fixes https://github.com/spf13/hugo/issues/2536
2016-10-10 13:40:38 +02:00
Bjørn Erik Pedersen
54b81535af Add BenchmarkGet 2016-10-10 11:47:45 +02:00
Albert
21ea37b673 Fix space between Go projects and badges 2016-10-09 21:34:00 +02:00
Albert
b61deff907 Add GoDoc badge 2016-10-09 20:33:40 +02:00
Benoît Masson
ec4eb2fa85 Nested maps (#195)
Fixes #71, #93, #158, #168, #209, #141, #160, #162, #190

* Fixed: indentation in comment
* Fixed: Get() returns nil when nested element not found
* Fixed: insensitiviseMaps() made recursive so that nested keys are lowercased
* Fixed: order of expected<=>actual in assert.Equal() statements
* Fixed: find() looks into "overrides" first
* Fixed: TestBindPFlags() to use a new Viper instance
* Fixed: removed extra aliases from display in Debug()
* Added: test for checking precedence of dot-containing keys.
* Fixed: Set() and SetDefault() insert nested values
* Added: tests for overriding nested values
* Changed: AllKeys() includes all keys / AllSettings() includes overridden nested values
* Added: test for shadowed nested key
* Fixed: properties parsing generates nested maps
* Fixed: Get() and IsSet() work correctly on nested values
* Changed: modifier README.md to reflect changes
2016-10-08 10:00:18 +02:00
Cameron Moore
670c42a85b Check for nil in viper.Sub
Fixes #191
2016-10-06 01:22:39 +02:00
Bjørn Erik Pedersen
382f87b929 Remove expensive TRACE logging
Also avoid doing a strings.Split in the Get common case.

Any TRACE statements in these hot paths must be totally turned off when not testing.

```
benchmark                     old ns/op     new ns/op     delta
BenchmarkGetBool-4            4090          409           -90.00%
BenchmarkGetBoolFromMap-4     6.33          6.28          -0.79%

benchmark                     old allocs     new allocs     delta
BenchmarkGetBool-4            6              3              -50.00%
BenchmarkGetBoolFromMap-4     0              0              +0.00%

benchmark                     old bytes     new bytes     delta
BenchmarkGetBool-4            129           33            -74.42%
BenchmarkGetBoolFromMap-4     0             0             +0.00%
```

Fixes #242
2016-09-26 17:04:02 +02:00
Bjørn Erik Pedersen
438cc0d5c8 Add benchmark for GetBool
```
BenchmarkGetBool-4                100000         12952 ns/op         225 B/op          10 allocs/op
BenchmarkGetBoolFromMap-4       100000000            10.8 ns/op           0 B/op           0 allocs/op
```
2016-09-26 10:07:35 +02:00
Yauhen Lazurkin
e26d6aedc1 Support time.Duration in viper.Unmarshal (#205)
* Fixes #105
2016-09-24 01:20:44 +02:00
Cameron Moore
ed0a9674c6 Fix retrieval of pflag stringSlice (#240)
Fixes #112
Changes some logging directives to use Printf
2016-09-22 20:19:24 +02:00
Albert
a78f70b5b9 Small refactorings (#230)
* Fix typo in description of UnmarshalExact
* Omit 2nd values from range loops
* Delete findCWD method from util (was unused)
* Edit documentation according to golint
* Fix documentation in util
* Use RemoteProvider interface instead of defaultRemoteProvider
* Fix err variable in BindFlagValues
2016-09-20 10:17:41 +02:00
Max Wolter
2f6a41490b Prevent shadowning of keys when a nested key's value is nil. 2016-09-19 19:37:56 +02:00
Albert
16990631d4 Fix typo in README (#227)
Fixed typo & superfluous white space in README
2016-08-30 16:32:46 +02:00
Vladimir Vivien
7fb2782df3 Handle TOML Library Licensing
This patch updates the package used for parsing TOML content from
"github.com/BurntSushi/toml" to "github.com/pelletier/go-toml" as the
latter uses a more accepted OSS license (MIT), enabling the inclusion of
Viper or projects that depend on Viper in projects that have licensing
requirements incongruent with the license of the previous TOML package.

Closes #228
Closes #225
Fixes #179
2016-08-20 20:00:39 +01:00
Schley Andrew Kutz
fe9c8b59e1 Add Hugo CI Step to Validate Viper
Since Hugo is such a heavy user of Viper, this patch adds an
after_success section to the Travis-CI build that validates the Viper
commit by attempting to build Hugo and executing `hugo -s docs`.

This patch handles issue #222.
2016-08-20 14:03:44 +01:00
Bjørn Erik Pedersen
654fc7bb54 Bump Travis to Go 1.7 2016-08-16 10:09:34 +02:00
Bjørn Erik Pedersen
346299ea79 Add getter for global Viper 2016-08-06 18:06:49 +02:00
Bjørn Erik Pedersen
abafbf243b Revert "Handle TOML Library Licensing"
It breaks Hugo, will have to test/investigate.

Erro when building Hugo docs:
```
ERROR: 2016/08/05 11:19:23 site.go:1208: unable to process menus in site config
ERROR: 2016/08/05 11:19:23 site.go:1209: Unable to Cast map[string]interface {}{"pre":"<i class='fa fa-space-shuttle'></i>", "weight":-20, "url":"/commands/", "name":"Hugo Cmd Reference", "identifier":"commands"} of type map[string]interface {} to []interface{}
```

This reverts commit 7402856f06.
2016-08-05 11:21:53 +02:00
Bjørn Erik Pedersen
a59dcccc82 Move the Afero fs to the Viper type
And make a exported setter for it.
2016-08-05 09:45:58 +02:00
Albert
d0c2644870 Add dot in BindPFlag comment 2016-08-05 09:25:24 +02:00
Matthieu Grieger
4cf0bd2789 Add support for Afero filesystems 2016-08-05 09:24:49 +02:00
Roland Schilter
5619c0edbe Reset cache on config name change
I stumbled over this when trying to merge multiple configs.

```
viper.SetConfigName("default")
err := viper.MergeInConfig()
```
which caches file path resolvemenet in `v.configFile`

and then
```
viper.SetConfigName("prod")
err := viper.MergeInConfig()
```

which reuses `v.configFile` without updating it accordingly to the new name.

See c1ccc378a0/viper.go (L1240)
2016-08-05 09:18:19 +02:00
Chuanjian Wang
64dc6f6810 Add GetInt64 2016-08-05 09:16:55 +02:00