fix(decode): report the smallest unknown field deterministically

Assisted-by: GLM 5.3
This commit is contained in:
2026-09-17 23:11:38 +02:00
parent 4def1b3e8b
commit 3c8ac859c0
4 changed files with 42 additions and 4 deletions
+21
View File
@@ -5,6 +5,7 @@ package interpres
import (
"math"
"strings"
"testing"
"time"
)
@@ -316,6 +317,26 @@ func TestDisallowUnknownFields(t *testing.T) {
}
}
func TestDisallowUnknownFieldsReportsSmallestKey(t *testing.T) {
// Map iteration order is random, so the reported key must be chosen
// deterministically: the smallest unknown key, whichever order the map
// iterates in.
type C struct {
Known string `toml:"known"`
}
data := []byte("known = \"x\"\nzeta = 1\nalpha = 2\nmu = 3\n")
for range 20 {
var c C
err := NewDecoder().DisallowUnknownFields().Decode(data, &c)
if err == nil {
t.Fatal("expected error for unknown fields")
}
if !strings.Contains(err.Error(), `unknown field "alpha"`) {
t.Fatalf("err = %v, want the smallest unknown key alpha", err)
}
}
}
func TestSkippedFieldTag(t *testing.T) {
type C struct {
Keep string `toml:"keep"`