Commit Graph

211 Commits

Author SHA1 Message Date
Rodrigo Chiossi cdccc8152c Fix SafeWriteConfig
If the config file does not exist and the force flag is not set,
OpenFile would not use O_CREATE flag, causing viper to fail with
error "File not exist" and to not create the config file.
This patch changes the behavior of writeConfig() so that if force is set
to false, OpenFile will use O_EXCL flag, thus failing if the file
already exists or creating a new file otherwise.

Signed-off-by: Rodrigo Chiossi <rodrigo.chiossi@intel.com>
2019-07-28 12:33:29 +02:00
bpizzi e02bc9eca5 Fixed missing f.Close() in writeConfig()
Defering can cause trouble because we're writing to the file outside the function
where the defering is registered, calling f.Sync() ensures that bytes are flushed
to disk even is Close() is called too soon.
2019-07-13 12:28:02 +02:00
Nicolas Martin e325492b82 Add missing call to initWF.Done() 2019-07-13 12:13:30 +02:00
Márk Sági-Kazár e6d1c6bc9a Improve godoc 2019-07-13 12:11:16 +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
Márk Sági-Kazár 7fdad0204e Fix typo 2019-07-13 12:04:36 +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
Mitch Connors 7a605a50e6 Uint Support (#681)
* add GetUint/GetUint32/GetUint64

* Add Get(string) support for uint.
2019-04-08 10:06:45 -04:00
Kris fccfc2c271 Add API for setting file permissions 2019-04-04 14:02:01 +02:00
Bjørn Erik Pedersen 9e56dacc08 Remove superflous insensitiviseMaps in Unmarshal methods
Fixes #482
2019-03-13 10:29:51 +01: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 3535c75fa8 Add MergeConfigMap
Fixes #605
2018-12-05 17:13:47 +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 62edee3196
Revert "Cater for case-sensitive dependencies (#463)"
This reverts commit b56071875a.
2018-09-29 21:41:27 -07:00
Panagiotis Moustafellos b56071875a Cater for case-sensitive dependencies (#463)
Switching jww from jwalterweatherman to jWalterWeatherman for cases when vgo is broken
2018-09-29 20:32:38 -07:00
Andrew Stuart 8e194e8ad2
Merge branch 'master' into mergefix/Issue284_Kubernetes_config 2018-09-28 01:05:04 -07:00
Andrew Stuart 2c12c60302
Fix nil pointer on watch function (#568) 2018-09-28 00:53:21 -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
Aarti Parikh 05116ad639 Revert "fix dep wrong case (#484)"
This reverts commit b7a62b2c00.
2018-08-28 02:34:36 -06:00
kun b7a62b2c00 fix dep wrong case (#484) 2018-08-28 01:37:55 -06:00
Robin Brämer e436d04e6d correct a comment on viper.Set() (#553)
correct regiser with register
2018-08-28 01:29:26 -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
Jeff Lindsay 4dddf7c62e Allow exists util function to take afero.Fs so it can be used with non-deafult instances of Viper (#405)
Signed-off-by: Jeff Lindsay <progrium@gmail.com>
2017-11-09 21:57:16 +01: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
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
Nikola Kovacs 9766537120 Fix grammar/typo in comment for getEnv()
See #310
2017-07-22 23:09:41 -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
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
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
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
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
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
Bjørn Erik Pedersen 346299ea79 Add getter for global Viper 2016-08-06 18:06:49 +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
Bjørn Erik Pedersen a0cdbddebd Remove kr/pretty dependency
See https://github.com/spf13/hugo/issues/2124
See https://github.com/kr/text/issues/6
2016-05-08 13:12:02 +02:00
Anthony Fok 8e57fea7a8 Update import path of fsnotify
Rename "gopkg.in/fsnotify.v1" to "github.com/fsnotify/fsnotify"
per upstream recommendation.

See https://github.com/fsnotify/fsnotify/issues/108 for rationale.
2016-04-20 21:51:22 +08:00
Tony Narlock 45b73b72ad Fix typo 2016-04-20 21:43:24 +08: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
Bjørn Erik Pedersen a212099cbe Watch the entire config dir for changes
Then checking the file name in the event handler. This seems to be the only robust way
of handling changes from a single file on multiple platforms and editors.

See #142
2016-01-11 16:07:23 +01:00
Bjørn Erik Pedersen cc70319ebc Fix config watch
* Only add *the* config file, not all possible folders
* Trigger reload on both write and create events;
  the latter is what we get from atomic save editors (like TextMate) once https://github.com/go-fsnotify/fsnotify/pull/111 is merged

See #142
2016-01-11 13:34:45 +01: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 110492b300 Bugfix for Nested Key Casing
This patch fixes a bug with how Viper handle's key casing when keys are
nested. While Viper is generally case-insensitive, this was not the case
with regards to nested keys. This patch makes nested keys insensitive as
well.
2015-12-24 10:25:30 -05: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
David Calavera 66249a6550 Add FlagValue interface to support other flag systems.
Using an interface allows people to use their favourite flag system
with viper without being restricted to the semantics of pflag or the
standard library.

This change introduce two new functions `BindFlagValues` and
`BindFlagValue` that behave like `BindFlags` and `BindFlag` but using
the new interface as values.

This change also introduces two internal structures to transform
`*pflag.FlagSet` and `*pflag.Flag` into the new interface. This way,
viper keeps working as expected for people that are currently using the
pflag package without breaking backwards compatibility.

Signed-off-by: David Calavera <david.calavera@gmail.com>
2015-12-24 10:16:41 -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 606a4f3933 add support for hcl 2015-12-15 14:44:21 -05:00
HuKeping 87d443c19b Fix tab issue.
As the title said.
2015-12-15 12:54:05 -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
Jonathan Boulle ce08532bfd Correct "etcd" naming
The project should always be referred to in lowercase.
2015-12-11 16:14:08 -05:00
akutz c25387f10d AllKeys() now includes bound flag and env var keys
This patch fixes a bug where the function `AllKeys()` did not include
the keys for bound flags and environment variables.
2015-11-12 14:28:38 -06:00
spf13 e37b56e207 Add dynamic reading of config file support 2015-11-09 23:22:04 -05:00
akutz c374c6d0a9 Flag Binding Refactor
This patch alters the way flags are handled to coincide with the
documentation on the Viper README. The documentation indicated that flag
bindings were late, when in fact they were very, very early. This patch
changes flag bindings to behave as late bindings.
2015-11-09 19:24:13 -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
akutz 0a4a93b685 [110] Default Values Specify Type
This patch adds a feature, if enabled, will infer a value's type from
its default value no matter from where else the value is set. This is
particularly important when working with environment variables. For
example:

    package main

    import (
      "fmt"
      "os"

      "github.com/spf13/viper"
    )

    func print(name string, val interface{}) {
      fmt.Printf("%-15[1]s%-15[2]T%[2]v\n", name, val)
    }

    func main() {
      viper.BindEnv("mykey", "MYPREFIX_MYKEY")
      viper.SetDefault("mykey", []string{})
      os.Setenv("MYPREFIX_MYKEY", "a b c")

      v1 := viper.GetStringSlice("mykey")
      v2 := viper.Get("mykey")

      print("v1", v1)
      print("v2", v2)
    }

When this program is executed the following is emitted:

    [0]akutz@pax:ex$ ./ex1
    v1             []string       [a b c]
    v2             string         a b c
    [0]akutz@pax:ex$

You may wonder, why is this important? Just use the GetStringSlice
function. Well, it *becomes* important when dealing with marshaling.
If we update the above program to this:

    package main

    import (
      "fmt"
      "os"

      "github.com/spf13/viper"
    )

    type Data struct {
      MyKey []string
    }

    func print(name string, val interface{}) {
      fmt.Printf("%-15[1]s%-15[2]T%[2]v\n", name, val)
    }

    func main() {
      viper.BindEnv("mykey", "MYPREFIX_MYKEY")
      viper.SetDefault("mykey", []string{})
      os.Setenv("MYPREFIX_MYKEY", "a b c")

      v1 := viper.GetStringSlice("mykey")
      v2 := viper.Get("mykey")

      print("v1", v1)
      print("v2", v2)

      d := &Data{}
      viper.Marshal(d)
      print("d.MyKey", d.MyKey)
    }

Now we can see the issue when we execute the updated program:

    [0]akutz@pax:ex$ ./ex2
    v1             []string       [a b c]
    v2             string         a b c
    d.MyKey        []string       []
    [0]akutz@pax:ex$

The marshalled data structure's field MyKey is empty when in fact it
should have a string slice equal to, in value, []string {"a", "b",
"c"}.

The problem is that viper's Marshal function calls AllSettings which
ultimately uses the Get function. The Get function does try to infer
the value's type, but it does so using the type of the value retrieved
using this logic:

    Get has the behavior of returning the value associated with the
    first place from where it is set. Viper will check in the
    following order:

      * override
      * flag
      * env
      * config file
      * key/value store
      * default

While the above order is the one we want when retrieving the values,
this patch enables users to decide if it's the order they want to be
used when inferring a value's type. To that end the function
SetTypeByDefaultValue is introduced. When SetTypeByDefaultValue(true)
is called, a call to the Get function will now first check a key's
default value, if set, when inferring a value's type. This is
demonstrated using a modified version of the same program above:

    package main

    import (
      "fmt"
      "os"

      "github.com/spf13/viper"
    )

    type Data struct {
      MyKey []string
    }

    func print(name string, val interface{}) {
      fmt.Printf("%-15[1]s%-15[2]T%[2]v\n", name, val)
    }

    func main() {
      viper.BindEnv("mykey", "MYPREFIX_MYKEY")
      viper.SetDefault("mykey", []string{})
      os.Setenv("MYPREFIX_MYKEY", "a b c")

      v1 := viper.GetStringSlice("mykey")
      v2 := viper.Get("mykey")

      print("v1", v1)
      print("v2", v2)

      d1 := &Data{}
      viper.Marshal(d1)
      print("d1.MyKey", d1.MyKey)

      viper.SetTypeByDefaultValue(true)

      d2 := &Data{}
      viper.Marshal(d2)
      print("d2.MyKey", d2.MyKey)
    }

Now the following is emitted:

    [0]akutz@pax:ex$ ./ex3
    v1             []string       [a b c]
    v2             string         a b c
    d1.MyKey       []string       []
    d2.MyKey       []string       [a b c]
    [0]akutz@pax:ex$
2015-09-08 08:23:06 -04:00
jackspirou 3c0ff861e3 running tests again 2015-08-26 08:50:40 -04:00
jackspirou 09ba0a6954 fixing second slice type needed 2015-08-26 08:50:40 -04:00
jackspirou d028fd65ba changing import statements back 2015-08-26 08:50:40 -04:00
jackspirou cc1c9a82a5 typo: slice type needed 2015-08-26 08:50:40 -04:00
jackspirou 0a12778a8c using my own version of github.com/spf13/cast for now 2015-08-26 08:50:40 -04:00
jackspirou b9316c3299 adding GetStringMapStringSlice method 2015-08-26 08:50:40 -04:00
Vlad Didenko 9fca10189b Fixed #73 2015-08-17 00:11:40 -05:00
Vlad Didenko fa137328f6 Fixed #68 2015-08-17 00:09:59 -05:00
Vlad Didenko f14e1baa25 Fixes #83 2015-07-30 10:35:06 -07:00
Kiril Zvezdarov db7ff930a1 AddSecureRemoteProvider adds the secret keyring to the default provider struct 2015-06-21 19:19:00 -04:00
Kiril Zvezdarov 8e930a9714 Revert "The AddSecureRemoteProvider function didn't add secretKeyring to the remoteProvider struct."
This reverts commit ca00a9b4f7.
2015-06-21 19:13:01 -04:00
chalupaul ca00a9b4f7 The AddSecureRemoteProvider function didn't add secretKeyring to the remoteProvider struct. 2015-06-21 19:09:06 -04:00