flabk/pkg/ld/unmarshal.go

18 lines
381 B
Go
Raw Normal View History

2022-08-03 15:20:03 +01:00
package ld
import (
"fmt"
"reflect"
"sectorinf.com/emilis/flabk/pkg/ld/internal/parse"
"sectorinf.com/emilis/flabk/pkg/ld/internal/parse/chunk"
)
func Unmarshal[T any](v []byte) (T, error) {
ptr := new(T)
if err := parse.SetValue(reflect.ValueOf(ptr), reflect.TypeOf(ptr), chunk.New(v)); err != nil {
return *ptr, fmt.Errorf("unmarshal: %w", err)
}
return *ptr, nil
}