@@ -1940,6 +1940,35 @@ func TestDecoder_CustomUnmarshaler(t *testing.T) {
1940
1940
t .Fatalf ("failed to switch to custom unmarshaler. got: %q" , v .Foo )
1941
1941
}
1942
1942
})
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
+ })
1943
1972
}
1944
1973
1945
1974
func TestDecoder_CustomInterfaceUnmarshaler (t * testing.T ) {
@@ -1989,6 +2018,35 @@ func TestDecoder_CustomInterfaceUnmarshaler(t *testing.T) {
1989
2018
t .Fatalf ("failed to switch to custom interface unmarshaler. got: %q" , v .Foo )
1990
2019
}
1991
2020
})
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
+ })
1992
2050
}
1993
2051
1994
2052
type unmarshalContext struct {
0 commit comments