Skip to content

Commit 6cb79a7

Browse files
committed
feat: allow using custom unmarshaller
1 parent d5f6cd1 commit 6cb79a7

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

decode.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func (e *InvalidDecodeError) Error() string {
5757
// greater than the parent DataSize.
5858
var ErrElementOverflow = errors.New("ebml: element overflow")
5959

60+
// Unmarshaler is implemented by types that decode their element body
61+
// themselves instead of the default reflect-based mapping.
62+
type Unmarshaler interface {
63+
UnmarshalEBML(d *Decoder, el Element) error
64+
}
65+
6066
// DecodeHeader decodes the document header.
6167
func (d *Decoder) DecodeHeader() (*EBML, error) {
6268
for {
@@ -406,6 +412,11 @@ func (d *Decoder) decodeSingle(el Element, val reflect.Value) error {
406412
val = v.Index(n)
407413
}
408414
}
415+
if val.CanAddr() {
416+
if u, ok := val.Addr().Interface().(Unmarshaler); ok {
417+
return u.UnmarshalEBML(d, el)
418+
}
419+
}
409420
if err := validateReflectType(val, sch, 0); err != nil {
410421
if e, ok := err.(*DecodeTypeError); ok {
411422
e.extendError(sch.Name)

0 commit comments

Comments
 (0)