fix(decode): decode into a defined string or bool type
Assisted-by: DeepSeek V4.1 Flash
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user