@@ -1294,6 +1294,40 @@ func TestPoolReleasesOversizedBuffers(t *testing.T) {
12941294 require .Equal (t , "small" , s )
12951295}
12961296
1297+ // Issue #10: types implementing both TextUnmarshaler and BinaryUnmarshaler
1298+ // should use TextUnmarshaler when wire format is str.
1299+ type dualUnmarshaler struct {
1300+ text string
1301+ binary []byte
1302+ }
1303+
1304+ func (d * dualUnmarshaler ) MarshalText () ([]byte , error ) { return []byte (d .text ), nil }
1305+ func (d * dualUnmarshaler ) UnmarshalText (b []byte ) error { d .text = string (b ); return nil }
1306+ func (d * dualUnmarshaler ) MarshalBinary () ([]byte , error ) { return d .binary , nil }
1307+ func (d * dualUnmarshaler ) UnmarshalBinary (b []byte ) error { d .binary = b ; return nil }
1308+
1309+ func TestTextUnmarshalerWithStrFormat (t * testing.T ) {
1310+ // Encode a plain string (str format) and decode into a type that
1311+ // implements both BinaryUnmarshaler and TextUnmarshaler.
1312+ b , err := msgpack .Marshal ("hello" )
1313+ require .NoError (t , err )
1314+
1315+ var du dualUnmarshaler
1316+ require .NoError (t , msgpack .Unmarshal (b , & du ))
1317+ // Should have used TextUnmarshaler because the wire format is str.
1318+ require .Equal (t , "hello" , du .text )
1319+ require .Nil (t , du .binary )
1320+
1321+ // Encode as binary (bin format) and decode — should use BinaryUnmarshaler.
1322+ b , err = msgpack .Marshal ([]byte {0xDE , 0xAD })
1323+ require .NoError (t , err )
1324+
1325+ var du2 dualUnmarshaler
1326+ require .NoError (t , msgpack .Unmarshal (b , & du2 ))
1327+ require .Equal (t , []byte {0xDE , 0xAD }, du2 .binary )
1328+ require .Equal (t , "" , du2 .text )
1329+ }
1330+
12971331func mustParseTime (format , s string ) time.Time {
12981332 tm , err := time .Parse (format , s )
12991333 if err != nil {
0 commit comments