EncoderToBytes allows augmenting any type with a MarshalMsg method into a method that reads from T and returns a []byte. Provide an instance of T. This value isn't used. See ReadArrayBytes or ReadMapBytes "struct" examples for usage.
(_ T)
| 369 | // Provide an instance of T. This value isn't used. |
| 370 | // See ReadArrayBytes or ReadMapBytes "struct" examples for usage. |
| 371 | func EncoderToBytes[T any, _ FlexibleMarshaler[T]](_ T) func([]byte, T) []byte { |
| 372 | return func(b []byte, t T) []byte { |
| 373 | // Check if T implements Marshaler |
| 374 | if marshaler, ok := any(t).(Marshaler); ok { |
| 375 | b, _ = marshaler.MarshalMsg(b) |
| 376 | return b |
| 377 | } |
| 378 | // Check if *T implements Marshaler |
| 379 | if ptrMarshaler, ok := any(&t).(Marshaler); ok { |
| 380 | b, _ = ptrMarshaler.MarshalMsg(b) |
| 381 | return b |
| 382 | } |
| 383 | // The compiler should have asserted this. |
| 384 | panic("type does not implement Marshaler") |
| 385 | } |
| 386 | } |
searching dependent graphs…