Compare commits

..
Author SHA1 Message Date
Mark Sagi-Kazar ca55de9785 fix: Unmarshal struct decoding when passing in a pointer to a pointer
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
2023-12-08 18:59:35 +01:00
dependabot[bot]andMárk Sági-Kazár 9154b900c3 build(deps): bump actions/setup-go from 4.1.0 to 5.0.0
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 4.1.0 to 5.0.0.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](https://github.com/actions/setup-go/compare/93397bea11091df50f3d7e59dc26a7711a8bcfbe...0c52d547c9bc32b1aa3301fd7a9cb496313a4491)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-08 14:48:22 +01:00
dependabot[bot]andMárk Sági-Kazár 08e4a00949 build(deps): bump github/codeql-action from 2.22.8 to 2.22.9
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 2.22.8 to 2.22.9.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/github/codeql-action/compare/407ffafae6a767df3e0230c3df91b6443ae8df75...c0d1daa7f7e14667747d73a7dbbe8c074bc8bfe2)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-08 14:48:15 +01:00
Filip KrakowskiandMárk Sági-Kazár fb6eb1e8e9 fix: merge missing struct keys inside UnmarshalExact 2023-12-08 14:44:05 +01:00
4 changed files with 89 additions and 7 deletions
+3 -3
View File
@@ -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'
+3 -3
View File
@@ -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
+15 -1
View File
@@ -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
+68
View File
@@ -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) {