fix(decode): decode into a defined string or bool type

Assisted-by: DeepSeek V4.1 Flash
This commit is contained in:
2026-09-19 02:40:51 +02:00
parent 942c4b1489
commit 9023784da3
3 changed files with 33 additions and 1 deletions
+22
View File
@@ -725,3 +725,25 @@ func TestDecodeErrorOnMapDestination(t *testing.T) {
t.Fatalf("Path = %v", de.Path)
}
}
func TestUnmarshalIntoDefinedScalarTypes(t *testing.T) {
// A defined type whose underlying kind is string or bool takes the value.
// A bare reflect Set panics on such a type, because a string is not
// assignable to a defined string type without a conversion.
type Name string
type Flag bool
type Cfg struct {
N Name `toml:"n"`
F Flag `toml:"f"`
}
var cfg Cfg
if err := Unmarshal([]byte("n = \"x\"\nf = true\n"), &cfg); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if cfg.N != "x" {
t.Errorf("N = %q, want \"x\"", cfg.N)
}
if !cfg.F {
t.Error("F = false, want true")
}
}