Commit graph

94 commits

Author SHA1 Message Date
inkychris
99520c81d8 Implemented ability to unmarshal keys containing dots to structs.
Changed formatting of test objects for better git diffing and readibility.
Fixed failing tests on Windows.
2019-09-11 16:03:08 +02:00
Christian Muehlhaeuser
33bf76add3 Simplify code
Removed unnecessary conversions.
2019-07-29 18:37:00 +02:00
Christian Muehlhaeuser
1b33e8258e Add error handling
Added error handling around ineffectual err assignments. Please review
thoroughly.
2019-07-28 14:50:13 +02:00
CodeLingo Bot
275a36d0a0 Fix function comments based on best practices from Effective Go
Signed-off-by: CodeLingo Bot <bot@codelingo.io>
2019-07-13 12:11:16 +02:00
AGirard
b8221cf4ee Add GetIntSlice helper method 2019-07-13 12:04:36 +02:00
Márk Sági-Kazár
ad5ed02fa4 Add support for int slice flags (#637)
* Add support for int slice flags

* Add int slice test to unmarshal
2019-06-11 16:51:57 -04:00
mexisme
3620d3d9e1 Support .env format files (#528)
* Support `.env` format files

* Missing "dotenv" from SupportedExtns
2019-06-11 16:47:43 -04:00
Anthony Fok
2bd2732789
Use GetInt64 for largenum in viper_test.go
to prevent overflow error on 32-bit platforms.

Fixes #704
2019-05-24 11:53:49 -06:00
Anthony Fok
93066f92c6
Fix typo in viper_test.go: lagrenum → largenum 2019-05-24 07:40:12 -06:00
Mitch Connors
7a605a50e6 Uint Support (#681)
* add GetUint/GetUint32/GetUint64

* Add Get(string) support for uint.
2019-04-08 10:06:45 -04:00
Bjørn Erik Pedersen
6d33b5a963
Make the map in MergeConfigMap case insensitive 2018-12-07 11:03:36 +01:00
Bjørn Erik Pedersen
41cd1c3aa3 Restrict Travis to >= Go 1.11, use Go Modules, and get the test to pass 2018-12-05 17:13:47 +01:00
Bjørn Erik Pedersen
3535c75fa8 Add MergeConfigMap
Fixes #605
2018-12-05 17:13:47 +01:00
Bjørn Erik Pedersen
06c7c0d9b3
Use assert.EqualValues for slice checking 2018-11-19 10:38:40 +01:00
Benoit Masson
ae103d7e59 Moved shared resources out of the loops in TestBindPFlagsStringSlice()
Common code and resources put out of the loops, to improve
efficiency and readability.
2018-11-07 12:08:59 +01:00
Benoit Masson
69647fb422 Fixed TestBindPFlagsStringSlice()
Replaced Visit() by VisitAll(), so that the Changed attribute of
the updated flag is correctly set.
2018-11-07 12:08:59 +01:00
Benoit Masson
cc7e906d88 Updated TestBindPFlagsStringSlice() to highlight a failure
When pflag marked as changed, the value is not detected
(and lower priority value used)
2018-11-07 12:08:59 +01:00
Márk Sági-Kazár
b7a3b95476 Lookup environment variables instead of checking if the value is empty
This commit adds an `AllowEmptyEnv` option that, default off, that when set will allow set, but empty, environment variables

Fixes #317
2018-11-06 22:53:21 +01:00
Andrew Stuart
8e194e8ad2
Merge branch 'master' into mergefix/Issue284_Kubernetes_config 2018-09-28 01:05:04 -07:00
Andrew Stuart
0d783e7344
Use test log 2018-09-28 01:04:19 -07:00
Dr. Tobias Quathamer
0ac2068de9 Fix overflow error on 32 bit architectures (#340)
* Handle int64 separately for 32 bit architectures

* Remove tests which result in an overflow error on 32 bit architectures
2018-09-01 14:59:01 -06:00
Xavier Coulon
41f829b2c9 refactor test to avoid negative counter on WG
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
2018-08-06 09:29:13 +02:00
Xavier Coulon
e12d3d32d1 Merge branch 'master' into Issue284_Kubernetes_config 2018-08-06 08:18:53 +02:00
Chris Reeves
907c19d40d Support customising mapstructure.DecoderConfig for Unmarshal
* Added a new `DecoderConfigOption` type allowing the user to write custom
  functions that can override the default mapstructure.DecoderConfig
  settings

* Added a new `DecodeHook` function which returns
  a `DecoderConfigOption`. This allows the user to easily set their own
  Decode hooks when Unmarshaling

* Updated Unmarshal, UnmarshalKey and defaultDecoderConfig to support variadic
  trailing `DecoderConfigOption` functions to allow for customisation of
  the default  mapstructure.DecoderConfig

* Added a test case with example usage
2018-08-03 14:57:45 +02:00
Xavier Coulon
c1250e5dd7 apply review comments
use masks for checking the events.

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
2018-07-13 10:30:23 +02:00
Xavier Coulon
242f4890f5 Refactor with WaitGroup and check channel is open
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
2018-05-24 10:09:29 +02:00
Travis Jeffery
15738813a0 Add GetInt32 2018-05-07 09:10:07 +02:00
Bjørn Erik Pedersen
b5e8006cbe
Undexport GetConfigFile
It was exported in previous commit, but we have GetConfigFileUsed -- so use that.
2018-03-19 19:50:19 +01:00
Bjørn Erik Pedersen
00ed41cdba
Export and fix GetConfigFile 2018-03-19 19:12:24 +01:00
Xavier Coulon
e0f7631cf3 WatchConfig and Kubernetes (#284)
Support override of symlink to config file
Include tests for WatchConfig of regular files, as well
as config file which links to a folder which is itself a
link to another folder in the same "watch dir" (the way
Kubernetes exposes config files from ConfigMaps mounted
on a volume in a Pod)

Also:
- Add synchronization with WaitGroup to ensure that the WatchConfig
is properly started before returning
- Remove the watcher when the Config file is removed.

Fixes #284

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
2018-01-03 10:37:18 +01:00
Adam Sherwood
1a0c4a370c Feature/write config (#287)
* Added method to write into TOML file.

* Added functionality to export configuration based on config type. The feature supports JSON and TOML.

* Added method to write into YAML file.

* Fixed the issue of incorrect defer and error checking order. The error checking must be first otherwise it will cause panic.

* Add WriteConfig methods

* Add support for toml

* Add shared write function and safe methods

* Fix incorrectly modified imports

* Remove extra comments

* Fix spelling

* Make marshal spelling consistent throughout

* Add support for remaining configuration types

This commit moves a significant portion of the code back to viper.go to
facilitate having access to the object when reading the files. The purpose is to
add properties to the viper object at read time, so that we can add the comments
back to the file when writing.

* Add tests for each written file type

* Modify test for updated HCL specification

* Modify to only support HCL write in Go 1.7

* Revert "Modify to only support HCL write in Go 1.7"

This reverts commit 12b34bc4eb92cbf8ebfd56b79519f448607e3e51.

* Need to truncate the file before writing

* Write all settings including overrides

* Use filename variable

* Lint remote.go

* Fix toml return count error
2017-12-06 23:26:31 -05:00
Kamil Wargula
8ef37cbca7 Fix incorrect name of function in README.md
Change `SetEnvReplacer` to `SetEnvKeyReplacer`
2017-10-20 12:40:09 +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
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
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
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
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
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
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