Commit Graph

160 Commits

Author SHA1 Message Date
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
dsp c975dc1b4e implementing a weak decode wrapper called UnmarshalExact that errors on non existant fields in the destination struct 2016-02-09 16:07:50 -07:00
Marcin Stanisławski e072d51737 use aliases in all keys list to enable proper Unmarshaling 2016-02-08 15:52:20 -05:00
Lei Feng 0c82789feb Handle the case Get() returns either map[interface{}]interface{} or map[string]interface{} 2015-12-26 09:08:49 +08:00
akutz 991d18afb2 Adds MergeConfig functionality
This patch adds the `MergeConfig` and `MergeInConfig` functions to
enable reading new configuration files via a merge strategy rather
than replace. For example, take the following as the base YAML for a
configuration:

    hello:
        pop: 37890
        world:
        - us
        - uk
        - fr
        - de

Now imagine we want to read the following, new configuration data:

    hello:
        pop: 45000
        universe:
        - mw
        - ad
    fu: bar

Using the standard `ReadConfig` function the value returned by the
nested key `hello.world` would no longer be present after the second
configuration is read. This is because the `ReadConfig` function and
its relatives replace nested structures entirely.

The new `MergeConfig` function would produce the following config
after the second YAML snippet was merged with the first:

    hello:
        pop: 45000
        world:
        - us
        - uk
        - fr
        - de
        universe:
        - mw
        - ad
    fu: bar

Examples showing how this works can be found in the two unit tests
named `TestMergeConfig` and `TestMergeConfigNoMerge`.
2015-12-24 10:24:07 -05:00
Lei Feng 105e3d0d19 Add Sub() for Viper, which returns a branch of a Viper instance. 2015-12-24 22:44:14 +08:00
patdhlk 8e6f2421dc add test structure for hcl 2015-12-15 14:44:21 -05:00
patdhlk 60e1b5f599 changed the test hcl config 2015-12-15 14:44:21 -05:00
patdhlk 606a4f3933 add support for hcl 2015-12-15 14:44:21 -05:00
akutz e3bc06f20c Refactored IsSet to examine keys
This patch refactors the IsSet function to examine the keys in order
to see if a key is set instead of simply checking if a value is nil.
This change is necessary due to the fact that default values via
flag bindings will result in the old logic always being true for
the IsSet function due to a type's default value such as 0 for an
integer or an empty string for a string. While a type's default
value may be preferable when getting the value for a key, it
results in a false positive when determining if a key is actually
set. This change enables users to detect whether a key is set by
only returning a flag's value if it has changed.
2015-12-11 16:17:57 -05:00
James Sweet 87b94ba486 Fixed #115: Added code in the find method to search for nested configuration parameters 2015-11-04 19:35:10 -05:00
Matt Surabian 1967d93db7 Fixed #36: Changed Marshal to Unmarshal throughout. 2015-09-08 08:24:57 -04:00
Vlad Didenko 9fca10189b Fixed #73 2015-08-17 00:11:40 -05:00
Dotan Nahum 28ada1e5b0 Nested keys properly recurse on map[interface{}]interface{} 2015-06-21 18:51:43 -04:00
oliveagle f3482afcd0 replace bytes.Buffer with io.Reader 2015-05-19 10:07:21 -04:00
oliveagle 3492885e84 ReadBufConfig 2015-05-19 10:07:21 -04:00
bep be782f3fee Revert "Recursively insensitivize the configuration structures"
This reverts commit 8d9577a72e.

The commit is reasonable enough, but this is a major breaking change for Hugo.

We have to figure out how to handle this before we introduce this one.

See https://github.com/spf13/hugo/issues/1129
2015-05-11 23:36:48 +02:00
Kiril Zvezdarov 8d9577a72e Recursively insensitivize the configuration structures 2015-05-01 22:52:16 -04:00
Kiril Zvezdarov 47b5435941 Added test coverage for searching for deeply nested values 2015-05-01 22:52:16 -04:00
Wayne Walker ba3382dd23 59 - add properties file support to viper 2015-04-14 13:15:02 -05:00
Kiril Zvezdarov 24dd877ad7 Added BindPFlags function which binds all flags in a given flag set to the pflags register 2015-04-02 17:04:19 -04:00
Daniel Eloff e133904c4f Add GetSizeInBytes.
Useful to parse strings like 1GB or 12 mb into an unsigned integer number of bytes.
2015-03-12 22:19:13 -04:00
Chance Zibolski 03fb74b5d7 Support rewriting env keys 2015-03-06 11:21:17 -08:00
Ross Cooperman 90734830d1 Move viper.Reset() back to the public interface
It is helpful for applications that use viper to be able to
reset their configurations between test runs.
2015-02-24 11:54:15 -05:00
spf13 6c5a915341 adding more env tests 2015-02-16 23:32:10 -05:00
spf13 1022d75c73 Adding Support for Environment variable prefixes 2014-12-22 18:31:11 -05:00
spf13 91b076eec5 MarshallReader -> marshalReader 2014-12-06 09:48:28 +01:00
spf13 18a87c05c6 Adding documentation inline. Moving Reset() to viper_test.go 2014-12-05 17:04:40 +01:00
spf13 29f1858f87 Viper now supports multiple vipers. No API changes. 2014-12-05 03:55:51 +01:00
Brian Ketelsen 0562ef4c8b merge upstream/master 2014-10-28 22:09:30 -04:00
Brian Ketelsen 51da30f655 Added basic documentation, pointers to crypt repository, and tests for precedence 2014-10-27 15:32:46 -04:00
Brian Ketelsen f8939d9229 existing tests pass 2014-10-26 09:48:21 -04:00
Chris Hamant 1b8adf4854 fixing problem with case sensitivity with keys in env and flags maps 2014-10-09 16:39:24 -04:00
Chris Hamant 01b1780288 adding simple test for BindPFlag 2014-10-09 15:52:49 -04:00
spf13 83fd92627c Adding automatic reading from ENV w/tests 2014-09-27 14:05:01 -07:00
spf13 aacc3049e2 Adding AllKeys and AllSettings functions and tests 2014-09-27 14:05:01 -07:00
spf13 181a3b5f3b Adding support for ENV variables 2014-09-27 14:04:50 -07:00
Nate Finch 2b24bea958 fix issue #8
This fixes the aliases in config files bug.  Whenever we register an alias, if there is a value in
the config (or defaults or override) for the alias, we move that value to the new "real key".

Added a test for the bug, which fails without the changes and passes with the changes.

This also fixes a bug in Hugo, where specifying "Taxonomies" in your config file doesn't get recognized,
because Hugo aliases "Taxonomies" to "Indexes" which means that when the code does a Get("Taxnomies") it
got translated to Get("Indexes"), which didn't exist in the original config map.
2014-08-06 00:27:39 -04:00
Nate Finch 3cf05f93ef fix test after function rename 2014-08-06 00:24:36 -04:00
spf13 af373af72c Adding the ability to get into a struct 2014-06-26 17:58:55 -04:00
spf13 541c1f8c59 Fixing case sensitivity bug with override values 2014-05-29 16:48:24 -04:00
Victor Castell 16d5c7c27d Add support for yml extension 2014-05-09 23:28:57 -04:00
spf13 25817ada59 Added Reset Function 2014-04-07 23:35:53 -04:00
spf13 bcb02e2472 Viper is now case insensitive 2014-04-05 01:19:39 -04:00
spf13 98be0718d2 Initial Version 2014-04-04 17:21:59 -04:00