mirror of
https://github.com/spf13/viper.git
synced 2024-04-21 12:31:38 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca55de9785 | ||
|
|
9154b900c3 | ||
|
|
08e4a00949 | ||
|
|
fb6eb1e8e9 |
@@ -24,7 +24,7 @@ jobs:
|
||||
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
|
||||
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
|
||||
with:
|
||||
go-version: '1.21'
|
||||
|
||||
@@ -52,7 +52,7 @@ jobs:
|
||||
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
|
||||
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
|
||||
@@ -73,7 +73,7 @@ jobs:
|
||||
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
|
||||
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
|
||||
with:
|
||||
go-version: '1.21'
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ jobs:
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@407ffafae6a767df3e0230c3df91b6443ae8df75 # v2.22.8
|
||||
uses: github/codeql-action/init@c0d1daa7f7e14667747d73a7dbbe8c074bc8bfe2 # v2.22.9
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
@@ -54,7 +54,7 @@ jobs:
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@407ffafae6a767df3e0230c3df91b6443ae8df75 # v2.22.8
|
||||
uses: github/codeql-action/autobuild@c0d1daa7f7e14667747d73a7dbbe8c074bc8bfe2 # v2.22.9
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
@@ -68,5 +68,5 @@ jobs:
|
||||
# make release
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@407ffafae6a767df3e0230c3df91b6443ae8df75 # v2.22.8
|
||||
uses: github/codeql-action/analyze@c0d1daa7f7e14667747d73a7dbbe8c074bc8bfe2 # v2.22.9
|
||||
|
||||
|
||||
@@ -1127,6 +1127,13 @@ func (v *Viper) Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
|
||||
func (v *Viper) decodeStructKeys(input any, opts ...DecoderConfigOption) ([]string, error) {
|
||||
var structKeyMap map[string]any
|
||||
|
||||
rv := reflect.ValueOf(input)
|
||||
for rv.Kind() == reflect.Ptr {
|
||||
rv = reflect.Indirect(rv)
|
||||
}
|
||||
|
||||
input = rv.Interface()
|
||||
|
||||
err := decode(input, defaultDecoderConfig(&structKeyMap, opts...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1179,7 +1186,14 @@ func (v *Viper) UnmarshalExact(rawVal any, opts ...DecoderConfigOption) error {
|
||||
config := defaultDecoderConfig(rawVal, opts...)
|
||||
config.ErrorUnused = true
|
||||
|
||||
return decode(v.AllSettings(), config)
|
||||
// TODO: make this optional?
|
||||
structKeys, err := v.decodeStructKeys(rawVal, opts...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: struct keys should be enough?
|
||||
return decode(v.getSettings(append(v.AllKeys(), structKeys...)), config)
|
||||
}
|
||||
|
||||
// BindPFlags binds a full flag set to the configuration, using each flag's long
|
||||
|
||||
@@ -1052,6 +1052,74 @@ func TestUnmarshalWithAutomaticEnv(t *testing.T) {
|
||||
|
||||
assert.Error(t, err, "expected viper.Unmarshal to return error due to unset field 'FLAG'")
|
||||
})
|
||||
|
||||
t.Run("Exact", func(t *testing.T) {
|
||||
var config Configuration
|
||||
|
||||
v.Set("port", 1234)
|
||||
if err := v.UnmarshalExact(&config); err != nil {
|
||||
t.Fatalf("unable to decode into struct, %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(
|
||||
t,
|
||||
Configuration{
|
||||
Name: "Steve",
|
||||
Port: 1234,
|
||||
Duration: time.Second + time.Millisecond,
|
||||
Modes: []int{1, 2, 3},
|
||||
Authentication: AuthConfig{
|
||||
Secret: "42",
|
||||
},
|
||||
Storage: StorageConfig{
|
||||
Size: 4096,
|
||||
},
|
||||
},
|
||||
config,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUnmarshalIndirection(t *testing.T) {
|
||||
v := New()
|
||||
|
||||
v.Set("foo", "bar")
|
||||
|
||||
type config struct {
|
||||
Foo string
|
||||
}
|
||||
|
||||
var C config
|
||||
|
||||
CC := &C
|
||||
CCC := &CC
|
||||
|
||||
err := v.Unmarshal(&CCC)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, config{"bar"}, C)
|
||||
}
|
||||
|
||||
func TestUnmarshalInterface(t *testing.T) {
|
||||
v := New()
|
||||
|
||||
v.Set("foo", "bar")
|
||||
|
||||
type someInterface interface {
|
||||
Foo() string
|
||||
}
|
||||
|
||||
type config struct {
|
||||
Foo string
|
||||
Fooer someInterface
|
||||
}
|
||||
|
||||
var C config
|
||||
|
||||
err := v.Unmarshal(&C)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, config{Foo: "bar"}, C)
|
||||
}
|
||||
|
||||
func TestBindPFlags(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user