Skip to content

Commit fe1b2f9

Browse files
committed
Add test case to override interface type
1 parent 3144318 commit fe1b2f9

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

decode_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,35 @@ func TestDecoder_CustomUnmarshaler(t *testing.T) {
19401940
t.Fatalf("failed to switch to custom unmarshaler. got: %q", v.Foo)
19411941
}
19421942
})
1943+
1944+
t.Run("override interface type", func(t *testing.T) {
1945+
type I interface{}
1946+
type T struct {
1947+
Foo string `yaml:"foo"`
1948+
}
1949+
var i I
1950+
src := []byte(`foo: "bar"`)
1951+
if err := yaml.UnmarshalWithOptions(src, &i, yaml.CustomUnmarshaler[I](func(dst *I, b []byte) error {
1952+
var v T
1953+
if err := yaml.Unmarshal(b, &v); err != nil {
1954+
t.Fatal(err)
1955+
}
1956+
if v.Foo != "bar" {
1957+
t.Fatalf("failed to use unmarshal function. got %q", v.Foo)
1958+
}
1959+
*dst = &v
1960+
return nil
1961+
})); err != nil {
1962+
t.Fatal(err)
1963+
}
1964+
if v, ok := i.(*T); ok {
1965+
if v.Foo != "bar" {
1966+
t.Fatalf("failed to decode with custom interface unmarshaler. got: %q", v.Foo)
1967+
}
1968+
} else {
1969+
t.Fatalf("failed to switch to custom interface unmarshaler.")
1970+
}
1971+
})
19431972
}
19441973

19451974
func TestDecoder_CustomInterfaceUnmarshaler(t *testing.T) {
@@ -1989,6 +2018,35 @@ func TestDecoder_CustomInterfaceUnmarshaler(t *testing.T) {
19892018
t.Fatalf("failed to switch to custom interface unmarshaler. got: %q", v.Foo)
19902019
}
19912020
})
2021+
2022+
t.Run("override interface type", func(t *testing.T) {
2023+
type I interface{}
2024+
type T struct {
2025+
Foo string `yaml:"foo"`
2026+
}
2027+
var i I
2028+
src := []byte(`foo: "bar"`)
2029+
if err := yaml.UnmarshalWithOptions(src, &i, yaml.CustomInterfaceUnmarshaler[I](func(dst *I, f func(interface{}) error) error {
2030+
var v T
2031+
if err := f(&v); err != nil {
2032+
t.Fatal(err)
2033+
}
2034+
if v.Foo != "bar" {
2035+
t.Fatalf("failed to use unmarshal function. got %q", v.Foo)
2036+
}
2037+
*dst = &v
2038+
return nil
2039+
})); err != nil {
2040+
t.Fatal(err)
2041+
}
2042+
if v, ok := i.(*T); ok {
2043+
if v.Foo != "bar" {
2044+
t.Fatalf("failed to decode with custom interface unmarshaler. got: %q", v.Foo)
2045+
}
2046+
} else {
2047+
t.Fatalf("failed to switch to custom interface unmarshaler.")
2048+
}
2049+
})
19922050
}
19932051

19942052
type unmarshalContext struct {

0 commit comments

Comments
 (0)