reflection: add initial New() validating type and triggering a scan
Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
@@ -1,2 +1,36 @@
|
|||||||
// Package reflection helps Marshalling/Unmarshalling data to structs
|
// Package reflection helps Marshalling/Unmarshalling data to structs
|
||||||
package reflection
|
package reflection
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reflection provides Marshalling/Unmarshalling oriented view
|
||||||
|
// of a value
|
||||||
|
type Reflection[T any] struct {
|
||||||
|
rv reflect.Value
|
||||||
|
rt reflect.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the object it reflects
|
||||||
|
func (r *Reflection[T]) Value() *T {
|
||||||
|
return r.rv.Interface().(*T)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a Reflection of the given pointer
|
||||||
|
func New[T any](v *T) (*Reflection[T], error) {
|
||||||
|
if v == nil {
|
||||||
|
err := &InvalidUnmarshalError{Type: reflect.TypeOf(v)}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
r := &Reflection[T]{
|
||||||
|
rv: reflect.ValueOf(v),
|
||||||
|
rt: reflect.TypeOf(v),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.scan(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package reflection
|
||||||
|
|
||||||
|
func (*Reflection[T]) scan() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user