Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab3a50c

Browse files
committedDec 18, 2023
fix!: hide struct binding behind a feature flag
Signed-off-by: Mark Sagi-Kazar <[email protected]>
1 parent 9154b90 commit ab3a50c

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed
 

‎.github/workflows/ci.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Set up Go
2727
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
2828
with:
29-
go-version: '1.21'
29+
go-version: "1.21"
3030

3131
- name: Build
3232
run: go build .
@@ -44,8 +44,8 @@ jobs:
4444
fail-fast: false
4545
matrix:
4646
os: [ubuntu-latest, macos-latest, windows-latest]
47-
go: ['1.19', '1.20', '1.21']
48-
tags: ['', 'finder']
47+
go: ["1.19", "1.20", "1.21"]
48+
tags: ["", "finder", "viper_bind_struct"]
4949

5050
steps:
5151
- name: Checkout repository
@@ -75,7 +75,7 @@ jobs:
7575
- name: Set up Go
7676
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
7777
with:
78-
go-version: '1.21'
78+
go-version: "1.21"
7979

8080
- name: Lint
8181
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0

‎internal/features/bind_struct.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build viper_bind_struct
2+
3+
package features
4+
5+
const BindStruct = true
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build !viper_bind_struct
2+
3+
package features
4+
5+
const BindStruct = false

‎viper.go

+23-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"github.com/spf13/viper/internal/encoding/json"
4949
"github.com/spf13/viper/internal/encoding/toml"
5050
"github.com/spf13/viper/internal/encoding/yaml"
51+
"github.com/spf13/viper/internal/features"
5152
)
5253

5354
// ConfigMarshalError happens when failing to marshal the configuration.
@@ -1114,14 +1115,20 @@ func Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
11141115
}
11151116

11161117
func (v *Viper) Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
1117-
// TODO: make this optional?
1118-
structKeys, err := v.decodeStructKeys(rawVal, opts...)
1119-
if err != nil {
1120-
return err
1118+
keys := v.AllKeys()
1119+
1120+
if features.BindStruct {
1121+
// TODO: make this optional?
1122+
structKeys, err := v.decodeStructKeys(rawVal, opts...)
1123+
if err != nil {
1124+
return err
1125+
}
1126+
1127+
keys = append(keys, structKeys...)
11211128
}
11221129

11231130
// TODO: struct keys should be enough?
1124-
return decode(v.getSettings(append(v.AllKeys(), structKeys...)), defaultDecoderConfig(rawVal, opts...))
1131+
return decode(v.getSettings(keys), defaultDecoderConfig(rawVal, opts...))
11251132
}
11261133

11271134
func (v *Viper) decodeStructKeys(input any, opts ...DecoderConfigOption) ([]string, error) {
@@ -1179,14 +1186,20 @@ func (v *Viper) UnmarshalExact(rawVal any, opts ...DecoderConfigOption) error {
11791186
config := defaultDecoderConfig(rawVal, opts...)
11801187
config.ErrorUnused = true
11811188

1182-
// TODO: make this optional?
1183-
structKeys, err := v.decodeStructKeys(rawVal, opts...)
1184-
if err != nil {
1185-
return err
1189+
keys := v.AllKeys()
1190+
1191+
if features.BindStruct {
1192+
// TODO: make this optional?
1193+
structKeys, err := v.decodeStructKeys(rawVal, opts...)
1194+
if err != nil {
1195+
return err
1196+
}
1197+
1198+
keys = append(keys, structKeys...)
11861199
}
11871200

11881201
// TODO: struct keys should be enough?
1189-
return decode(v.getSettings(append(v.AllKeys(), structKeys...)), config)
1202+
return decode(v.getSettings(keys), config)
11901203
}
11911204

11921205
// BindPFlags binds a full flag set to the configuration, using each flag's long

‎viper_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/stretchr/testify/assert"
2929
"github.com/stretchr/testify/require"
3030

31+
"github.com/spf13/viper/internal/features"
3132
"github.com/spf13/viper/internal/testutil"
3233
)
3334

@@ -956,6 +957,10 @@ func TestUnmarshalWithDecoderOptions(t *testing.T) {
956957
}
957958

958959
func TestUnmarshalWithAutomaticEnv(t *testing.T) {
960+
if !features.BindStruct {
961+
t.Skip("binding struct is not enabled")
962+
}
963+
959964
t.Setenv("PORT", "1313")
960965
t.Setenv("NAME", "Steve")
961966
t.Setenv("DURATION", "1s1ms")

0 commit comments

Comments
 (0)
Please sign in to comment.