diff --git a/x/config/cli/binder_test.go b/x/config/cli/binder_test.go index 2cfbb41886..8c20b8501b 100644 --- a/x/config/cli/binder_test.go +++ b/x/config/cli/binder_test.go @@ -870,6 +870,20 @@ func TestAMapReadWholeIsReplacedNotMerged(t *testing.T) { type recursiveUintMap map[uint32]*recursiveUintMap +func TestDiscardDeprecatedSection(t *testing.T) { + type hasDiscard struct { + Deprecated markup.Discard + Keep string + } + + var c hasDiscard + t.Setenv("TEST_DEPRECATED", "ignored") + file := "Keep = 'kept'\n[Deprecated]\nURL = 'https://user:pass@example.com/secret'\n[Deprecated.Nested]\nToken = 'hunter2'\n" + require.NoError(t, run(t, &c, testOptions, "--config", writeConfig(t, file))) + assert.Equal(t, hasDiscard{Keep: "kept"}, c) + assert.Nil(t, flagsOf(t, &hasDiscard{}, testOptions).Lookup("deprecated")) +} + func TestRecursiveConfigFileOnlyType(t *testing.T) { type hasRecursiveMap struct { Tree recursiveMap diff --git a/x/config/cli/entry.go b/x/config/cli/entry.go index b71bce9949..c69892b265 100644 --- a/x/config/cli/entry.go +++ b/x/config/cli/entry.go @@ -192,6 +192,10 @@ func (e *typedEntry[T]) decode(cc commandConfig) error { dst := reflect.ValueOf(e.dst).Elem() supplied := map[string]sourceLayer{} for _, k := range e.leaves { + // A discarded value is parsed into any, then dropped. + if decode.IsDiscard(k.goType) { + continue + } if len(k.entries) > 0 { layer, err := e.decodeEntries(k, cc, dst) if err != nil { diff --git a/x/config/cli/walk.go b/x/config/cli/walk.go index 5c44aebad2..615766cdb3 100644 --- a/x/config/cli/walk.go +++ b/x/config/cli/walk.go @@ -162,6 +162,6 @@ func derefOrZero(f reflect.Value) reflect.Value { // A struct already being walked is a leaf, callers are expected to have dereferenced the field already. func (e *typedEntry[T]) isLeaf(field reflect.StructField, elem reflect.Value, scope walkScope) bool { lang := e.b.opts.Markup - return elem.Kind() != reflect.Struct || lang.IsLeaf(field.Type) || lang.IsLeaf(elem.Type()) || - slices.Contains(scope.ancestors, elem.Type()) + return decode.IsDiscard(field.Type) || elem.Kind() != reflect.Struct || lang.IsLeaf(field.Type) || + lang.IsLeaf(elem.Type()) || slices.Contains(scope.ancestors, elem.Type()) } diff --git a/x/config/internal/decode/discard.go b/x/config/internal/decode/discard.go new file mode 100644 index 0000000000..55e8decaa6 --- /dev/null +++ b/x/config/internal/decode/discard.go @@ -0,0 +1,14 @@ +package decode + +import "reflect" + +// Discard accepts any value a config file holds at its key, a table, an array or a scalar, and keeps none of it. +// Use it for deprecated keys that old files still carry. +type Discard struct{} + +var discardType = reflect.TypeFor[Discard]() + +// IsDiscard reports whether t is [Discard], or a pointer to it. +func IsDiscard(t reflect.Type) bool { + return DerefType(t) == discardType +} diff --git a/x/config/internal/decode/filevalues.go b/x/config/internal/decode/filevalues.go index d5e6c0f10d..d7f161ee99 100644 --- a/x/config/internal/decode/filevalues.go +++ b/x/config/internal/decode/filevalues.go @@ -111,7 +111,10 @@ func (n *KeyNode) Lookup(v reflect.Value, path []string) (reflect.Value, bool) { return v, true } -var stringType = reflect.TypeFor[string]() +var ( + stringType = reflect.TypeFor[string]() + anyType = reflect.TypeFor[any]() +) // Config file map keys are text, so every key is read as a string and parsed as its flag would be (see SetText), even // one the markup could read itself, so two texts for one key are always caught. @@ -128,6 +131,11 @@ type fileTyper struct { } func (ft fileTyper) valueType(t reflect.Type) (reflect.Type, error) { + // any is the slot the markup parses into, so a table, an array or a scalar all decode. + if IsDiscard(t) { + return anyType, nil + } + if _, ok := ft.visiting[t]; ok { ft.visiting[t] = true // Recursion itself is not an error, for example, nothing should stop the type below from being used. @@ -238,6 +246,10 @@ func (ft fileTyper) structType(t reflect.Type) (reflect.Type, error) { // Turns a value decoded as FileValueType(t) back into t, parsing the map keys that were read as strings. func FromFile(t reflect.Type, v reflect.Value) (reflect.Value, error) { + if IsDiscard(t) { + return reflect.Zero(t), nil + } + if v.Type() == t { return v, nil } diff --git a/x/config/markup/decode.go b/x/config/markup/decode.go index a4b95cdf94..2bc2816601 100644 --- a/x/config/markup/decode.go +++ b/x/config/markup/decode.go @@ -46,7 +46,8 @@ func Decode(lang Markup, data []byte, v any) error { for _, leaf := range w.leaves { raw, ok := w.keys.Lookup(fileValues.Elem(), leaf.fileKey) - if !ok { + // A discarded value is parsed into any, then dropped. + if !ok || decode.IsDiscard(leaf.goType) { continue } @@ -117,7 +118,7 @@ func (w *keyWalker) walk(t reflect.Type, fileKey, goPath []string, ancestors []r } // A struct already being walked is a leaf, so a recursive type can't walk forever. - if elem.Kind() != reflect.Struct || w.lang.IsLeaf(f.Type) || w.lang.IsLeaf(elem) || + if decode.IsDiscard(f.Type) || elem.Kind() != reflect.Struct || w.lang.IsLeaf(f.Type) || w.lang.IsLeaf(elem) || slices.Contains(ancestors, elem) { fileType, err := decode.FileValueType(elem, w.lang) if err != nil { diff --git a/x/config/markup/markup.go b/x/config/markup/markup.go index 539eda1d82..e2220bcfaa 100644 --- a/x/config/markup/markup.go +++ b/x/config/markup/markup.go @@ -16,5 +16,9 @@ import ( // Markup is a configuration language, described as its own decoder reads a document. type Markup = decode.Markup +// Discard accepts any value a config file holds at its key, a table, an array or a scalar, and keeps none of it. +// Use it for deprecated keys that old files still carry. +type Discard = decode.Discard + // Err is what a caller reports without a Markup: no language is chosen silently on its behalf. var Err = errors.New("no markup language set: see x/config/markup/tomlmarkup for an example") diff --git a/x/config/markup/markuptests/markuptests.go b/x/config/markup/markuptests/markuptests.go index bbcee3c795..e628daf8d7 100644 --- a/x/config/markup/markuptests/markuptests.go +++ b/x/config/markup/markuptests/markuptests.go @@ -14,35 +14,66 @@ import ( func Run(t *testing.T, lang markup.Markup) { t.Helper() - data, err := lang.Marshal(struct { - Size, Duration, Rate, URL, SecretURL, SecretString, Timestamp string - }{ - Size: "1.5kb", - Duration: "1m30s", - Rate: "every2s:3", - URL: "https://user:pass@example.com/path?q=1", - SecretURL: "https://user:pass@example.com/secret", - SecretString: "hunter2", - Timestamp: "2025-06-15T12:30:45Z", + t.Run("roundtrip", func(t *testing.T) { + data, err := lang.Marshal(struct { + Size, Duration, Rate, URL, SecretURL, SecretString, Timestamp string + }{ + Size: "1.5kb", + Duration: "1m30s", + Rate: "every2s:3", + URL: "https://user:pass@example.com/path?q=1", + SecretURL: "https://user:pass@example.com/secret", + SecretString: "hunter2", + Timestamp: "2025-06-15T12:30:45Z", + }) + require.NoError(t, err) + + var got struct { + Size config.Size + Duration config.Duration + Rate config.Rate + URL config.URL + SecretURL config.SecretURL + SecretString config.SecretString + Timestamp config.Timestamp + } + require.NoError(t, lang.Unmarshal(data, &got), string(data)) + + require.Equal(t, 1500*config.Byte, got.Size) + require.Equal(t, 90*time.Second, got.Duration.Duration()) + require.Equal(t, config.Rate{Limit: rate.Every(2 * time.Second), Burst: 3}, got.Rate) + require.Equal(t, "https://user:pass@example.com/path?q=1", got.URL.URL().String()) + require.Equal(t, "https://user:pass@example.com/secret", got.SecretURL.URL().String()) + require.Equal(t, config.SecretString("hunter2"), got.SecretString) + require.Equal(t, config.NewTimestamp(time.Date(2025, 6, 15, 12, 30, 45, 0, time.UTC)), got.Timestamp) + }) + + t.Run("discard", func(t *testing.T) { + type configWithDiscard struct { + Deprecated markup.Discard + Keep string + } + + // All Deprecated field values contain the word "old" to ensure it's not serialized + for _, tc := range []struct { + name string + value any + }{ + {"leaf", "old"}, + {"array", []any{map[string]any{"Name": "old"}, map[string]any{"Name": "b"}}}, + {"object", map[string]any{"URL": "https://user:pass@example.com/old/secret", "Secret": "old"}}, + } { + t.Run(tc.name, func(t *testing.T) { + data, err := lang.Marshal(map[string]any{"Deprecated": tc.value, "Keep": "kept"}) + require.NoError(t, err) + + got := configWithDiscard{Keep: "default"} + require.NoError(t, markup.Decode(lang, data, &got), string(data)) + require.Equal(t, configWithDiscard{Keep: "kept"}, got) + r, err := lang.Marshal(got) + require.NoError(t, err) + require.NotContains(t, string(r), "old") + }) + } }) - require.NoError(t, err) - - var got struct { - Size config.Size - Duration config.Duration - Rate config.Rate - URL config.URL - SecretURL config.SecretURL - SecretString config.SecretString - Timestamp config.Timestamp - } - require.NoError(t, lang.Unmarshal(data, &got), string(data)) - - require.Equal(t, 1500*config.Byte, got.Size) - require.Equal(t, 90*time.Second, got.Duration.Duration()) - require.Equal(t, config.Rate{Limit: rate.Every(2 * time.Second), Burst: 3}, got.Rate) - require.Equal(t, "https://user:pass@example.com/path?q=1", got.URL.URL().String()) - require.Equal(t, "https://user:pass@example.com/secret", got.SecretURL.URL().String()) - require.Equal(t, config.SecretString("hunter2"), got.SecretString) - require.Equal(t, config.NewTimestamp(time.Date(2025, 6, 15, 12, 30, 45, 0, time.UTC)), got.Timestamp) }