From 41a2d95d4c029e685d5ceaba9bd726db1c6b9853 Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 22 Apr 2022 23:52:21 +0800 Subject: [PATCH] feat: add more marshaler interface --- marshaler.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/marshaler.go b/marshaler.go index 7de7ce3..d20e181 100644 --- a/marshaler.go +++ b/marshaler.go @@ -1,7 +1,33 @@ package gsr -// Marshaler interface for Marshal/Unmarshal data +// Marshaler interface type Marshaler interface { Marshal(v interface{}) ([]byte, error) - Unmarshal(data []byte, ptr interface{}) error +} + +// Unmarshaler interface +type Unmarshaler interface { + Unmarshal(v []byte, ptr interface{}) error +} + +// DataMarshaler interface for Marshal/Unmarshal data +type DataMarshaler interface { + Marshaler + Unmarshaler +} + +// MarshalFunc define +type MarshalFunc func(v interface{}) ([]byte, error) + +// Marshal implements the Marshaler +func (m MarshalFunc) Marshal(v interface{}) ([]byte, error) { + return m(v) +} + +// UnmarshalFunc define +type UnmarshalFunc func(v []byte, ptr interface{}) error + +// Unmarshal implements the Unmarshaler +func (u UnmarshalFunc) Unmarshal(v []byte, ptr interface{}) error { + return u(v, ptr) }