Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions x/config/cli/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions x/config/cli/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions x/config/cli/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
14 changes: 14 additions & 0 deletions x/config/internal/decode/discard.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 13 additions & 1 deletion x/config/internal/decode/filevalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions x/config/markup/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/config/markup/markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
91 changes: 61 additions & 30 deletions x/config/markup/markuptests/markuptests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading