@@ -11,6 +11,7 @@ import (
1111 "strings"
1212 "testing"
1313 "time"
14+ "unsafe"
1415
1516 "github.com/goccy/go-yaml"
1617 "github.com/goccy/go-yaml/ast"
@@ -1345,6 +1346,81 @@ func TestEncoder_MultipleDocuments(t *testing.T) {
13451346 }
13461347}
13471348
1349+ func TestEncoder_UnmarshallableTypes (t * testing.T ) {
1350+ for _ , test := range []struct {
1351+ desc string
1352+ input any
1353+ expectedErr string
1354+ }{
1355+ {
1356+ desc : "channel" ,
1357+ input : make (chan int ),
1358+ expectedErr : "unknown value type chan int" ,
1359+ },
1360+ {
1361+ desc : "function" ,
1362+ input : func () {},
1363+ expectedErr : "unknown value type func()" ,
1364+ },
1365+ {
1366+ desc : "complex number" ,
1367+ input : complex (10 , 11 ),
1368+ expectedErr : "unknown value type complex128" ,
1369+ },
1370+ {
1371+ desc : "unsafe pointer" ,
1372+ input : unsafe .Pointer (& struct {}{}),
1373+ expectedErr : "unknown value type unsafe.Pointer" ,
1374+ },
1375+ {
1376+ desc : "uintptr" ,
1377+ input : uintptr (0x1234 ),
1378+ expectedErr : "unknown value type uintptr" ,
1379+ },
1380+ {
1381+ desc : "map with channel" ,
1382+ input : map [string ]any {"key" : make (chan string )},
1383+ expectedErr : "unknown value type chan string" ,
1384+ },
1385+ {
1386+ desc : "nested map with func" ,
1387+ input : map [string ]any {
1388+ "a" : map [string ]any {
1389+ "b" : func (_ string ) {},
1390+ },
1391+ },
1392+ expectedErr : "unknown value type func(string)" ,
1393+ },
1394+ {
1395+ desc : "slice with channel" ,
1396+ input : []any {make (chan bool )},
1397+ expectedErr : "unknown value type chan bool" ,
1398+ },
1399+ {
1400+ desc : "nested slice with complex number" ,
1401+ input : []any {[]any {complex (10 , 11 )}},
1402+ expectedErr : "unknown value type complex128" ,
1403+ },
1404+ {
1405+ desc : "struct with unsafe pointer" ,
1406+ input : struct {
1407+ Field unsafe.Pointer `yaml:"field"`
1408+ }{},
1409+ expectedErr : "unknown value type unsafe.Pointer" ,
1410+ },
1411+ } {
1412+ t .Run (test .desc , func (t * testing.T ) {
1413+ var buf bytes.Buffer
1414+ err := yaml .NewEncoder (& buf ).Encode (test .input )
1415+ if err == nil {
1416+ t .Errorf ("expect error:\n %s\n but got none\n " , test .expectedErr )
1417+ } else if err .Error () != test .expectedErr {
1418+ t .Errorf ("expect error:\n %s\n actual\n %s\n " , test .expectedErr , err )
1419+ }
1420+ })
1421+ }
1422+ }
1423+
13481424func ExampleMarshal_node () {
13491425 type T struct {
13501426 Text ast.Node `yaml:"text"`
0 commit comments